SELECT 
  p.product_id, 
  ifnull(
    count(dp.post_id), 
    0
  ) as discussion_amount_posts 
FROM 
  q_discussion 
  INNER JOIN q_products as p ON (
    q_discussion.object_id = p.product_id
  ) 
  INNER JOIN q_discussion_posts as dp ON (
    q_discussion.thread_id = dp.thread_id 
    AND q_discussion.object_type = 'P'
  ) 
WHERE 
  dp.status = 'A' 
  and p.product_id in (
    37513, 
    37514, 
    37515, 
    37516, 
    37520, 
    37521, 
    37522, 
    37523, 
    37524, 
    37525, 
    37526, 
    37527, 
    37528, 
    37529, 
    37530, 
    37531, 
    37532, 
    37533, 
    37534, 
    37535, 
    37536, 
    37537, 
    37538, 
    37539, 
    37540, 
    37541, 
    37542, 
    37543, 
    37544, 
    37545, 
    37546, 
    37547, 
    37548, 
    37549, 
    37550, 
    37551, 
    37552, 
    37553, 
    37554, 
    37555, 
    37557, 
    37558, 
    37559, 
    37560, 
    37561, 
    37562, 
    37563, 
    37820, 
    37821, 
    37822, 
    37823, 
    37824, 
    37825, 
    37826, 
    37827, 
    37828, 
    37829, 
    37830, 
    37831, 
    37832, 
    37833, 
    37834, 
    37835, 
    37836, 
    37837, 
    37838, 
    37839, 
    37840, 
    37841, 
    37842, 
    37843, 
    37844, 
    37845, 
    37846, 
    37847, 
    37848, 
    37849, 
    37850, 
    37851, 
    37852, 
    37853, 
    37854, 
    37855, 
    37856, 
    37858, 
    37859, 
    37860, 
    37861, 
    37862, 
    37863, 
    37864, 
    37865, 
    37866, 
    37867, 
    37868, 
    37869, 
    37870, 
    37871, 
    37872, 
    37873, 
    37874, 
    37875, 
    37877, 
    37878, 
    37879, 
    37880, 
    37881, 
    37882, 
    37883, 
    37884, 
    37885, 
    37886, 
    37887, 
    37888, 
    37889, 
    37890, 
    37891, 
    37892, 
    37893, 
    37894, 
    37895, 
    37896, 
    37897, 
    38150, 
    38151, 
    38152, 
    38155, 
    38156
  ) 
GROUP BY 
  p.product_id

Query time 0.00121

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "21.61"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "0.29"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "dp",
            "access_type": "ALL",
            "possible_keys": [
              "thread_id",
              "thread_id_2"
            ],
            "rows_examined_per_scan": 57,
            "rows_produced_per_join": 5,
            "filtered": "10.00",
            "cost_info": {
              "read_cost": "13.01",
              "eval_cost": "1.14",
              "prefix_cost": "14.15",
              "data_read_per_join": "2K"
            },
            "used_columns": [
              "post_id",
              "thread_id",
              "status"
            ],
            "attached_condition": "(`portal`.`dp`.`status` = 'A')"
          }
        },
        {
          "table": {
            "table_name": "q_discussion",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "object_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "thread_id"
            ],
            "key_length": "3",
            "ref": [
              "portal.dp.thread_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 0,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "5.70",
              "eval_cost": "0.06",
              "prefix_cost": "20.99",
              "data_read_per_join": "4"
            },
            "used_columns": [
              "thread_id",
              "object_id",
              "object_type"
            ],
            "attached_condition": "((`portal`.`q_discussion`.`object_id` in (37513,37514,37515,37516,37520,37521,37522,37523,37524,37525,37526,37527,37528,37529,37530,37531,37532,37533,37534,37535,37536,37537,37538,37539,37540,37541,37542,37543,37544,37545,37546,37547,37548,37549,37550,37551,37552,37553,37554,37555,37557,37558,37559,37560,37561,37562,37563,37820,37821,37822,37823,37824,37825,37826,37827,37828,37829,37830,37831,37832,37833,37834,37835,37836,37837,37838,37839,37840,37841,37842,37843,37844,37845,37846,37847,37848,37849,37850,37851,37852,37853,37854,37855,37856,37858,37859,37860,37861,37862,37863,37864,37865,37866,37867,37868,37869,37870,37871,37872,37873,37874,37875,37877,37878,37879,37880,37881,37882,37883,37884,37885,37886,37887,37888,37889,37890,37891,37892,37893,37894,37895,37896,37897,38150,38151,38152,38155,38156)) and (`portal`.`q_discussion`.`object_type` = 'P'))"
          }
        },
        {
          "table": {
            "table_name": "p",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "ref": [
              "portal.q_discussion.object_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 0,
            "filtered": "100.00",
            "using_index": true,
            "cost_info": {
              "read_cost": "0.29",
              "eval_cost": "0.06",
              "prefix_cost": "21.33",
              "data_read_per_join": "1K"
            },
            "used_columns": [
              "product_id"
            ]
          }
        }
      ]
    }
  }
}