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 = 512 
WHERE 
  cscart_products_categories.product_id IN (
    12498, 15432, 3317, 3314, 3319, 3316, 
    3320, 3318, 3315, 3321, 3322, 3298, 
    3299, 3329, 3330, 3306, 3305, 3304, 
    3325, 3289, 3287, 3283, 3284, 3285, 
    3286, 3292, 3291, 3300, 3301, 3302, 
    3326, 3295, 3294, 3327, 3297, 3307, 
    3309, 3308, 3293, 3288, 3290, 3303, 
    3310, 3324, 3323, 13136, 3922, 15904
  ) 
GROUP BY 
  cscart_products_categories.product_id

Query time 0.00172

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "68.36"
    },
    "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": 49,
            "rows_produced_per_join": 49,
            "filtered": "100.00",
            "index_condition": "(`dev_yourmart_co_bw`.`cscart_products_categories`.`product_id` in (12498,15432,3317,3314,3319,3316,3320,3318,3315,3321,3322,3298,3299,3329,3330,3306,3305,3304,3325,3289,3287,3283,3284,3285,3286,3292,3291,3300,3301,3302,3326,3295,3294,3327,3297,3307,3309,3308,3293,3288,3290,3303,3310,3324,3323,13136,3922,15904))",
            "cost_info": {
              "read_cost": "29.16",
              "eval_cost": "4.90",
              "prefix_cost": "34.06",
              "data_read_per_join": "784"
            },
            "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",
              "dev_yourmart_co_bw.cscart_products_categories.product_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 49,
            "filtered": "100.00",
            "cost_info": {
              "read_cost": "12.25",
              "eval_cost": "4.90",
              "prefix_cost": "51.21",
              "data_read_per_join": "784"
            },
            "used_columns": [
              "product_id",
              "category_id",
              "position"
            ]
          }
        },
        {
          "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": [
              "dev_yourmart_co_bw.cscart_products_categories.category_id"
            ],
            "rows_examined_per_scan": 1,
            "rows_produced_per_join": 2,
            "filtered": "5.00",
            "cost_info": {
              "read_cost": "12.25",
              "eval_cost": "0.25",
              "prefix_cost": "68.36",
              "data_read_per_join": "6K"
            },
            "used_columns": [
              "category_id",
              "usergroup_ids",
              "status",
              "storefront_id"
            ],
            "attached_condition": "((`dev_yourmart_co_bw`.`cscart_categories`.`storefront_id` in (0,1)) and ((`dev_yourmart_co_bw`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`dev_yourmart_co_bw`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`dev_yourmart_co_bw`.`cscart_categories`.`usergroup_ids`))) and (`dev_yourmart_co_bw`.`cscart_categories`.`status` in ('A','H')))"
          }
        }
      ]
    }
  }
}

Result

product_id category_ids position
3283 329M
3284 329M
3285 329M
3286 329M
3287 329M
3288 329M
3289 329M
3290 329M
3291 329M
3292 329M
3293 329M
3294 329M
3295 329M
3297 329M
3298 329M
3299 329M
3300 329M
3301 329M
3302 329M
3303 329M
3304 329M
3305 329M
3306 329M
3307 329M
3308 329M
3309 329M
3310 329M
3314 329M
3315 329M
3316 329M
3317 329M
3318 329M
3319 329M
3320 329M
3321 329M
3322 329M
3323 329M
3324 329M
3325 329M
3326 329M
3327 329M
3329 329M
3330 329M
3922 352M
12498 329M
13136 329M
15432 380M
15904 329M