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 (
    350398, 
    350316, 
    355400, 
    355448, 
    350217, 
    350261, 
    350356, 
    350219, 
    350578, 
    350251, 
    355391, 
    350477, 
    356841, 
    350526, 
    350585, 
    350202, 
    350542, 
    350537, 
    350208, 
    350484, 
    350212, 
    353704, 
    350323, 
    350539, 
    350211, 
    350553, 
    353714, 
    350510, 
    350416, 
    350204, 
    350459, 
    350213, 
    350207, 
    350455, 
    350222, 
    350225, 
    350198, 
    350392, 
    355393, 
    350505, 
    350580, 
    350518, 
    350528, 
    355591, 
    353706, 
    350584, 
    350320, 
    350406, 
    350214, 
    355452, 
    350206, 
    350572, 
    350479, 
    350209, 
    350582, 
    350329, 
    353699, 
    350544, 
    350327, 
    350257, 
    350574, 
    350460, 
    350300, 
    350530, 
    350227, 
    350499, 
    353720, 
    350483, 
    350254, 
    350451, 
    355428, 
    350389, 
    350593, 
    350331, 
    354317, 
    350240, 
    353700, 
    350318, 
    355622, 
    353710, 
    350475, 
    350569, 
    350419, 
    350314, 
    350203, 
    350523, 
    350394, 
    350270, 
    350561, 
    350330, 
    350472, 
    355443, 
    350514, 
    350502, 
    350332, 
    350429, 
    350235, 
    350423, 
    350567, 
    350321, 
    350243, 
    350541, 
    353814, 
    350260, 
    350445, 
    350552, 
    350566, 
    350525, 
    358352, 
    355435, 
    350232, 
    350205, 
    350533, 
    355450, 
    355404, 
    350588, 
    350267, 
    350333, 
    350457, 
    350592, 
    350264, 
    350249, 
    355430, 
    350464, 
    350571, 
    350436, 
    350319, 
    350476, 
    350564, 
    355387, 
    350262, 
    353703, 
    350324, 
    350383, 
    350466, 
    350349, 
    350409, 
    350453, 
    350473, 
    350201, 
    350522, 
    350591, 
    350535, 
    350515, 
    355590, 
    350555, 
    350343, 
    350556, 
    355451, 
    350517, 
    350490, 
    350413, 
    350563, 
    355427, 
    350547, 
    355413, 
    350536, 
    354316, 
    350395, 
    350242, 
    350545, 
    350568, 
    353811, 
    350355, 
    355426, 
    350216, 
    350524, 
    350576, 
    350551, 
    350557, 
    356843, 
    350359, 
    350301, 
    350474, 
    350424, 
    350246, 
    350570, 
    350546, 
    350587, 
    350441, 
    350438, 
    353715, 
    350446, 
    350447, 
    350374, 
    350387, 
    350238, 
    350296, 
    350559, 
    350218, 
    350496, 
    350437, 
    350486, 
    355623, 
    350269, 
    350481, 
    350532, 
    350573, 
    355434, 
    350221, 
    350339, 
    350360, 
    350575, 
    350315, 
    350540, 
    354279, 
    350562, 
    350230, 
    350199, 
    350558, 
    350256, 
    350276, 
    355592, 
    350554, 
    350508, 
    350538, 
    350285, 
    350548, 
    350520, 
    350295, 
    355402, 
    350529, 
    350263, 
    350272, 
    355433, 
    350334, 
    350506, 
    350402, 
    350495, 
    350458, 
    350560, 
    355453, 
    350487, 
    350224, 
    350266, 
    350220, 
    355440, 
    350223, 
    350498, 
    350543, 
    350467, 
    350480, 
    350501, 
    350290, 
    350425, 
    354318, 
    350241, 
    350489, 
    350521, 
    350550, 
    350259, 
    350507, 
    350231, 
    350284, 
    350452, 
    350299, 
    354324, 
    355454, 
    350215, 
    350357, 
    350255, 
    355431, 
    350228, 
    350273, 
    350289, 
    353721, 
    354741, 
    350253, 
    353722, 
    350549, 
    354321, 
    350293, 
    350469, 
    354739, 
    350247, 
    350371, 
    350428, 
    350577, 
    350364, 
    350277, 
    350396, 
    350244, 
    355419, 
    350401, 
    350463, 
    350470, 
    350298, 
    350348, 
    350430, 
    353713, 
    350407, 
    350581, 
    350274, 
    355399, 
    350390, 
    350239, 
    350397, 
    350440, 
    354319, 
    350443, 
    350497, 
    350236, 
    350595, 
    350271, 
    350363, 
    350485, 
    355407, 
    350234, 
    350340, 
    350427, 
    350594, 
    355621, 
    350265, 
    350338, 
    350344, 
    350527, 
    350304, 
    355423, 
    355432, 
    350478, 
    350509, 
    350322, 
    350492, 
    353718, 
    350233, 
    350488, 
    350386, 
    353705, 
    350491, 
    354740, 
    350494, 
    353709, 
    350336, 
    350350, 
    350432, 
    350325, 
    350367, 
    353698, 
    350503, 
    354320, 
    350291, 
    350309, 
    350471, 
    350449, 
    355403, 
    350303, 
    353103, 
    350590, 
    350297, 
    350248, 
    350280, 
    350512, 
    350312, 
    350565, 
    355437, 
    355394, 
    354313, 
    350278, 
    350326, 
    350534, 
    350372, 
    354315, 
    354280, 
    350444, 
    350391, 
    355444, 
    350404, 
    350431, 
    350405, 
    350493, 
    353101, 
    354314, 
    350433, 
    355436, 
    350275, 
    350258, 
    350307, 
    350586, 
    353102, 
    355449, 
    350245, 
    353107, 
    350200, 
    350531, 
    350250, 
    350408, 
    355447, 
    353707, 
    350422, 
    355401, 
    350414, 
    350366, 
    350426, 
    350210, 
    350287, 
    350482, 
    350415, 
    355424, 
    350516, 
    350400, 
    350450, 
    350279, 
    350418, 
    353711, 
    350294, 
    353106, 
    353719, 
    350465, 
    353104, 
    350439, 
    350229, 
    354281, 
    350328, 
    353708, 
    355441, 
    350399, 
    353717, 
    350353, 
    355445, 
    350281, 
    350461, 
    350358, 
    350305, 
    350370, 
    354742, 
    350237, 
    350369, 
    350434, 
    350292, 
    355398, 
    350252, 
    350286, 
    350288, 
    356844, 
    350308, 
    350341, 
    350454, 
    350500, 
    353724, 
    350365, 
    350376, 
    355442, 
    350317, 
    353725, 
    350302, 
    355624, 
    350354, 
    355397, 
    350351, 
    350511, 
    355422, 
    350579, 
    355405, 
    350381, 
    350420, 
    350306, 
    354322, 
    350347, 
    354323, 
    350342, 
    350368, 
    355618, 
    350283, 
    355620, 
    354738, 
    350435, 
    355619, 
    350504, 
    355446, 
    350226, 
    350335, 
    350442, 
    350448, 
    350468, 
    355421, 
    354526, 
    350345, 
    350456, 
    356842, 
    350385, 
    350513, 
    355389, 
    350282, 
    350519, 
    355408, 
    355412, 
    350417, 
    358345, 
    353702, 
    350337, 
    350412, 
    358351, 
    350362, 
    355406, 
    350462, 
    353716, 
    350583, 
    354523, 
    350375, 
    358343, 
    350352, 
    353726, 
    350361, 
    353701, 
    350268, 
    350377, 
    350410, 
    350411, 
    354743, 
    355429, 
    355425, 
    350346, 
    355418, 
    358333, 
    350313, 
    350373, 
    353712, 
    353723, 
    355415, 
    355417, 
    355438, 
    354524, 
    350379, 
    350380, 
    350388, 
    355396, 
    355409, 
    355439, 
    355420, 
    350382, 
    350403, 
    354525, 
    358348, 
    358349, 
    350378, 
    355395, 
    353105, 
    353813, 
    350589, 
    355411, 
    358350, 
    350393, 
    350311, 
    355390, 
    358353, 
    358347, 
    350384, 
    350310, 
    350421, 
    355410, 
    353812, 
    355388, 
    355414, 
    355416, 
    355392, 
    358346
  ) 
GROUP BY 
  p.product_id

Query time 0.00355

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 (350398,350316,355400,355448,350217,350261,350356,350219,350578,350251,355391,350477,356841,350526,350585,350202,350542,350537,350208,350484,350212,353704,350323,350539,350211,350553,353714,350510,350416,350204,350459,350213,350207,350455,350222,350225,350198,350392,355393,350505,350580,350518,350528,355591,353706,350584,350320,350406,350214,355452,350206,350572,350479,350209,350582,350329,353699,350544,350327,350257,350574,350460,350300,350530,350227,350499,353720,350483,350254,350451,355428,350389,350593,350331,354317,350240,353700,350318,355622,353710,350475,350569,350419,350314,350203,350523,350394,350270,350561,350330,350472,355443,350514,350502,350332,350429,350235,350423,350567,350321,350243,350541,353814,350260,350445,350552,350566,350525,358352,355435,350232,350205,350533,355450,355404,350588,350267,350333,350457,350592,350264,350249,355430,350464,350571,350436,350319,350476,350564,355387,350262,353703,350324,350383,350466,350349,350409,350453,350473,350201,350522,350591,350535,350515,355590,350555,350343,350556,355451,350517,350490,350413,350563,355427,350547,355413,350536,354316,350395,350242,350545,350568,353811,350355,355426,350216,350524,350576,350551,350557,356843,350359,350301,350474,350424,350246,350570,350546,350587,350441,350438,353715,350446,350447,350374,350387,350238,350296,350559,350218,350496,350437,350486,355623,350269,350481,350532,350573,355434,350221,350339,350360,350575,350315,350540,354279,350562,350230,350199,350558,350256,350276,355592,350554,350508,350538,350285,350548,350520,350295,355402,350529,350263,350272,355433,350334,350506,350402,350495,350458,350560,355453,350487,350224,350266,350220,355440,350223,350498,350543,350467,350480,350501,350290,350425,354318,350241,350489,350521,350550,350259,350507,350231,350284,350452,350299,354324,355454,350215,350357,350255,355431,350228,350273,350289,353721,354741,350253,353722,350549,354321,350293,350469,354739,350247,350371,350428,350577,350364,350277,350396,350244,355419,350401,350463,350470,350298,350348,350430,353713,350407,350581,350274,355399,350390,350239,350397,350440,354319,350443,350497,350236,350595,350271,350363,350485,355407,350234,350340,350427,350594,355621,350265,350338,350344,350527,350304,355423,355432,350478,350509,350322,350492,353718,350233,350488,350386,353705,350491,354740,350494,353709,350336,350350,350432,350325,350367,353698,350503,354320,350291,350309,350471,350449,355403,350303,353103,350590,350297,350248,350280,350512,350312,350565,355437,355394,354313,350278,350326,350534,350372,354315,354280,350444,350391,355444,350404,350431,350405,350493,353101,354314,350433,355436,350275,350258,350307,350586,353102,355449,350245,353107,350200,350531,350250,350408,355447,353707,350422,355401,350414,350366,350426,350210,350287,350482,350415,355424,350516,350400,350450,350279,350418,353711,350294,353106,353719,350465,353104,350439,350229,354281,350328,353708,355441,350399,353717,350353,355445,350281,350461,350358,350305,350370,354742,350237,350369,350434,350292,355398,350252,350286,350288,356844,350308,350341,350454,350500,353724,350365,350376,355442,350317,353725,350302,355624,350354,355397,350351,350511,355422,350579,355405,350381,350420,350306,354322,350347,354323,350342,350368,355618,350283,355620,354738,350435,355619,350504,355446,350226,350335,350442,350448,350468,355421,354526,350345,350456,356842,350385,350513,355389,350282,350519,355408,355412,350417,358345,353702,350337,350412,358351,350362,355406,350462,353716,350583,354523,350375,358343,350352,353726,350361,353701,350268,350377,350410,350411,354743,355429,355425,350346,355418,358333,350313,350373,353712,353723,355415,355417,355438,354524,350379,350380,350388,355396,355409,355439,355420,350382,350403,354525,358348,358349,350378,355395,353105,353813,350589,355411,358350,350393,350311,355390,358353,358347,350384,350310,350421,355410,353812,355388,355414,355416,355392,358346)) 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"
            ]
          }
        }
      ]
    }
  }
}