SELECT 
  SQL_CALC_FOUND_ROWS products.product_id, 
  IF(
    shared_descr.product_id IS NOT NULL, 
    shared_descr.product, descr1.product
  ) as product, 
  stoc_intern, 
  pret_achizitie, 
  pret_revanzator, 
  gestiune 
FROM 
  cscart_products as products 
  LEFT JOIN cscart_product_options_inventory as inventory ON inventory.product_id = products.product_id 
  LEFT JOIN cscart_product_descriptions as descr1 ON descr1.product_id = products.product_id 
  AND descr1.lang_code = 'ro' 
  LEFT JOIN cscart_product_prices as prices ON prices.product_id = products.product_id 
  AND prices.lower_limit = 1 
  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') 
  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 = 'ro' 
  LEFT JOIN cscart_product_popularity as popularity ON popularity.product_id = products.product_id 
WHERE 
  1 
  AND cscart_categories.category_id IN (
    286, 
    14399, 
    36085, 
    39203, 
    40128, 
    40198, 
    16209, 
    16210, 
    29492, 
    36150, 
    16340, 
    40148, 
    40153, 
    40167, 
    41139, 
    16343, 
    40782, 
    40783, 
    40784, 
    40785, 
    40786, 
    40787, 
    16362, 
    16374, 
    16494, 
    29493, 
    36089, 
    36147, 
    36157, 
    41434, 
    30571, 
    30572, 
    30575, 
    30965, 
    30966, 
    36151, 
    36152, 
    36154, 
    40779, 
    40780, 
    40781, 
    32211, 
    35083, 
    35775, 
    35809, 
    36059, 
    36076, 
    36105, 
    40166, 
    40177, 
    40498, 
    40500, 
    40502, 
    40503, 
    40794, 
    40795, 
    40796, 
    40797, 
    40798, 
    36102, 
    36123, 
    39202, 
    39847, 
    40152, 
    40155, 
    40193, 
    40215, 
    41055, 
    41432, 
    42575, 
    42577, 
    42611, 
    42614, 
    16293, 
    16415, 
    29091, 
    30283, 
    30391, 
    30542, 
    30567, 
    40515, 
    40516, 
    40517, 
    40518, 
    40519, 
    40520, 
    40521, 
    36087, 
    40508, 
    40509, 
    40510, 
    40511, 
    40512, 
    40513, 
    40186, 
    40524, 
    40507, 
    40514, 
    40522, 
    40523, 
    28866, 
    295, 
    12291, 
    12326, 
    12347, 
    12377, 
    12450, 
    12517, 
    12526, 
    12531, 
    36061, 
    36065, 
    41415, 
    36099, 
    36103, 
    36104, 
    36125, 
    40792, 
    36127, 
    40143, 
    41069, 
    12296, 
    12297, 
    12358, 
    12365, 
    12401, 
    12298, 
    12415, 
    12495, 
    15639, 
    30284, 
    42558, 
    13054, 
    15757, 
    15848, 
    30213, 
    30218, 
    30219, 
    30224, 
    30299, 
    41140, 
    41141, 
    41143, 
    30222, 
    30223, 
    30226, 
    30227, 
    30228, 
    30229, 
    30233, 
    30319, 
    41147, 
    41148, 
    41149, 
    41150, 
    41151, 
    41152, 
    30297, 
    41056, 
    41071, 
    42568, 
    42600, 
    30239, 
    30379, 
    30250, 
    30251, 
    30311, 
    30356, 
    32182, 
    42591, 
    40930, 
    41770, 
    42586, 
    41855, 
    42314, 
    42559, 
    42567, 
    42583, 
    42592, 
    42599, 
    42602, 
    42606, 
    42617, 
    42618, 
    42555, 
    42556, 
    42557, 
    42565, 
    42580, 
    42620, 
    42902, 
    42560, 
    42561, 
    42562, 
    42563, 
    42589, 
    42598, 
    42621, 
    42564, 
    42566, 
    42569, 
    42570, 
    42571, 
    42572, 
    42573, 
    42574, 
    42900, 
    42576, 
    42578, 
    42579, 
    42581, 
    42582, 
    42585, 
    42587, 
    42588, 
    42590, 
    42593, 
    42594, 
    42595, 
    42596, 
    42597, 
    42607, 
    42610, 
    42613, 
    42619, 
    42601, 
    42603, 
    42604, 
    42605, 
    42608, 
    42609, 
    42612, 
    42615, 
    42616, 
    42888, 
    42889, 
    42890, 
    42898, 
    42892, 
    42896, 
    42897, 
    42901, 
    42903, 
    42905, 
    42906, 
    42907, 
    42911, 
    42912, 
    30717, 
    30718, 
    35375, 
    40156, 
    35668, 
    36058, 
    36066, 
    36067, 
    36068, 
    36106, 
    38229, 
    39209, 
    40154, 
    40157, 
    40497, 
    40527, 
    40528, 
    40529, 
    40531, 
    40532, 
    40533, 
    40534, 
    40535, 
    40536, 
    36062, 
    36063, 
    38196, 
    40130, 
    40373, 
    40374, 
    40375, 
    40376, 
    40377, 
    40378, 
    40379, 
    40496
  ) 
  AND cscart_categories.company_id = 1 
  AND (
    CASE products.tracking WHEN 'O' THEN inventory.amount > 0 WHEN 'B' THEN products.amount > 0 ELSE 1 END
  ) 
  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) 
GROUP BY 
  products.product_id 
ORDER BY 
  popularity.total desc, 
  product_id ASC 
LIMIT 
  0, 32

Query time 0.05610

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "342.77"
    },
    "ordering_operation": {
      "using_filesort": true,
      "grouping_operation": {
        "using_temporary_table": true,
        "using_filesort": false,
        "nested_loop": [
          {
            "table": {
              "table_name": "inventory",
              "access_type": "system",
              "possible_keys": [
                "pc"
              ],
              "rows_examined_per_scan": 0,
              "rows_produced_per_join": 1,
              "filtered": "0.00",
              "const_row_not_found": true,
              "cost_info": {
                "read_cost": "0.00",
                "eval_cost": "0.10",
                "prefix_cost": "0.00",
                "data_read_per_join": "880"
              },
              "used_columns": [
                "product_id",
                "amount"
              ]
            }
          },
          {
            "table": {
              "table_name": "cscart_categories",
              "access_type": "range",
              "possible_keys": [
                "PRIMARY",
                "c_status",
                "p_category_id"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "category_id"
              ],
              "key_length": "3",
              "rows_examined_per_scan": 284,
              "rows_produced_per_join": 5,
              "filtered": "2.00",
              "cost_info": {
                "read_cost": "308.12",
                "eval_cost": "0.57",
                "prefix_cost": "308.68",
                "data_read_per_join": "14K"
              },
              "used_columns": [
                "category_id",
                "company_id",
                "usergroup_ids",
                "status"
              ],
              "attached_condition": "((`maniamall_cscart`.`cscart_categories`.`company_id` = 1) and (`maniamall_cscart`.`cscart_categories`.`category_id` in (286,14399,36085,39203,40128,40198,16209,16210,29492,36150,16340,40148,40153,40167,41139,16343,40782,40783,40784,40785,40786,40787,16362,16374,16494,29493,36089,36147,36157,41434,30571,30572,30575,30965,30966,36151,36152,36154,40779,40780,40781,32211,35083,35775,35809,36059,36076,36105,40166,40177,40498,40500,40502,40503,40794,40795,40796,40797,40798,36102,36123,39202,39847,40152,40155,40193,40215,41055,41432,42575,42577,42611,42614,16293,16415,29091,30283,30391,30542,30567,40515,40516,40517,40518,40519,40520,40521,36087,40508,40509,40510,40511,40512,40513,40186,40524,40507,40514,40522,40523,28866,295,12291,12326,12347,12377,12450,12517,12526,12531,36061,36065,41415,36099,36103,36104,36125,40792,36127,40143,41069,12296,12297,12358,12365,12401,12298,12415,12495,15639,30284,42558,13054,15757,15848,30213,30218,30219,30224,30299,41140,41141,41143,30222,30223,30226,30227,30228,30229,30233,30319,41147,41148,41149,41150,41151,41152,30297,41056,41071,42568,42600,30239,30379,30250,30251,30311,30356,32182,42591,40930,41770,42586,41855,42314,42559,42567,42583,42592,42599,42602,42606,42617,42618,42555,42556,42557,42565,42580,42620,42902,42560,42561,42562,42563,42589,42598,42621,42564,42566,42569,42570,42571,42572,42573,42574,42900,42576,42578,42579,42581,42582,42585,42587,42588,42590,42593,42594,42595,42596,42597,42607,42610,42613,42619,42601,42603,42604,42605,42608,42609,42612,42615,42616,42888,42889,42890,42898,42892,42896,42897,42901,42903,42905,42906,42907,42911,42912,30717,30718,35375,40156,35668,36058,36066,36067,36068,36106,38229,39209,40154,40157,40497,40527,40528,40529,40531,40532,40533,40534,40535,40536,36062,36063,38196,40130,40373,40374,40375,40376,40377,40378,40379,40496)) and ((`maniamall_cscart`.`cscart_categories`.`usergroup_ids` = '') or (0 <> find_in_set(0,`maniamall_cscart`.`cscart_categories`.`usergroup_ids`)) or (0 <> find_in_set(1,`maniamall_cscart`.`cscart_categories`.`usergroup_ids`))) and (`maniamall_cscart`.`cscart_categories`.`status` in ('A','H')))"
            }
          },
          {
            "table": {
              "table_name": "products_categories",
              "access_type": "ref",
              "possible_keys": [
                "PRIMARY",
                "pt"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "category_id"
              ],
              "key_length": "3",
              "ref": [
                "maniamall_cscart.cscart_categories.category_id"
              ],
              "rows_examined_per_scan": 19,
              "rows_produced_per_join": 110,
              "filtered": "100.00",
              "using_index": true,
              "cost_info": {
                "read_cost": "5.13",
                "eval_cost": "11.07",
                "prefix_cost": "324.88",
                "data_read_per_join": "1K"
              },
              "used_columns": [
                "product_id",
                "category_id"
              ]
            }
          },
          {
            "table": {
              "table_name": "products",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "status"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "maniamall_cscart.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 5,
              "filtered": "5.00",
              "cost_info": {
                "read_cost": "0.00",
                "eval_cost": "0.55",
                "prefix_cost": "335.95",
                "data_read_per_join": "24K"
              },
              "used_columns": [
                "product_id",
                "status",
                "amount",
                "usergroup_ids",
                "tracking",
                "stoc_intern",
                "pret_achizitie",
                "pret_revanzator",
                "gestiune"
              ],
              "attached_condition": "((0 <> (case `maniamall_cscart`.`products`.`tracking` when 'O' then <cache>((NULL > 0)) when 'B' then (`maniamall_cscart`.`products`.`amount` > 0) else 1 end)) and ((`maniamall_cscart`.`products`.`usergroup_ids` = '') or (0 <> find_in_set(0,`maniamall_cscart`.`products`.`usergroup_ids`)) or (0 <> find_in_set(1,`maniamall_cscart`.`products`.`usergroup_ids`))) and (`maniamall_cscart`.`products`.`status` = 'A'))"
            }
          },
          {
            "table": {
              "table_name": "shared_descr",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "product_id",
                "company_id"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id",
                "lang_code",
                "company_id"
              ],
              "key_length": "13",
              "ref": [
                "maniamall_cscart.products_categories.product_id",
                "const",
                "const"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 5,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "0.00",
                "eval_cost": "0.55",
                "prefix_cost": "336.50",
                "data_read_per_join": "21K"
              },
              "used_columns": [
                "product_id",
                "lang_code",
                "company_id",
                "product"
              ]
            }
          },
          {
            "table": {
              "table_name": "descr1",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "product_id"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id",
                "lang_code"
              ],
              "key_length": "9",
              "ref": [
                "maniamall_cscart.products_categories.product_id",
                "const"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 5,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "0.00",
                "eval_cost": "0.55",
                "prefix_cost": "337.05",
                "data_read_per_join": "21K"
              },
              "used_columns": [
                "product_id",
                "lang_code",
                "product"
              ]
            }
          },
          {
            "table": {
              "table_name": "popularity",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "total"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "maniamall_cscart.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 5,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "0.00",
                "eval_cost": "0.55",
                "prefix_cost": "337.61",
                "data_read_per_join": "177"
              },
              "used_columns": [
                "product_id",
                "total"
              ]
            }
          },
          {
            "table": {
              "table_name": "prices",
              "access_type": "ref",
              "possible_keys": [
                "usergroup",
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "key": "usergroup",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "maniamall_cscart.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 1,
              "filtered": "25.00",
              "using_index": true,
              "cost_info": {
                "read_cost": "4.61",
                "eval_cost": "0.14",
                "prefix_cost": "342.77",
                "data_read_per_join": "33"
              },
              "used_columns": [
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "attached_condition": "((`maniamall_cscart`.`prices`.`lower_limit` = 1) and (`maniamall_cscart`.`prices`.`usergroup_id` in (0,0,1)))"
            }
          }
        ]
      }
    }
  }
}

Result

product_id product stoc_intern pret_achizitie pret_revanzator gestiune
386230 Telemetru cu Laser - Dispozitiv Automat de Masurare a Distantei cu Afisaj LCD -99 47.97 89.00 Y
1026757 Ochelari protectie - PC - grad luminozitate 8 - MTO-TSP305 -99 0.00 0.00 Y
671515 Set 5 extractoare suruburi rupte sau uzate + carcasa -99 5.09 20.00 Y
1001748 FILIERA HSS M10X1 Profi Tools -99 34.21 0.00 Y
1002989 DIBLURI POLISTIREN DE FIXARE - 10X130MM, 50/SET Profi Tools -99 0.00 0.00 N
1026639 Manusi protectie M15 - Marime 8 - MTO-GPA00104 -99 0.00 0.00 Y
1026634 Manusi de protectie - PU + HPPE + textil - rezistente la taiere - XL (INDUSTRIAL) - MTO-TSP1701-XL -99 0.00 0.00 Y
1028815 Kit cilindru 43cc 40mm (CG430) - MTO-MC0001 -99 0.00 0.00 Y
1029088 Saiba cap rotativ - motocoasa China (MC) MC0040.8 - MTO-MC0040.8 -99 0.00 0.00 Y
459108 Menghina din Fonta 60mm -99 18.26 45.00 Y
562176 Telemetru Laser Blow cu Display LED, Boloboc Incorporat, Calibrare Automata, Raza 50m -99 111.00 134.00 Y
1205512 Echer reglabil multifunctional pentru Tamplarie sau Constructii, 4 laturi, 70x30cm -99 4.59 5.51 Y
1779540 Ventuza dubla pentru transport geamuri sau sticla, lungime 32 cm, capacitate 70 kg, negru -99 19.03 22.83 Y
1914195 Ghidaj pentru burghie, 4-83 mm, Tagred -99 22.64 45.00 Y
1029029 Rezervor combustibil China - MTO-MC0024 -99 0.00 0.00 N
1026067 Fir taietor 15m - 3mm - MTO-MC0043.4 -99 0.00 0.00 Y
999679 VINCLU ALUMINIU CU SCALA METRICA-IMPERIALA 350MM Profi Tools -99 0.00 0.00 Y
1006064 DIBLURI METALICE M10(12X40MM), 100/SET Profi Tools -99 51.15 0.00 Y
1028861 Kit piston 52cc 44mm - MTO-MC0004 -99 0.00 0.00 Y
1005469 CONEXPANDURI ANCORA DUBLA CU TIJA M16/25X250MM, 5/SET Profi Tools -99 102.28 0.00 Y
1005477 CONEXPANDURI ANCORA DUBLA CU TIJA M20/27X250MM, 3/SET Profi Tools -99 100.44 0.00 Y
1329210 Perie circulara, cu tija, pentru lemn/metal, diametru 100 mm -99 7.19 8.63 Y
1001621 PISTOL PENTRU GRUNDUIT CU REZERVOR INFERIOR Profi Tools -99 0.00 0.00 N
1028489 Arc demaror China - MTO-MC0018 -99 0.00 0.00 Y
1051465 POMPA AUTO METAL+MANER PLASTIC ALB (MARE)-57*35CM - MTO-R50144 -99 0.00 0.00 Y
1051466 POMPA BICICLETA PLASTIC - MTO-R50147 -99 0.00 0.00 Y
1000770 GALEATA ZUGRAV DREPTUNGHIULARA - 12L Profi Tools -99 0.00 0.00 N
1026826 Pensula de vopsea 100mm - MTO-THT84042 -99 0.00 0.00 Y
1000893 DISC ABRAZIV HARTIE CU ORIFICII 125MM - GR.40, 5/SET Profi Tools -99 0.00 0.00 Y
1005493 SURUBURI GIPS-CARTON-LEMN 4.2X70MM, 250/SET Profi Tools -99 0.00 0.00 Y
1005777 CONEXPANDURI ANCORA CEP CU TIJA M8X140MM, 30/SET Profi Tools -99 0.00 0.00 N
1000273 PERIE SARMA IMPLETITA TIP CIRCULAR CU FILET 125MM Profi Tools -99 0.00 0.00 Y