SELECT 
  cscart_products_categories.product_id, 
  GROUP_CONCAT(
    IF(
      cscart_products_categories.link_type = "M", 
      CONCAT(
        cscart_products_categories.category_id, 
        "M"
      ), 
      cscart_products_categories.category_id
    )
  ) AS category_ids 
FROM 
  cscart_products_categories 
  INNER JOIN cscart_categories ON cscart_categories.category_id = cscart_products_categories.category_id 
  AND cscart_categories.storefront_id IN (0, 1) 
  AND (
    cscart_categories.usergroup_ids = '' 
    OR FIND_IN_SET(
      0, cscart_categories.usergroup_ids
    ) 
    OR FIND_IN_SET(
      1, cscart_categories.usergroup_ids
    )
  ) 
  AND cscart_categories.status IN ('A', 'H') 
WHERE 
  cscart_products_categories.product_id IN (
    2187, 
    2244, 
    2172, 
    2171, 
    1668, 
    8586, 
    8372, 
    2222, 
    2175, 
    2228, 
    2220, 
    2177, 
    2230, 
    2226, 
    1667, 
    2221, 
    2179, 
    1666, 
    8240, 
    2183, 
    8813, 
    2176, 
    8433, 
    2224, 
    2195, 
    2225, 
    8514, 
    2189, 
    2196, 
    2186, 
    2180, 
    2178, 
    2191, 
    2170, 
    2181, 
    2190, 
    2185, 
    2198, 
    2182, 
    2188, 
    2173, 
    8784, 
    2193, 
    2184, 
    2197, 
    2174, 
    2199, 
    2219, 
    2223, 
    2194, 
    2243, 
    2200, 
    2229, 
    8789, 
    8695, 
    8766, 
    8802, 
    8362, 
    8573, 
    8696, 
    8243, 
    8617, 
    8601, 
    8375, 
    8570, 
    8812, 
    8536, 
    8604, 
    8589, 
    8699, 
    8590, 
    8612, 
    8800, 
    8702, 
    8576, 
    8701, 
    8464, 
    8469, 
    8577, 
    8708, 
    8703, 
    8783, 
    8804, 
    8275, 
    8569, 
    8724, 
    8445, 
    8470, 
    8435, 
    8801, 
    8805, 
    8811, 
    8815, 
    8441, 
    8446, 
    8543, 
    8245, 
    8440, 
    8519, 
    8593, 
    8595, 
    8432, 
    8444, 
    8471, 
    8594, 
    8472, 
    8457, 
    8806, 
    8828, 
    8830, 
    8575, 
    8269, 
    8466, 
    8363, 
    8459, 
    8460, 
    8603, 
    8581, 
    8709, 
    8438, 
    8799, 
    8831, 
    8241, 
    8272, 
    8273, 
    8276, 
    8365, 
    8382, 
    8538, 
    8572, 
    8578, 
    8580, 
    8585, 
    8597, 
    8614, 
    8615, 
    8729, 
    8730, 
    8757, 
    8797, 
    8810, 
    8814, 
    8829, 
    8242, 
    8244, 
    8277, 
    8280, 
    8373, 
    8377, 
    8403, 
    8431, 
    8434, 
    8443, 
    8517, 
    8540, 
    8574, 
    8587, 
    8596, 
    8600, 
    8605
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00543

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "292.81"
    },
    "grouping_operation": {
      "using_filesort": false,
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_products_categories",
            "access_type": "range",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "pt",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "rows_examined_per_scan": 316,
            "rows_produced_per_join": 316,
            "filtered": "100.00",
            "index_condition": "(`softwarepirmam_hewadelivard_cscart_4`.`cscart_products_categories`.`product_id` in (2187,2244,2172,2171,1668,8586,8372,2222,2175,2228,2220,2177,2230,2226,1667,2221,2179,1666,8240,2183,8813,2176,8433,2224,2195,2225,8514,2189,2196,2186,2180,2178,2191,2170,2181,2190,2185,2198,2182,2188,2173,8784,2193,2184,2197,2174,2199,2219,2223,2194,2243,2200,2229,8789,8695,8766,8802,8362,8573,8696,8243,8617,8601,8375,8570,8812,8536,8604,8589,8699,8590,8612,8800,8702,8576,8701,8464,8469,8577,8708,8703,8783,8804,8275,8569,8724,8445,8470,8435,8801,8805,8811,8815,8441,8446,8543,8245,8440,8519,8593,8595,8432,8444,8471,8594,8472,8457,8806,8828,8830,8575,8269,8466,8363,8459,8460,8603,8581,8709,8438,8799,8831,8241,8272,8273,8276,8365,8382,8538,8572,8578,8580,8585,8597,8614,8615,8729,8730,8757,8797,8810,8814,8829,8242,8244,8277,8280,8373,8377,8403,8431,8434,8443,8517,8540,8574,8587,8596,8600,8605))",
            "cost_info": {
              "read_cost": "150.61",
              "eval_cost": "31.60",
              "prefix_cost": "182.21",
              "data_read_per_join": "4K"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "softwarepirmam_hewadelivard_cscart_4.cscart_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 15,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "79.00",
              "eval_cost": "1.58",
              "prefix_cost": "292.81",
              "data_read_per_join": "49K"
            },
            "used_columns": [
              "category_id",
              "storefront_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`storefront_id` in (0,1)) and ((`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`usergroup_ids`))) and (`softwarepirmam_hewadelivard_cscart_4`.`cscart_categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
1666 250,380M
1667 380,250M
1668 452,424,423,166,492M
2170 350,308,386,250,166,382M
2171 386,350,308,250,166,382M
2172 386,350,308,250,166,382M
2173 386,350,308,250,166,382M
2174 386,350,308,250,166,382M
2175 386,350,308,250,166,382M
2176 386,350,308,250,166,382M
2177 386,350,308,250,166,382M
2178 386,350,308,250,166,382M
2179 250,166,386,350,308,382M
2180 386,350,308,250,166,382M
2181 386,350,308,250,166,382M
2182 386,350,308,250,166,382M
2183 386,350,308,250,166,382M
2184 385,308,350,250,166,381M
2185 385,308,350,250,166,381M
2186 385,308,350,250,166,381M
2187 385,308,350,250,166,381M
2188 385,308,350,250,166,381M
2189 385,308,350,250,166,381M
2190 385,308,350,250,166,381M
2191 385,308,350,250,166,381M
2193 426,166,423M
2194 426,423,166M
2195 426,423,166M
2196 426,423,166M
2197 423,166,426M
2198 425,423,166M
2199 425,423,166M
2200 492,423,425M
2219 423,166,425M
2220 250,336M
2221 424,423,451,166M
2222 424,423,451,166M
2223 424,423,452,166M
2224 424,423,451,166M
2225 424,423,452,166M
2226 424,423,450,166M
2228 166,424,450,423M
2229 424,423,450,166M
2230 424,423,450,166M
2243 166,378,432M
2244 166,378,432M
8240 492,657M
8241 432M
8242 451M
8243 457,344M
8244 449M
8245 432M
8269 542M
8272 457M
8273 423,586M
8275 423M
8276 423,449M
8277 449M
8280 448M
8362 423,380M
8363 423,477M
8365 449M
8372 423,657M
8373 450M
8375 386,382M
8377 449M
8382 423M
8403 449M
8431 449M
8432 386,382M
8433 464M
8434 347M
8435 657M
8438 347M
8440 449M
8441 449M
8443 448M
8444 449M
8445 449M
8446 449M
8457 449M
8459 380,166,425M
8460 166,425M
8464 449M
8466 449M
8469 449M
8470 658M
8471 449M
8472 378,432M
8514 423,657M
8517 450M
8519 347M
8536 658M
8538 658M
8540 658M
8543 417M
8569 542,546,568,343M
8570 344M
8572 658M
8573 658M
8574 658M
8575 658M
8576 542,546,568,343M
8577 542,546,568,343M
8578 658M
8580 658M
8581 658M
8585 449M
8586 423,657M
8587 449M
8589 423M
8590 658M
8593 449M
8594 449M
8595 449M
8596 448M
8597 448M
8600 423M
8601 658M
8603 457M
8604 423M
8605 658M
8612 449M
8614 344M
8615 449M
8617 344M
8695 423M
8696 344M
8699 423M
8701 423M
8702 423M
8703 344M
8708 520M
8709 344M
8724 449M
8729 449M
8730 449M
8757 344M
8766 658M
8783 658M
8784 542,546,568,343M
8789 250,336M
8797 449M
8799 387,350,308,383M
8800 542,546,568,343M
8801 423,657M
8802 462M
8804 423M
8805 399M
8806 166,446M
8810 449M
8811 449M
8812 426,570,571,567M
8813 546,568,343M
8814 449M
8815 658M
8828 449M
8829 449M
8830 524M
8831 449M