SELECT 
  products_categories.product_id, 
  GROUP_CONCAT(
    IF(
      products_categories.link_type = "M", 
      CONCAT(
        products_categories.category_id, 
        "M"
      ), 
      products_categories.category_id
    )
  ) AS category_ids 
FROM 
  products_categories 
  INNER JOIN categories ON categories.category_id = products_categories.category_id 
  AND categories.storefront_id IN (0, 1) 
  AND (
    categories.usergroup_ids = '' 
    OR FIND_IN_SET(0, categories.usergroup_ids) 
    OR FIND_IN_SET(1, categories.usergroup_ids)
  ) 
  AND categories.status IN ('A', 'H') 
WHERE 
  products_categories.product_id IN (
    138903, 171934, 139416, 139364, 139389, 
    139360, 138902, 139368, 173441, 139376, 
    139318, 139415, 139314, 139315, 139377, 
    149277, 139381, 139366, 139363, 139387, 
    139379, 139418, 139321, 139361, 139419, 
    173429, 149275, 139380, 139355, 139358, 
    139372, 139383, 139390, 138901, 173431, 
    173432, 149249, 173426, 138899, 173436, 
    149261, 149274, 139370, 173443, 139367, 
    173438, 139385, 149278
  ) 
GROUP BY 
  products_categories.product_id

Query time 0.00213

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "52.02"
    },
    "grouping_operation": {
      "using_filesort": false,
      "nested_loop": [
        {
          "table": {
            "table_name": "products_categories",
            "access_type": "range",
            "possible_keys": [
              "PRIMARY",
              "link_type",
              "pt"
            ],
            "key": "pt",
            "used_key_parts": [
              "product_id"
            ],
            "key_length": "3",
            "rows_examined_per_scan": 94,
            "rows_produced_per_join": 94,
            "filtered": "100.00",
            "using_index": true,
            "cost_info": {
              "read_cost": "9.72",
              "eval_cost": "9.40",
              "prefix_cost": "19.12",
              "data_read_per_join": "1K"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ],
            "attached_condition": "(`goimagine`.`products_categories`.`product_id` in (138903,171934,139416,139364,139389,139360,138902,139368,173441,139376,139318,139415,139314,139315,139377,149277,139381,139366,139363,139387,139379,139418,139321,139361,139419,173429,149275,139380,139355,139358,139372,139383,139390,138901,173431,173432,149249,173426,138899,173436,149261,149274,139370,173443,139367,173438,139385,149278))"
          }
        },
        {
          "table": {
            "table_name": "categories",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "goimagine.products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 4,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "23.50",
              "eval_cost": "0.47",
              "prefix_cost": "52.02",
              "data_read_per_join": "12K"
            },
            "used_columns": [
              "category_id",
              "usergroup_ids",
              "status",
              "storefront_id"
            ],
            "attached_condition": "((`goimagine`.`categories`.`storefront_id` in (0,1)) and ((`goimagine`.`categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`goimagine`.`categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`goimagine`.`categories`.`usergroup_ids`))) and (`goimagine`.`categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids
138899 8395,87M
138901 8395,87M
138902 8395,87M
138903 8395,87M
139314 8456,183M
139315 8456,183M
139318 8456,183M
139321 8456,183M
139355 8456,183M
139358 8456,183M
139360 8456,183M
139361 8456,183M
139363 8456,183M
139364 8456,183M
139366 8456,183M
139367 8456,183M
139368 8456,183M
139370 8456,183M
139372 8456,183M
139376 8456,183M
139377 8456,183M
139379 8456,183M
139380 8456,183M
139381 8456,183M
139383 8456,183M
139385 8456,183M
139387 8456,183M
139389 183M
139390 8456,183M
139415 8456,183M
139416 8456,183M
139418 8456,183M
139419 8456,183M
149249 8395,87M
149261 11584,183M
149274 11584,183M
149275 11584,183M
149277 183M
149278 11584,183M
171934 11462,6534M
173426 11583,183M
173429 11583,183M
173431 11583,183M
173432 11583,183M
173436 11583,183M
173438 11583,183M
173441 11583,183M
173443 11583,183M