SELECT 
  p.product_id, 
  ifnull(
    count(dp.post_id), 
    0
  ) as discussion_amount_posts 
FROM 
  cscart_discussion 
  INNER JOIN cscart_products as p ON (
    cscart_discussion.object_id = p.product_id
  ) 
  INNER JOIN cscart_discussion_posts as dp ON (
    cscart_discussion.thread_id = dp.thread_id 
    AND cscart_discussion.object_type = 'P'
  ) 
WHERE 
  dp.status = 'A' 
  and p.product_id in (
    431190, 
    431092, 
    431106, 
    431147, 
    430849, 
    431162, 
    431128, 
    431099, 
    431132, 
    431202, 
    430637, 
    430639, 
    431096, 
    431168, 
    431141, 
    431153, 
    431137, 
    431155, 
    431033, 
    431130, 
    430911, 
    430640, 
    431134, 
    431125, 
    431154, 
    431090, 
    431146, 
    431065, 
    431067, 
    431187, 
    431048, 
    431094, 
    430697, 
    430767, 
    431050, 
    431104, 
    430745, 
    431124, 
    431117, 
    431191, 
    431157, 
    431160, 
    430681, 
    431116, 
    430743, 
    430850, 
    430908, 
    431084, 
    431107, 
    430998, 
    431026, 
    430655, 
    430744, 
    431100, 
    431115, 
    430915, 
    430916, 
    430636, 
    430924, 
    431195, 
    430747, 
    430803, 
    431176, 
    431203, 
    430748, 
    431204, 
    431133, 
    430648, 
    431196, 
    430823, 
    431158, 
    431199, 
    431122, 
    430903, 
    430848, 
    431139, 
    430831, 
    431150, 
    430665, 
    430696, 
    431145, 
    431102, 
    430771, 
    430907, 
    430649, 
    430646, 
    430647, 
    430912, 
    430935, 
    430638, 
    430766, 
    431097, 
    431151, 
    430821, 
    430824, 
    431359, 
    430857, 
    431201, 
    430746, 
    431068, 
    430813, 
    430925, 
    431085, 
    431103, 
    431163, 
    431164, 
    430817, 
    430654, 
    430902, 
    431313, 
    431053, 
    431148, 
    430751, 
    431044, 
    431120, 
    431456, 
    430660, 
    430825, 
    430678, 
    430680, 
    430959, 
    431013, 
    431127, 
    430691, 
    430756, 
    430820, 
    431109, 
    431161, 
    430737, 
    431035, 
    431041, 
    431194, 
    430875, 
    431049, 
    430819, 
    431083, 
    430808, 
    430989, 
    431080, 
    430635, 
    430650, 
    430629, 
    430860, 
    431126, 
    430657, 
    430807, 
    430921, 
    430922, 
    430843, 
    430812, 
    430868, 
    431073, 
    431144, 
    430789, 
    430795, 
    430758, 
    431082, 
    430818, 
    430901, 
    430928, 
    430950, 
    431156, 
    430658, 
    430673, 
    431110, 
    430684, 
    431066, 
    431114, 
    430689, 
    430822, 
    431118, 
    431064, 
    430769, 
    430892, 
    431043, 
    431169, 
    430628, 
    430664, 
    430842, 
    430834, 
    430859, 
    430906, 
    431459, 
    430768, 
    430890, 
    430988, 
    431183, 
    431250, 
    430703, 
    430838, 
    430962, 
    431008, 
    430909, 
    431063, 
    431159, 
    430695, 
    430904, 
    430923, 
    430815, 
    430832, 
    430914, 
    431356, 
    430891, 
    430939, 
    431197, 
    430880, 
    430930, 
    431089, 
    431136, 
    430847, 
    430893, 
    431135, 
    430854, 
    430913, 
    430662, 
    430676, 
    430980, 
    431078, 
    430787, 
    431142, 
    431226, 
    431393, 
    430672, 
    430760, 
    430806, 
    431174, 
    430633, 
    430829, 
    430869, 
    431031, 
    431119, 
    430753, 
    431325, 
    431327, 
    430712, 
    430976, 
    431111, 
    430644, 
    430796, 
    430844, 
    431123, 
    430772, 
    431023, 
    431062, 
    431088, 
    430663, 
    430884, 
    430899, 
    430981, 
    431238, 
    431245, 
    431251, 
    430801, 
    430845, 
    430876, 
    430958, 
    431467, 
    430898, 
    431032, 
    431093, 
    431167, 
    431121, 
    430698, 
    430851, 
    430927, 
    431314, 
    431291, 
    431423, 
    431501, 
    430651, 
    430730, 
    430883, 
    431029, 
    431072, 
    431131, 
    431220, 
    430653, 
    430809, 
    430910, 
    431046, 
    431205, 
    430685, 
    430687, 
    430722, 
    430866, 
    431042, 
    431140, 
    431198, 
    431333, 
    430631, 
    430896, 
    430900, 
    431185, 
    431463, 
    430932, 
    430937, 
    430947, 
    430994, 
    430999, 
    431077, 
    431345, 
    430634, 
    430645, 
    430714, 
    430862, 
    430885, 
    430887, 
    430992, 
    431338, 
    430656, 
    430738, 
    430761, 
    430865, 
    431054, 
    431316, 
    430626, 
    430870, 
    431192, 
    430708, 
    430797, 
    430853, 
    431019, 
    431326, 
    431365, 
    430677, 
    430749, 
    431061, 
    430802, 
    430846, 
    430873, 
    430881, 
    431011, 
    431024, 
    431242, 
    430960, 
    430993, 
    431039, 
    431322, 
    430836, 
    430936, 
    431003, 
    431052, 
    431395, 
    431429, 
    430704, 
    430707, 
    430905, 
    430841, 
    430969, 
    431020, 
    431022, 
    431091, 
    431248, 
    431264, 
    430810, 
    431051, 
    431315, 
    431503, 
    430652, 
    430861, 
    430940, 
    430984, 
    431113, 
    431178, 
    431193, 
    431247, 
    431487, 
    431006, 
    431200, 
    431301, 
    431439, 
    430782, 
    430837, 
    431074, 
    431339, 
    431426, 
    430816, 
    430926, 
    431252, 
    430641, 
    430794, 
    430968, 
    431275, 
    430788, 
    430879, 
    431171, 
    431334, 
    430661, 
    430811, 
    431098, 
    431055, 
    430742, 
    430839, 
    430675, 
    430800, 
    430805, 
    430855, 
    430863, 
    430953, 
    430979, 
    431428, 
    430778, 
    431005, 
    431244, 
    430688, 
    430852, 
    430874, 
    430894, 
    431262, 
    431336, 
    430713, 
    430755, 
    430826, 
    430827, 
    430878, 
    430917, 
    430931, 
    430952, 
    430995, 
    431000, 
    431027, 
    431028, 
    431101, 
    431368, 
    431504, 
    430856, 
    430991, 
    431177, 
    431184, 
    431451, 
    430780, 
    430897, 
    431234, 
    431342, 
    431358, 
    431469, 
    430864, 
    430694, 
    431357, 
    431420, 
    430671, 
    430840, 
    430871, 
    430918, 
    430977, 
    430666, 
    430702, 
    430888, 
    431010, 
    431344, 
    430667, 
    430706, 
    430972, 
    430990, 
    430982, 
    431025, 
    431037, 
    431129, 
    431289, 
    431297, 
    430692, 
    430895, 
    430997, 
    431007, 
    431165, 
    431308, 
    431381, 
    430933, 
    431295, 
    430723, 
    430804, 
    430946, 
    430674, 
    430679, 
    430720, 
    431237, 
    431047, 
    431149, 
    431280, 
    431288, 
    431354, 
    431355, 
    431376, 
    431424, 
    431470, 
    430726, 
    431087, 
    431138, 
    431457, 
    430627, 
    430886, 
    430920, 
    431112, 
    431392, 
    430949, 
    431218, 
    431350, 
    430718, 
    430835, 
    431021, 
    431034, 
    431056, 
    431108, 
    431246, 
    431309, 
    430828, 
    430974, 
    431440, 
    431494, 
    430867, 
    430872, 
    430889, 
    431152, 
    431505, 
    430683, 
    430741, 
    431012, 
    431340, 
    431411, 
    430690, 
    430793, 
    430877, 
    431045, 
    431211, 
    430739, 
    431425, 
    431465, 
    430715, 
    430799, 
    431329, 
    431403, 
    430961, 
    431240, 
    431284, 
    431341, 
    430770, 
    430975, 
    431360, 
    431464, 
    430735, 
    430791, 
    430858, 
    431030, 
    431239, 
    431343, 
    431349, 
    430643, 
    430948, 
    431038, 
    431219, 
    431406, 
    431474, 
    430734, 
    430934, 
    431186, 
    431328, 
    430762, 
    430764, 
    430790, 
    430830, 
    430740, 
    431283, 
    431500, 
    430732, 
    430765, 
    430786, 
    430882, 
    430919, 
    431303, 
    431353, 
    431410, 
    430686, 
    430701, 
    430711, 
    431182, 
    431230, 
    431243, 
    431279, 
    431498, 
    431060, 
    431405, 
    431497, 
    430792, 
    431231, 
    431274, 
    431495, 
    431076, 
    431175, 
    431249, 
    431422, 
    431290, 
    431311, 
    431337, 
    431419, 
    430659, 
    430957, 
    430965, 
    430983, 
    431434, 
    430642, 
    430954, 
    431166, 
    431260, 
    431380, 
    431409, 
    431004, 
    431105, 
    431304, 
    431346, 
    431480, 
    430963, 
    431287, 
    430781, 
    430964, 
    430966, 
    431002, 
    431071, 
    431458, 
    430754, 
    430759, 
    430814, 
    431015, 
    431324, 
    430798, 
    431079, 
    431217, 
    431352, 
    431408, 
    431452, 
    431453, 
    430669, 
    430750, 
    430944, 
    431273, 
    431362, 
    431407, 
    431481, 
    431188, 
    431222, 
    431394, 
    430700, 
    430705, 
    430727, 
    430938, 
    431001, 
    430978, 
    430985, 
    431009, 
    431057, 
    431208, 
    431241, 
    431335, 
    431447, 
    430699, 
    430752, 
    430757, 
    430773, 
    430942, 
    430973, 
    431210, 
    431383, 
    431502, 
    431292, 
    431427, 
    431488, 
    430719, 
    431017, 
    431236, 
    431330, 
    431040, 
    431466, 
    430630, 
    430929, 
    430943, 
    431227, 
    431235, 
    430632, 
    430693, 
    431263, 
    431482, 
    430955, 
    431018, 
    430971, 
    431069, 
    431364, 
    431378, 
    431415, 
    431302, 
    431361, 
    431421, 
    430717, 
    430736, 
    431281, 
    431385, 
    431285, 
    430725, 
    431223, 
    431086, 
    431212, 
    431369, 
    431402, 
    430785, 
    430833, 
    430987, 
    431300, 
    430724, 
    430941, 
    431377, 
    431384, 
    430733, 
    431081, 
    431181, 
    431229, 
    431472, 
    430956, 
    431253, 
    431454, 
    430721, 
    431387, 
    431225, 
    431431, 
    430682, 
    430951, 
    431438, 
    431450, 
    430716, 
    431382, 
    431446, 
    431468, 
    431401, 
    431412, 
    431430, 
    430945, 
    431143, 
    431215, 
    431058, 
    431170, 
    431294, 
    431312, 
    431367, 
    431460, 
    430731, 
    431036, 
    431224, 
    431259, 
    431276, 
    431310, 
    430777, 
    431332, 
    431396, 
    431298, 
    431414, 
    431014, 
    431172, 
    431207, 
    431296, 
    431471, 
    431479, 
    431180, 
    431233, 
    431442, 
    431189, 
    431413, 
    431455, 
    431213, 
    431278, 
    431351, 
    430970, 
    430996, 
    431095, 
    431299, 
    431483, 
    431496, 
    431059, 
    431075, 
    431391, 
    431478, 
    431282, 
    431436, 
    431265, 
    431435, 
    431489, 
    431232, 
    431477, 
    430728, 
    431449, 
    431214, 
    431448, 
    430986, 
    431379, 
    431331, 
    431375, 
    431221, 
    431323, 
    431492, 
    431388, 
    431475, 
    431261, 
    431437, 
    431485, 
    431499, 
    431476, 
    431258, 
    431374, 
    431266, 
    431462, 
    430709, 
    431347, 
    431441, 
    431366, 
    431363, 
    431320, 
    430774, 
    431179, 
    431307, 
    431404, 
    431257, 
    431348, 
    430779, 
    431228, 
    430710, 
    431277, 
    431317, 
    430668, 
    431473, 
    431321, 
    430670, 
    431206, 
    431389, 
    431418, 
    431209, 
    431293, 
    431445, 
    431486, 
    431400, 
    431491, 
    431390, 
    431070, 
    431256, 
    431443, 
    431433, 
    431397, 
    430775, 
    430783, 
    431370, 
    431432, 
    431490, 
    431286, 
    431306, 
    431268, 
    431493, 
    431386, 
    431398, 
    431444, 
    430967, 
    431305, 
    430763, 
    431254, 
    431255, 
    431484, 
    430776, 
    431399, 
    431216, 
    430784, 
    431016, 
    431173, 
    431318, 
    431417, 
    431371, 
    431416, 
    431319, 
    431372, 
    430729, 
    431272, 
    431461, 
    431373, 
    431271, 
    431267, 
    431269, 
    431270
  ) 
GROUP BY 
  p.product_id

Query time 0.00461

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "7.29"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "0.10"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "dp",
            "access_type": "index",
            "possible_keys": [
              "thread_id",
              "thread_id_2"
            ],
            "key": "thread_id_2",
            "used_key_parts": [
              "thread_id",
              "status"
            ],
            "key_length": "6",
            "rows_examined_per_scan": 19,
            "rows_produced_per_join": 1,
            "filtered": "10.00",
            "using_index": true,
            "cost_info": {
              "read_cost": "4.42",
              "eval_cost": "0.38",
              "prefix_cost": "4.80",
              "data_read_per_join": "851"
            },
            "used_columns": [
              "post_id",
              "thread_id",
              "status"
            ],
            "attached_condition": "(`webmarco`.`dp`.`status` = 'A')"
          }
        },
        {
          "table": {
            "table_name": "cscart_discussion",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "object_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "thread_id"
            ],
            "key_length": "3",
            "ref": [
              "webmarco.dp.thread_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 0,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "1.90",
              "eval_cost": "0.02",
              "prefix_cost": "7.08",
              "data_read_per_join": "2"
            },
            "used_columns": [
              "thread_id",
              "object_id",
              "object_type"
            ],
            "attached_condition": "((`webmarco`.`cscart_discussion`.`object_id` in (431190,431092,431106,431147,430849,431162,431128,431099,431132,431202,430637,430639,431096,431168,431141,431153,431137,431155,431033,431130,430911,430640,431134,431125,431154,431090,431146,431065,431067,431187,431048,431094,430697,430767,431050,431104,430745,431124,431117,431191,431157,431160,430681,431116,430743,430850,430908,431084,431107,430998,431026,430655,430744,431100,431115,430915,430916,430636,430924,431195,430747,430803,431176,431203,430748,431204,431133,430648,431196,430823,431158,431199,431122,430903,430848,431139,430831,431150,430665,430696,431145,431102,430771,430907,430649,430646,430647,430912,430935,430638,430766,431097,431151,430821,430824,431359,430857,431201,430746,431068,430813,430925,431085,431103,431163,431164,430817,430654,430902,431313,431053,431148,430751,431044,431120,431456,430660,430825,430678,430680,430959,431013,431127,430691,430756,430820,431109,431161,430737,431035,431041,431194,430875,431049,430819,431083,430808,430989,431080,430635,430650,430629,430860,431126,430657,430807,430921,430922,430843,430812,430868,431073,431144,430789,430795,430758,431082,430818,430901,430928,430950,431156,430658,430673,431110,430684,431066,431114,430689,430822,431118,431064,430769,430892,431043,431169,430628,430664,430842,430834,430859,430906,431459,430768,430890,430988,431183,431250,430703,430838,430962,431008,430909,431063,431159,430695,430904,430923,430815,430832,430914,431356,430891,430939,431197,430880,430930,431089,431136,430847,430893,431135,430854,430913,430662,430676,430980,431078,430787,431142,431226,431393,430672,430760,430806,431174,430633,430829,430869,431031,431119,430753,431325,431327,430712,430976,431111,430644,430796,430844,431123,430772,431023,431062,431088,430663,430884,430899,430981,431238,431245,431251,430801,430845,430876,430958,431467,430898,431032,431093,431167,431121,430698,430851,430927,431314,431291,431423,431501,430651,430730,430883,431029,431072,431131,431220,430653,430809,430910,431046,431205,430685,430687,430722,430866,431042,431140,431198,431333,430631,430896,430900,431185,431463,430932,430937,430947,430994,430999,431077,431345,430634,430645,430714,430862,430885,430887,430992,431338,430656,430738,430761,430865,431054,431316,430626,430870,431192,430708,430797,430853,431019,431326,431365,430677,430749,431061,430802,430846,430873,430881,431011,431024,431242,430960,430993,431039,431322,430836,430936,431003,431052,431395,431429,430704,430707,430905,430841,430969,431020,431022,431091,431248,431264,430810,431051,431315,431503,430652,430861,430940,430984,431113,431178,431193,431247,431487,431006,431200,431301,431439,430782,430837,431074,431339,431426,430816,430926,431252,430641,430794,430968,431275,430788,430879,431171,431334,430661,430811,431098,431055,430742,430839,430675,430800,430805,430855,430863,430953,430979,431428,430778,431005,431244,430688,430852,430874,430894,431262,431336,430713,430755,430826,430827,430878,430917,430931,430952,430995,431000,431027,431028,431101,431368,431504,430856,430991,431177,431184,431451,430780,430897,431234,431342,431358,431469,430864,430694,431357,431420,430671,430840,430871,430918,430977,430666,430702,430888,431010,431344,430667,430706,430972,430990,430982,431025,431037,431129,431289,431297,430692,430895,430997,431007,431165,431308,431381,430933,431295,430723,430804,430946,430674,430679,430720,431237,431047,431149,431280,431288,431354,431355,431376,431424,431470,430726,431087,431138,431457,430627,430886,430920,431112,431392,430949,431218,431350,430718,430835,431021,431034,431056,431108,431246,431309,430828,430974,431440,431494,430867,430872,430889,431152,431505,430683,430741,431012,431340,431411,430690,430793,430877,431045,431211,430739,431425,431465,430715,430799,431329,431403,430961,431240,431284,431341,430770,430975,431360,431464,430735,430791,430858,431030,431239,431343,431349,430643,430948,431038,431219,431406,431474,430734,430934,431186,431328,430762,430764,430790,430830,430740,431283,431500,430732,430765,430786,430882,430919,431303,431353,431410,430686,430701,430711,431182,431230,431243,431279,431498,431060,431405,431497,430792,431231,431274,431495,431076,431175,431249,431422,431290,431311,431337,431419,430659,430957,430965,430983,431434,430642,430954,431166,431260,431380,431409,431004,431105,431304,431346,431480,430963,431287,430781,430964,430966,431002,431071,431458,430754,430759,430814,431015,431324,430798,431079,431217,431352,431408,431452,431453,430669,430750,430944,431273,431362,431407,431481,431188,431222,431394,430700,430705,430727,430938,431001,430978,430985,431009,431057,431208,431241,431335,431447,430699,430752,430757,430773,430942,430973,431210,431383,431502,431292,431427,431488,430719,431017,431236,431330,431040,431466,430630,430929,430943,431227,431235,430632,430693,431263,431482,430955,431018,430971,431069,431364,431378,431415,431302,431361,431421,430717,430736,431281,431385,431285,430725,431223,431086,431212,431369,431402,430785,430833,430987,431300,430724,430941,431377,431384,430733,431081,431181,431229,431472,430956,431253,431454,430721,431387,431225,431431,430682,430951,431438,431450,430716,431382,431446,431468,431401,431412,431430,430945,431143,431215,431058,431170,431294,431312,431367,431460,430731,431036,431224,431259,431276,431310,430777,431332,431396,431298,431414,431014,431172,431207,431296,431471,431479,431180,431233,431442,431189,431413,431455,431213,431278,431351,430970,430996,431095,431299,431483,431496,431059,431075,431391,431478,431282,431436,431265,431435,431489,431232,431477,430728,431449,431214,431448,430986,431379,431331,431375,431221,431323,431492,431388,431475,431261,431437,431485,431499,431476,431258,431374,431266,431462,430709,431347,431441,431366,431363,431320,430774,431179,431307,431404,431257,431348,430779,431228,430710,431277,431317,430668,431473,431321,430670,431206,431389,431418,431209,431293,431445,431486,431400,431491,431390,431070,431256,431443,431433,431397,430775,430783,431370,431432,431490,431286,431306,431268,431493,431386,431398,431444,430967,431305,430763,431254,431255,431484,430776,431399,431216,430784,431016,431173,431318,431417,431371,431416,431319,431372,430729,431272,431461,431373,431271,431267,431269,431270)) and (`webmarco`.`cscart_discussion`.`object_type` = 'P'))"
          }
        },
        {
          "table": {
            "table_name": "p",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "age_verification",
              "status",
              "idx_parent_product_id",
              "idx_master_product_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "ref": [
              "webmarco.cscart_discussion.object_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 0,
            "filtered": "100.00",
            "using_index": true,
            "cost_info": {
              "read_cost": "0.10",
              "eval_cost": "0.02",
              "prefix_cost": "7.19",
              "data_read_per_join": "760"
            },
            "used_columns": [
              "product_id"
            ]
          }
        }
      ]
    }
  }
}