performance - Mysql Slow Query group concat with subquery -
i have following query
select `cs`.sku , group_concat( if( ( ( select count(id_catalog_category) `catalog_category` lft <= `ccg`.lft , rgt >= `ccg`.rgt )=2 ), `ccg`.name_en, null ) ) level_1_categories catalog_simple `cs` left join `catalog_config` `cc` on `cs`.`fk_catalog_config` = `cc`.`id_catalog_config` left join `catalog_config_has_catalog_category` `cchcc` on `cc`.`id_catalog_config` = `cchcc`.`fk_catalog_config` left join `catalog_category` `ccg` on `cchcc`.`fk_catalog_category` = `ccg`.`id_catalog_category` group `cc`.sku
explain extended give following results.
"id" "select_type" "table" "type" "possible_keys" "key" "key_len" "ref" "rows" "filtered" "extra" "1" "primary" "cs" "all" null null null null "27384" "100.00" "using temporary; using filesort" "1" "primary" "cc" "eq_ref" "primary" "primary" "4" "pkfas.cs.fk_catalog_config" "1" "100.00" "" "1" "primary" "cchcc" "ref" "uk_id_catalog_config_has_catalog_category,fk_catalog_config" "uk_id_catalog_config_has_catalog_category" "4" "pkfas.cc.id_catalog_config" "2" "100.00" "using index" "1" "primary" "ccg" "eq_ref" "primary" "primary" "4" "pkfas.cchcc.fk_catalog_category" "1" "100.00" "" "2" "dependent subquery" "catalog_category" "all" "lft,rgt" null null null "1739" "100.00" "using where"
this query takes around 100 seconds execute
sql slow has following results
query_time: 95.189445 lock_time: 0.000000 rows_sent: 8523 rows_examined: 242668622
please guide me way make fast. in advance
the performance of query can achieved removing dependent subquery (which executed each row) try this:
select `cs`.sku , group_concat( if( tmp.za_count = 2, `ccg`.name_en, null ) ) level_1_categories catalog_simple `cs` left join `catalog_config` `cc` on `cs`.`fk_catalog_config` = `cc`.`id_catalog_config` left join `catalog_config_has_catalog_category` `cchcc` on `cc`.`id_catalog_config` = `cchcc`.`fk_catalog_config` left join ( select `ccg`.`id_catalog_category`, sum(if(`ccg1`.`id_catalog_category` null,0,1)) za_count `catalog_category` `ccg` on `cchcc`.`fk_catalog_category` = `ccg`.`id_catalog_category` left join `catalog_category` `ccg1` on `ccg1`.lft <= `ccg`.lft , `ccg1`.rgt >= `ccg`.rgt group `ccg`.`id_catalog_category` ) tmp on tmp.id_catalog_category = `cchcc`.`fk_catalog_category` group `cc`.sku
Comments
Post a Comment