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, 
  product_position_source.position AS position 
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') 
  LEFT JOIN cscart_products_categories AS product_position_source ON cscart_products_categories.product_id = product_position_source.product_id 
  AND product_position_source.category_id = 266 
WHERE 
  cscart_products_categories.product_id IN (
    4040, 4041, 4117, 4118, 4119, 4120, 4121, 
    4122, 4123, 4171, 4172, 4173, 4174, 
    4175, 4176, 50, 51, 52, 53, 54, 55, 56, 
    97, 98, 99, 100, 101, 102, 103, 182, 183, 
    184, 185, 186, 187, 188, 189, 190, 191, 
    192, 193, 194, 195, 226, 227, 228, 229, 
    230, 231, 232, 264, 265, 266, 267, 268, 
    269, 270, 318, 319, 320, 321, 322, 323, 
    3961, 3962, 3963, 3964, 3965, 3966, 
    3967, 4006, 4007, 4008, 4009, 4010, 
    4011, 4012, 4066, 4067, 4068, 4069, 
    4070, 4071, 4072, 4073, 4074, 4075, 
    4076, 4077, 4078, 4079, 4110, 4111, 
    4112, 4113, 4114
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00281

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "61.18"
    },
    "grouping_operation": {
      "using_temporary_table": true,
      "using_filesort": true,
      "cost_info": {
        "sort_cost": "1.78"
      },
      "nested_loop": [
        {
          "table": {
            "table_name": "cscart_categories",
            "access_type": "ALL",
            "possible_keys": [
              "PRIMARY",
              "c_status",
              "p_category_id"
            ],
            "rows_examined_per_scan": 46,
            "rows_produced_per_join": 1,
            "filtered": "4.00",
            "cost_info": {
              "read_cost": "5.15",
              "eval_cost": "0.18",
              "prefix_cost": "5.33",
              "data_read_per_join": "7K"
            },
            "used_columns": [
              "category_id",
              "storefront_id",
              "usergroup_ids",
              "status"
            ],
            "attached_condition": "((`test2_gaseus`.`cscart_categories`.`storefront_id` in (0,1)) and ((`test2_gaseus`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`test2_gaseus`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`test2_gaseus`.`cscart_categories`.`usergroup_ids`))) and (`test2_gaseus`.`cscart_categories`.`status` in ('A','H')))"
          }
        },
        {
          "table": {
            "table_name": "cscart_products_categories",
            "access_type": "ref",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id"
            ],
            "key_length": "3",
            "ref": [
              "test2_gaseus.cscart_categories.category_id"
            ],
            "rows_examined_per_scan": 83,
            "rows_produced_per_join": 1,
            "filtered": "1.16",
            "index_condition": "(`test2_gaseus`.`cscart_products_categories`.`product_id` in (4040,4041,4117,4118,4119,4120,4121,4122,4123,4171,4172,4173,4174,4175,4176,50,51,52,53,54,55,56,97,98,99,100,101,102,103,182,183,184,185,186,187,188,189,190,191,192,193,194,195,226,227,228,229,230,231,232,264,265,266,267,268,269,270,318,319,320,321,322,323,3961,3962,3963,3964,3965,3966,3967,4006,4007,4008,4009,4010,4011,4012,4066,4067,4068,4069,4070,4071,4072,4073,4074,4075,4076,4077,4078,4079,4110,4111,4112,4113,4114))",
            "cost_info": {
              "read_cost": "38.18",
              "eval_cost": "0.18",
              "prefix_cost": "58.78",
              "data_read_per_join": "28"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "link_type"
            ]
          }
        },
        {
          "table": {
            "table_name": "product_position_source",
            "access_type": "eq_ref",
            "possible_keys": [
              "PRIMARY",
              "pt"
            ],
            "key": "PRIMARY",
            "used_key_parts": [
              "category_id",
              "product_id"
            ],
            "key_length": "6",
            "ref": [
              "const",
              "test2_gaseus.cscart_products_categories.product_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 1,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "0.44",
              "eval_cost": "0.18",
              "prefix_cost": "59.40",
              "data_read_per_join": "28"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        }
      ]
    }
  }
}

Result

product_id category_ids position
50 273M
51 273M
52 273M
53 273M
54 273M
55 273M
56 273M
97 273M
98 273M
99 273M
100 273M
101 273M
102 273M
103 273M
182 273M
183 273M
184 273M
185 273M
186 273M
187 273M
188 273M
189 273M
190 273M
191 273M
192 273M
193 273M
194 273M
195 273M
226 273M
227 273M
228 273M
229 273M
230 273M
231 273M
232 273M
264 273M
265 273M
266 273M
267 273M
268 273M
269 273M
270 273M
318 273M
319 273M
320 273M
321 273M
322 273M
323 273M
3961 273M
3962 273M
3963 273M
3964 273M
3965 273M
3966 273M
3967 273M
4006 273M
4007 273M
4008 273M
4009 273M
4010 273M
4011 273M
4012 273M
4040 273M
4041 273M
4066 273M
4067 273M
4068 273M
4069 273M
4070 273M
4071 273M
4072 273M
4073 273M
4074 273M
4075 273M
4076 273M
4077 273M
4078 273M
4079 273M
4110 273M
4111 273M
4112 273M
4113 273M
4114 273M
4117 273M
4118 273M
4119 273M
4120 273M
4121 273M
4122 273M
4123 273M
4171 273M
4172 273M
4173 273M
4174 273M
4175 273M
4176 273M