SELECT 
  SQL_CALC_FOUND_ROWS products.product_id, 
  descr1.product as product, 
  companies.company as company_name, 
  products.product_type, 
  products.parent_product_id, 
  IF(
    products.age_verification = 'Y', 
    'Y', 
    IF(
      cscart_categories.age_verification = 'Y', 
      'Y', cscart_categories.parent_age_verification
    )
  ) as need_age_verification, 
  IF(
    products.age_limit > cscart_categories.age_limit, 
    IF(
      products.age_limit > cscart_categories.parent_age_limit, 
      products.age_limit, cscart_categories.parent_age_limit
    ), 
    IF(
      cscart_categories.age_limit > cscart_categories.parent_age_limit, 
      cscart_categories.age_limit, cscart_categories.parent_age_limit
    )
  ) as age_limit, 
  AVG(
    cscart_discussion_rating.rating_value
  ) AS average_rating, 
  cscart_discussion.type AS discussion_type, 
  cscart_discussion.thread_id AS discussion_thread_id, 
  (
    opt_prices.product_id IS NOT NULL 
    AND abt_normal_prices.price > 0
  ) AS ab__is_qty_discount, 
  sop.old_product_only 
FROM 
  cscart_products as products 
  LEFT JOIN cscart_product_descriptions as descr1 ON descr1.product_id = products.product_id 
  AND descr1.lang_code = 'en' 
  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_warehouses_sum_products_amount as war_sum_amount ON war_sum_amount.product_id = products.product_id 
  LEFT JOIN cscart_discussion ON cscart_discussion.object_id = products.product_id 
  AND cscart_discussion.object_type = 'P' 
  LEFT JOIN cscart_discussion_posts ON cscart_discussion_posts.thread_id = cscart_discussion.thread_id 
  AND cscart_discussion_posts.status = 'A' 
  LEFT JOIN cscart_discussion_rating ON cscart_discussion.thread_id = cscart_discussion_rating.thread_id 
  AND cscart_discussion_rating.post_id = cscart_discussion_posts.post_id 
  AND cscart_discussion_rating.rating_value != 0 
  LEFT JOIN cscart_product_prices AS opt_prices ON opt_prices.product_id = products.product_id 
  AND opt_prices.lower_limit > 1 
  AND opt_prices.usergroup_id IN (0, 1) 
  LEFT JOIN cscart_product_prices AS abt_normal_prices ON abt_normal_prices.product_id = products.product_id 
  AND abt_normal_prices.lower_limit = 1 
  LEFT JOIN cscart_sg_old_product_sell as sop ON sop.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 products.parent_product_id = 0 
  AND products.product_type != 'D' 
  AND (
    sop.old_product_only = "Y" 
    OR sop.old_product_only IS NULL
  ) 
GROUP BY 
  products.product_id 
HAVING 
  average_rating > 0 
ORDER BY 
  average_rating desc, 
  products.product_id ASC 
LIMIT 
  0, 5

Query time 0.16775

JSON explain

{
  "query_block": {
    "select_id": 1,
    "cost_info": {
      "query_cost": "551.75"
    },
    "ordering_operation": {
      "using_filesort": true,
      "grouping_operation": {
        "using_temporary_table": true,
        "using_filesort": false,
        "nested_loop": [
          {
            "table": {
              "table_name": "cscart_categories",
              "access_type": "ALL",
              "possible_keys": [
                "PRIMARY",
                "c_status",
                "p_category_id"
              ],
              "rows_examined_per_scan": 152,
              "rows_produced_per_join": 6,
              "filtered": "4.00",
              "cost_info": {
                "read_cost": "15.90",
                "eval_cost": "0.61",
                "prefix_cost": "16.51",
                "data_read_per_join": "16K"
              },
              "used_columns": [
                "category_id",
                "usergroup_ids",
                "status",
                "age_verification",
                "age_limit",
                "parent_age_verification",
                "parent_age_limit",
                "storefront_id"
              ],
              "attached_condition": "(((`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')) and (`dev_yourmart_co_bw`.`cscart_categories`.`storefront_id` in (0,1)))"
            }
          },
          {
            "table": {
              "table_name": "products_categories",
              "access_type": "ref",
              "possible_keys": [
                "PRIMARY",
                "pt"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "category_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.cscart_categories.category_id"
              ],
              "rows_examined_per_scan": 89,
              "rows_produced_per_join": 541,
              "filtered": "100.00",
              "using_index": true,
              "cost_info": {
                "read_cost": "4.86",
                "eval_cost": "54.11",
                "prefix_cost": "75.49",
                "data_read_per_join": "8K"
              },
              "used_columns": [
                "product_id",
                "category_id"
              ]
            }
          },
          {
            "table": {
              "table_name": "products",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "status",
                "idx_parent_product_id"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 125,
              "filtered": "23.23",
              "cost_info": {
                "read_cost": "135.28",
                "eval_cost": "12.57",
                "prefix_cost": "264.88",
                "data_read_per_join": "654K"
              },
              "used_columns": [
                "product_id",
                "product_type",
                "status",
                "company_id",
                "usergroup_ids",
                "age_verification",
                "age_limit",
                "parent_product_id"
              ],
              "attached_condition": "((`dev_yourmart_co_bw`.`products`.`parent_product_id` = 0) and ((`dev_yourmart_co_bw`.`products`.`usergroup_ids` = '') or (0 <> find_in_set(0,`dev_yourmart_co_bw`.`products`.`usergroup_ids`)) or (0 <> find_in_set(1,`dev_yourmart_co_bw`.`products`.`usergroup_ids`))) and (`dev_yourmart_co_bw`.`products`.`status` = 'A') and (`dev_yourmart_co_bw`.`products`.`product_type` <> 'D'))"
            }
          },
          {
            "table": {
              "table_name": "companies",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "company_id"
              ],
              "key_length": "4",
              "ref": [
                "dev_yourmart_co_bw.products.company_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 12,
              "filtered": "10.00",
              "cost_info": {
                "read_cost": "31.43",
                "eval_cost": "1.26",
                "prefix_cost": "308.88",
                "data_read_per_join": "78K"
              },
              "used_columns": [
                "company_id",
                "status",
                "company"
              ],
              "attached_condition": "(`dev_yourmart_co_bw`.`companies`.`status` = 'A')"
            }
          },
          {
            "table": {
              "table_name": "prices",
              "access_type": "ref",
              "possible_keys": [
                "usergroup",
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "key": "product_id",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 7,
              "filtered": "59.08",
              "cost_info": {
                "read_cost": "3.14",
                "eval_cost": "0.74",
                "prefix_cost": "313.28",
                "data_read_per_join": "178"
              },
              "used_columns": [
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "attached_condition": "((`dev_yourmart_co_bw`.`prices`.`lower_limit` = 1) and (`dev_yourmart_co_bw`.`prices`.`usergroup_id` in (0,0,1)))"
            }
          },
          {
            "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": [
                "dev_yourmart_co_bw.products_categories.product_id",
                "const"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 7,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "1.86",
                "eval_cost": "0.74",
                "prefix_cost": "315.88",
                "data_read_per_join": "28K"
              },
              "used_columns": [
                "product_id",
                "lang_code",
                "product"
              ]
            }
          },
          {
            "table": {
              "table_name": "war_sum_amount",
              "access_type": "ref",
              "possible_keys": [
                "PRIMARY"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id"
              ],
              "rows_examined_per_scan": 20,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "using_index": true,
              "cost_info": {
                "read_cost": "2.81",
                "eval_cost": "14.85",
                "prefix_cost": "333.55",
                "data_read_per_join": "2K"
              },
              "used_columns": [
                "product_id"
              ]
            }
          },
          {
            "table": {
              "table_name": "cscart_discussion",
              "access_type": "eq_ref",
              "possible_keys": [
                "object_id"
              ],
              "key": "object_id",
              "used_key_parts": [
                "object_id",
                "object_type"
              ],
              "key_length": "6",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id",
                "const"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "1.86",
                "eval_cost": "14.85",
                "prefix_cost": "350.26",
                "data_read_per_join": "3K"
              },
              "used_columns": [
                "thread_id",
                "object_id",
                "object_type",
                "type"
              ]
            }
          },
          {
            "table": {
              "table_name": "cscart_discussion_posts",
              "access_type": "ref",
              "possible_keys": [
                "thread_id",
                "thread_id_2"
              ],
              "key": "thread_id",
              "used_key_parts": [
                "thread_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.cscart_discussion.thread_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "37.14",
                "eval_cost": "14.85",
                "prefix_cost": "402.25",
                "data_read_per_join": "64K"
              },
              "used_columns": [
                "post_id",
                "thread_id",
                "status"
              ],
              "attached_condition": "<if>(is_not_null_compl(cscart_discussion_posts), (`dev_yourmart_co_bw`.`cscart_discussion_posts`.`status` = 'A'), true)"
            }
          },
          {
            "table": {
              "table_name": "cscart_discussion_rating",
              "access_type": "eq_ref",
              "possible_keys": [
                "PRIMARY",
                "thread_id"
              ],
              "key": "PRIMARY",
              "used_key_parts": [
                "post_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.cscart_discussion_posts.post_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "1.86",
                "eval_cost": "14.85",
                "prefix_cost": "418.96",
                "data_read_per_join": "2K"
              },
              "used_columns": [
                "rating_value",
                "post_id",
                "thread_id"
              ],
              "attached_condition": "<if>(is_not_null_compl(cscart_discussion_rating), ((`dev_yourmart_co_bw`.`cscart_discussion_rating`.`thread_id` = `dev_yourmart_co_bw`.`cscart_discussion`.`thread_id`) and (`dev_yourmart_co_bw`.`cscart_discussion_rating`.`rating_value` <> 0)), true)"
            }
          },
          {
            "table": {
              "table_name": "opt_prices",
              "access_type": "ref",
              "possible_keys": [
                "usergroup",
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "key": "product_id",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "37.14",
                "eval_cost": "14.85",
                "prefix_cost": "470.95",
                "data_read_per_join": "3K"
              },
              "used_columns": [
                "product_id",
                "lower_limit",
                "usergroup_id"
              ],
              "attached_condition": "<if>(is_not_null_compl(opt_prices), ((`dev_yourmart_co_bw`.`opt_prices`.`lower_limit` > 1) and (`dev_yourmart_co_bw`.`opt_prices`.`usergroup_id` in (0,1))), true)"
            }
          },
          {
            "table": {
              "table_name": "abt_normal_prices",
              "access_type": "ref",
              "possible_keys": [
                "usergroup",
                "product_id",
                "lower_limit"
              ],
              "key": "product_id",
              "used_key_parts": [
                "product_id"
              ],
              "key_length": "3",
              "ref": [
                "dev_yourmart_co_bw.products_categories.product_id"
              ],
              "rows_examined_per_scan": 1,
              "rows_produced_per_join": 148,
              "filtered": "100.00",
              "cost_info": {
                "read_cost": "37.14",
                "eval_cost": "14.85",
                "prefix_cost": "522.94",
                "data_read_per_join": "3K"
              },
              "used_columns": [
                "product_id",
                "price",
                "lower_limit"
              ],
              "attached_condition": "<if>(is_not_null_compl(abt_normal_prices), (`dev_yourmart_co_bw`.`abt_normal_prices`.`lower_limit` = 1), true)"
            }
          },
          {
            "table": {
              "table_name": "sop",
              "access_type": "ALL",
              "rows_examined_per_scan": 8,
              "rows_produced_per_join": 278,
              "filtered": "23.44",
              "using_join_buffer": "hash join",
              "cost_info": {
                "read_cost": "0.96",
                "eval_cost": "27.85",
                "prefix_cost": "551.75",
                "data_read_per_join": "217K"
              },
              "used_columns": [
                "user_id",
                "product_id",
                "old_product_only"
              ],
              "attached_condition": "(<if>(found_match(sop), ((`dev_yourmart_co_bw`.`sop`.`old_product_only` = 'Y') or (`dev_yourmart_co_bw`.`sop`.`old_product_only` is null)), true) and <if>(is_not_null_compl(sop), (`dev_yourmart_co_bw`.`sop`.`product_id` = `dev_yourmart_co_bw`.`products_categories`.`product_id`), true))"
            }
          }
        ]
      }
    }
  }
}