SELECT 
  SQL_CALC_FOUND_ROWS products.product_id, 
  IF(
    shared_descr.product_id IS NOT NULL, 
    shared_descr.product, descr1.product
  ) as product, 
  companies.company as company_name, 
  variation_group_products.group_id AS variation_group_id, 
  products.product_type, 
  products.parent_product_id, 
  1 as instock_marker, 
  IF(
    products.product_type = 'C', 
    (
      SELECT 
        MAX(amount) 
      FROM 
        cscart_products 
      WHERE 
        parent_product_id = products.product_id
    ) > 0, 
    IF(
      products.amount > 0, 
      1, 
      IF(products.tracking = 'O', 1, 0)
    )
  ) as instock_sorting 
FROM 
  cscart_products as products 
  LEFT JOIN cscart_product_descriptions as descr1 ON descr1.product_id = products.product_id 
  AND descr1.lang_code = 'uk' 
  LEFT JOIN cscart_product_prices as prices ON prices.product_id = products.product_id 
  AND prices.lower_limit = 1 
  LEFT JOIN cscart_companies AS companies ON companies.company_id = products.company_id 
  INNER JOIN cscart_products_categories as products_categories ON products_categories.product_id = products.product_id 
  INNER JOIN cscart_categories ON cscart_categories.category_id = products_categories.category_id 
  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') 
  AND cscart_categories.storefront_id IN (0, 1) 
  LEFT JOIN cscart_ult_product_descriptions shared_descr ON shared_descr.product_id = products.product_id 
  AND shared_descr.company_id = 1 
  AND shared_descr.lang_code = 'uk' 
  LEFT JOIN cscart_images_links as img_links ON img_links.object_id = products.product_id 
  AND img_links.object_type = 'product' 
  AND img_links.type = 'M' 
  INNER JOIN cscart_product_variation_group_products AS variation_group_products ON variation_group_products.product_id = products.product_id 
WHERE 
  1 
  AND companies.status IN ('A') 
  AND (
    products.usergroup_ids = '' 
    OR FIND_IN_SET(0, products.usergroup_ids) 
    OR FIND_IN_SET(1, products.usergroup_ids)
  ) 
  AND products.status IN ('A') 
  AND prices.usergroup_id IN (0, 0, 1) 
  AND img_links.pair_id > 0 
  AND variation_group_products.group_id IN (835) 
GROUP BY 
  products.product_id 
ORDER BY 
  instock_sorting DESC, 
  product asc, 
  products.product_id ASC 
LIMIT 
  0, 100

Query time 0.00644

JSON explain

{
  "query_block": {
    "select_id": 1,
    "filesort": {
      "sort_key": "if(products.product_type = 'C',(subquery#2) > 0,if(products.amount > 0,1,if(products.tracking = 'O',1,0))) desc, if(shared_descr.product_id is not null,shared_descr.product,descr1.product), products.product_id",
      "temporary_table": {
        "nested_loop": [
          {
            "table": {
              "table_name": "companies",
              "access_type": "system",
              "possible_keys": ["PRIMARY"],
              "rows": 1,
              "filtered": 100
            }
          },
          {
            "table": {
              "table_name": "variation_group_products",
              "access_type": "ref",
              "possible_keys": ["PRIMARY", "idx_group_id"],
              "key": "idx_group_id",
              "key_length": "3",
              "used_key_parts": ["group_id"],
              "ref": ["const"],
              "rows": 89,
              "filtered": 100,
              "attached_condition": "variation_group_products.group_id <=> 835"
            }
          },
          {
            "table": {
              "table_name": "products",
              "access_type": "eq_ref",
              "possible_keys": ["PRIMARY", "status"],
              "key": "PRIMARY",
              "key_length": "3",
              "used_key_parts": ["product_id"],
              "ref": ["lillyshop_lillyshop.variation_group_products.product_id"],
              "rows": 1,
              "filtered": 90.50141144,
              "attached_condition": "products.company_id = 1 and (products.usergroup_ids = '' or find_in_set(0,products.usergroup_ids) or find_in_set(1,products.usergroup_ids)) and products.`status` = 'A'"
            }
          },
          {
            "table": {
              "table_name": "shared_descr",
              "access_type": "eq_ref",
              "possible_keys": ["PRIMARY", "product_id", "company_id"],
              "key": "PRIMARY",
              "key_length": "13",
              "used_key_parts": ["product_id", "lang_code", "company_id"],
              "ref": [
                "lillyshop_lillyshop.variation_group_products.product_id",
                "const",
                "const"
              ],
              "rows": 1,
              "filtered": 100,
              "attached_condition": "trigcond(shared_descr.lang_code = 'uk')"
            }
          },
          {
            "table": {
              "table_name": "descr1",
              "access_type": "eq_ref",
              "possible_keys": ["PRIMARY", "product_id"],
              "key": "PRIMARY",
              "key_length": "9",
              "used_key_parts": ["product_id", "lang_code"],
              "ref": [
                "lillyshop_lillyshop.variation_group_products.product_id",
                "const"
              ],
              "rows": 1,
              "filtered": 100,
              "attached_condition": "trigcond(descr1.lang_code = 'uk')"
            }
          },
          {
            "table": {
              "table_name": "prices",
              "access_type": "ref",
              "possible_keys": [
                "usergroup",
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "key": "usergroup",
              "key_length": "3",
              "used_key_parts": ["product_id"],
              "ref": ["lillyshop_lillyshop.variation_group_products.product_id"],
              "rows": 1,
              "filtered": 99.99145508,
              "attached_condition": "prices.lower_limit = 1 and prices.usergroup_id in (0,0,1)",
              "using_index": true
            }
          },
          {
            "table": {
              "table_name": "products_categories",
              "access_type": "ref",
              "possible_keys": ["PRIMARY", "pt"],
              "key": "pt",
              "key_length": "3",
              "used_key_parts": ["product_id"],
              "ref": ["lillyshop_lillyshop.variation_group_products.product_id"],
              "rows": 1,
              "filtered": 100
            }
          },
          {
            "table": {
              "table_name": "cscart_categories",
              "access_type": "eq_ref",
              "possible_keys": ["PRIMARY", "c_status", "p_category_id"],
              "key": "PRIMARY",
              "key_length": "3",
              "used_key_parts": ["category_id"],
              "ref": ["lillyshop_lillyshop.products_categories.category_id"],
              "rows": 1,
              "filtered": 100,
              "attached_condition": "(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') and cscart_categories.storefront_id in (0,1)"
            }
          },
          {
            "table": {
              "table_name": "img_links",
              "access_type": "ref",
              "possible_keys": ["PRIMARY", "object_id"],
              "key": "object_id",
              "key_length": "81",
              "used_key_parts": ["object_id", "object_type", "type"],
              "ref": [
                "lillyshop_lillyshop.variation_group_products.product_id",
                "const",
                "const"
              ],
              "rows": 1,
              "filtered": 100,
              "index_condition": "img_links.object_id = variation_group_products.product_id and img_links.object_type = 'product' and img_links.`type` = 'M'",
              "attached_condition": "img_links.pair_id > 0"
            }
          }
        ],
        "subqueries": [
          {
            "expression_cache": {
              "state": "uninitialized",
              "query_block": {
                "select_id": 2,
                "nested_loop": [
                  {
                    "table": {
                      "table_name": "cscart_products",
                      "access_type": "ALL",
                      "possible_keys": ["idx_parent_product_id"],
                      "rows": 11707,
                      "filtered": 100,
                      "attached_condition": "cscart_products.parent_product_id = products.product_id"
                    }
                  }
                ]
              }
            }
          }
        ]
      }
    }
  }
}

Result

product_id product company_name variation_group_id product_type parent_product_id instock_marker instock_sorting
19860 База кольорова DNKA Cover base №001 Ambitious червона, 12 мл LILLYSHOP 835 P 0 1 1
19820 База кольорова DNKA Cover base №002 Gambling червона з мікроблиском, 12 мл LILLYSHOP 835 P 0 1 1
19863 База кольорова DNKA Cover base №003 Passionate насичено-червона, 12 мл LILLYSHOP 835 P 0 1 1
19865 База кольорова DNKA Cover base №004 Sexy червона, 12 мл LILLYSHOP 835 P 0 1 1
19866 База кольорова DNKA Cover base №005 Daring червона з блискітками, 12 мл LILLYSHOP 835 P 0 1 1
20154 База кольорова DNKA Cover base №005A Hot світловідбиваюча червона, 12 мл LILLYSHOP 835 P 0 1 1
19867 База кольорова DNKA Cover base №006 Rich бордова, 12 мл LILLYSHOP 835 P 0 1 1
19868 База кольорова DNKA Cover base №007 Powerful капучіно, 12 мл LILLYSHOP 835 P 0 1 1
19869 База кольорова DNKA Cover base №008 Magical бежева з голографічними блискітками, 12 мл LILLYSHOP 835 P 0 1 1
19870 База кольорова DNKA Cover base №009 Nice рожева з шиммером, 12 мл LILLYSHOP 835 P 0 1 1
19871 База кольорова DNKA Cover base №010 Wonderful рожева опалова, 12 мл LILLYSHOP 835 P 0 1 1
19872 База кольорова DNKA Cover base №010А Lovely рожева з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
19873 База кольорова DNKA Cover base №011 Famous молочна з золотою поталлю, 12 мл LILLYSHOP 835 P 0 1 1
19875 База кольорова DNKA Cover base №011B Beauttiful рожева з кольоровою поталлю, 12 мл LILLYSHOP 835 P 0 1 1
19874 База кольорова DNKA Cover base №011А Hapy молочна з кольоровою поталлю, 12 мл LILLYSHOP 835 P 0 1 1
19876 База кольорова DNKA Cover base №012 Shining бежева світловідбиваюча, 12 мл LILLYSHOP 835 P 0 1 1
20087 База кольорова DNKA Cover base №012A Confident світловідбиваюча вишнева, 12 мл LILLYSHOP 835 P 0 1 1
19877 База кольорова DNKA Cover base №013 Amazing сіра з срібним шиммером, 12 мл LILLYSHOP 835 P 0 1 1
19878 База кольорова DNKA Cover base №013А Сheerful чорна світловідбиваюча, 12 мл LILLYSHOP 835 P 0 1 1
19879 База кольорова DNKA Cover base №014 Brave синя, 12 мл LILLYSHOP 835 P 0 1 1
19880 База кольорова DNKA Cover base №015 Cosmic блакитна з шиммером, 12 мл LILLYSHOP 835 P 0 1 1
19881 База кольорова DNKA Cover base №016 Sincere блакитна, 12 мл LILLYSHOP 835 P 0 1 1
19882 База кольорова DNKA Cover base №017 Kind помаранчева, 12 мл LILLYSHOP 835 P 0 1 1
19883 База кольорова DNKA Cover base №018 Dreamy персикова, 12 мл LILLYSHOP 835 P 0 1 1
19884 База кольорова DNKA Cover base №019 Fresh м`ятна, 12 мл LILLYSHOP 835 P 0 1 1
19885 База кольорова DNKA Cover base №020 Intuitive бірюзова, 12 мл LILLYSHOP 835 P 0 1 1
19886 База кольорова DNKA Cover base №021 Sunny жовта, 12 мл LILLYSHOP 835 P 0 1 1
19888 База кольорова DNKA Cover base №023 Tender ніжно-лавандова, 12 мл LILLYSHOP 835 P 0 1 1
19889 База кольорова DNKA Cover base №024 Creative лавандова, 12 мл LILLYSHOP 835 P 0 1 1
19890 База кольорова DNKA Cover base №025 Pretty рожева, 12 мл LILLYSHOP 835 P 0 1 1
19891 База кольорова DNKA Cover base №026 Sweet світло-рожева, 12 мл LILLYSHOP 835 P 0 1 1
19892 База кольорова DNKA Cover base №027 Serious лілова, 12 мл LILLYSHOP 835 P 0 1 1
19893 База кольорова DNKA Cover base №028 Mysterious бежева з голографічними блискітками, 12 мл LILLYSHOP 835 P 0 1 1
19894 База кольорова DNKA Cover base №029 Naked нюдова, 12 мл LILLYSHOP 835 P 0 1 1
19895 База кольорова DNKA Cover base №030 Luxurious нюдова з шиммером, 12 мл LILLYSHOP 835 P 0 1 1
19896 База кольорова DNKA Cover base №031 Business тепла нюдова, 12 мл LILLYSHOP 835 P 0 1 1
19897 База кольорова DNKA Cover base №032 Soulful нюдово-сіра, 12 мл LILLYSHOP 835 P 0 1 1
19898 База кольорова DNKA Cover base №033 Esthetic нюдово-рожева, 12 мл LILLYSHOP 835 P 0 1 1
19899 База кольорова DNKA Cover base №034 Modest камуфлююча рожева, 12 мл LILLYSHOP 835 P 0 1 1
19900 База кольорова DNKA Cover base №035 Perfectionist камуфлююча рожева, 12 мл LILLYSHOP 835 P 0 1 1
19901 База кольорова DNKA Cover base №036 Elegant світло-нюдова, 12 мл LILLYSHOP 835 P 0 1 1
19902 База кольорова DNKA Cover base №037 Cute пудрово-рожева, 12 мл LILLYSHOP 835 P 0 1 1
19903 База кольорова DNKA Cover base №037А Supreme сіро-рожева з чорно-білими крихтами, 12 мл LILLYSHOP 835 P 0 1 1
19904 База кольорова DNKA Cover base №038 Flirty пастельно-рожева, 12 мл LILLYSHOP 835 P 0 1 1
19905 База кольорова DNKA Cover base №039 Sensual молочно-рожева, 12 мл LILLYSHOP 835 P 0 1 1
19925 База кольорова DNKA Cover base №039A Different рожева з чорно-білими крихтами, 12 мл LILLYSHOP 835 P 0 1 1
19906 База кольорова DNKA Cover base №040 Romantic світло-рожева з шиммером, 12 мл LILLYSHOP 835 P 0 1 1
19907 База кольорова DNKA Cover base №041 Stunning прозоро-бежева з голофрафічними блискітками, 12 мл LILLYSHOP 835 P 0 1 1
19908 База кольорова DNKA Cover base №042 Sparkling молочно-опалова, 12 мл LILLYSHOP 835 P 0 1 1
19909 База кольорова DNKA Cover base №043 Faithful біла, 12 мл LILLYSHOP 835 P 0 1 1
19910 База кольорова DNKA Cover base №044 Funny cвітло-сіра з кольоровою поталлю, 12 мл LILLYSHOP 835 P 0 1 1
19911 База кольорова DNKA Cover base №045 Star молочно-опалова, 12 мл LILLYSHOP 835 P 0 1 1
20576 База кольорова DNKA Cover base №046 Stilish сіра з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
20088 База кольорова DNKA Cover base №047 Trendy сіра з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
20089 База кольорова DNKA Cover base №048 Chic біла з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
20091 База кольорова DNKA Cover base №050 Fancy світловідбиваюча з рожевими крихтами, 12 мл LILLYSHOP 835 P 0 1 1
20092 База кольорова DNKA Cover base №051 Royal світловідбиваюча з жовтими крихтами, 12 мл LILLYSHOP 835 P 0 1 1
20093 База кольорова DNKA Cover base №052 Meteoric світловідбиваюча з бежевими крихтами, 12 мл LILLYSHOP 835 P 0 1 1
20094 База кольорова DNKA Cover base №053 Glowing світловідбиваюча з блакитними крихтами, 12 мл LILLYSHOP 835 P 0 1 1
18661 База кольорова DNKA Cover base №059 Smart молочна з крихтами, 12 мл LILLYSHOP 835 P 0 1 1
18662 База кольорова DNKA Cover base №060 Awesome блакитна з крихтами, 12 мл LILLYSHOP 835 P 0 1 1
19131 База кольорова DNKA Cover base №061 Confetti бежева з конфеті, 12 мл LILLYSHOP 835 P 0 1 1
18664 База кольорова DNKA Cover base №062 Mellow світло-рожева з конфеті, 12 мл LILLYSHOP 835 P 0 1 1
20724 База кольорова DNKA Cover base №066 Serenity лавандова з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 1
20725 База кольорова DNKA Cover base №067 Holiday жовта з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 1
20728 База кольорова DNKA Cover base №070 Lotus біла з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 1
20955 База кольорова DNKA Cover base №071 Saphire синя, 12 мл LILLYSHOP 835 P 0 1 1
20956 База кольорова DNKA Cover base №072 Crash жовта, 12 мл LILLYSHOP 835 P 0 1 1
20957 База кольорова DNKA Cover base №073 Flamingo малинова, 12 мл LILLYSHOP 835 P 0 1 1
20958 База кольорова DNKA Cover base №074 Muse насичено-малинова, 12 мл LILLYSHOP 835 P 0 1 1
20959 База кольорова DNKA Cover base №076 Aperol помаранчевий, 12 мл LILLYSHOP 835 P 0 1 1
22277 База кольорова DNKA Cover base №077 Campari неоново-червоний, 12 мл LILLYSHOP 835 P 0 1 1
20971 База кольорова DNKA Cover base №084 Euphoria світловідбиваюча фіолетова, 12 мл LILLYSHOP 835 P 0 1 1
21078 База кольорова DNKA Cover base №085 Glam світловідбиваючий малиновий, 12 мл LILLYSHOP 835 P 0 1 1
21102 База кольорова DNKA Cover base №086 Force світловідбиваюча теракотова, 12 мл LILLYSHOP 835 P 0 1 1
20972 База кольорова DNKA Cover base №087 Proud світловідбиваюча жовта, 12 мл LILLYSHOP 835 P 0 1 1
20973 База кольорова DNKA Cover base №088 Space світловідбиваюча синя, 12 мл LILLYSHOP 835 P 0 1 1
20974 База кольорова DNKA Cover base №089 Impuls світловідбиваюча бірюзова, 12 мл LILLYSHOP 835 P 0 1 1
21103 База кольорова DNKA Cover base №090 Wizard світловідбиваюча зелена, 12 мл LILLYSHOP 835 P 0 1 1
21923 База кольорова DNKA Cover base №093 Fine Work молочна з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
21924 База кольорова DNKA Cover base №094 Prosecco молочна з блискітками, 12 мл LILLYSHOP 835 P 0 1 1
21925 База кольорова DNKA Cover base №095 Coctail молочна з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
21926 База кольорова DNKA Cover base №096 Black Dress чорна з поталлю, 12 мл LILLYSHOP 835 P 0 1 1
20065 База кольорова DNKA Cover base №010B Ukrainian, 12 мл LILLYSHOP 835 P 0 1 0
19887 База кольорова DNKA Cover base №022 Naive лимонна, 12 мл LILLYSHOP 835 P 0 1 0
21749 База кольорова DNKA Cover base №035L Perfectionist рожева, 12 мл LILLYSHOP 835 P 0 1 0
20723 База кольорова DNKA Cover base №065 Kiss рожева з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 0
20726 База кольорова DNKA Cover base №068 Breeze блакитна з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 0
20727 База кольорова DNKA Cover base №069 Relax салатова з хлоп`ями, 12 мл LILLYSHOP 835 P 0 1 0