在我的一家Magento商店里安装了一个模块,nostress export。此模块触发以下MySQL查询以创建产品导出:
SELECT `cpf5`.`entity_id` AS `id`,
`cpf5`.`type_id` AS `product_type`,
CONCAT('http://www.store.com/',IFNULL(cpf5.url_path,CONCAT(cpf5.url_key,'.html'))) AS `url`,
`cpf5`.`name`,
IF( cpf5.image = 'no_selection','',CONCAT('http://www.store.com/media/','catalog/product', cpf5.image)) AS `image`,
`cpf5`.`ean`,
`cpf5`.`manufacturer_value` AS `manufacturer`,
`cpf5`.`sku`,
`cpf5`.`description`,
`cpf5`.`color_value` AS `color`,
`cpf5`.`new_materiaal`,
`cpf5`.`beslist_cat`,
IFNULL(cpr.parent_id,cpf5.entity_id) AS `group_id`,
(cpr.parent_id IS NOT NULL) AS `is_child`,
`ncec`.`path` AS `taxonomy_path`,
IF(cisi.qty > 0 OR (cisi.use_config_manage_stock = '0' AND cisi.manage_stock = '0' ),1,0) AS `stock_status`,
`ncmg`.`media_gallery`,
ROUND(cip.min_price,2) AS `price_final_include_tax`,
ROUND(cip.price,2) AS `price_original_include_tax`
FROM `catalog_product_flat_5` AS `cpf5`
LEFT JOIN `catalog_product_relation` AS `cpr` ON cpr.child_id=cpf5.entity_id
INNER JOIN `nostress_export_categoryproducts` AS `ncp` ON ncp.product_id=cpf5.entity_id OR ncp.product_id=cpr.parent_id
LEFT JOIN `catalog_category_flat_store_5` AS `ccf5` ON ccf5.entity_id=ncp.category_id
LEFT JOIN `nostress_export_cache_categorypath` AS `nccp` ON nccp.category_id =ccf5.entity_id AND nccp.store_id =5
INNER JOIN (
SELECT `ncp`.`product_id`, MAX(ccf5.level) AS `max_level` FROM `nostress_export_categoryproducts` AS `ncp` LEFT JOIN `catalog_category_flat_store_5` AS `ccf5` ON ccf5.entity_id=ncp.category_id AND ncp.export_id = 2 WHERE (ccf5.is_active='1') GROUP BY `product_id`
) AS `ncpml` ON ncpml.max_level = ccf5.level AND (ncpml.product_id = cpf5.entity_id OR ncpml.product_id = cpr.parent_id)
LEFT JOIN `nostress_export_cache_enginecategory` AS `ncec` ON ncec.hash = ccf5.nsc_taxonomy_beslist AND locale = 'all' AND taxonomy_code ='beslist'
LEFT JOIN `catalog_product_flat_5` AS `pcpf` ON pcpf.entity_id=cpr.parent_id
LEFT JOIN `cataloginventory_stock_status` AS `ciss` ON ciss.product_id=cpf5.entity_id AND ciss.website_id =30
LEFT JOIN `cataloginventory_stock_item` AS `cisi` ON cisi.product_id=cpf5.entity_id
LEFT JOIN `nostress_export_cache_mediagallery` AS `ncmg` ON ncmg.product_id =cpf5.entity_id AND ncmg.store_id = 5
LEFT JOIN `nostress_export_cache_tax` AS `nct` ON nct.tax_class_id = cpf5.tax_class_id AND nct.store_id = 5
LEFT JOIN `catalog_product_index_price` AS `cip` ON cip.entity_id = cpf5.entity_id AND cip.website_id = 30 AND cip.customer_group_id = '0'
WHERE (ncp.export_id='2') AND (cpf5.type_id IN ('simple')) AND (cpf5.visibility IN('4') OR pcpf.visibility IN('4')) GROUP BY `cpf5`.`entity_id` ORDER BY `group_id` ASC, `cpf5`.`type_id` ASC LIMIT 1000;
问题是此查询执行时间超过6分钟并锁定“cataloginventory_stock_status”表,因此需要此表的6分钟窗口中的所有查询都会超时。我把它缩小到最后一部分:
GROUP BY `cpf5`.`entity_id` ORDER BY `group_id` ASC
没有它,它会在30秒内运行。 “entity_id”有一个INDEX。当我查看MySQL进程列表时,查询状态为“创建排序索引”。有没有办法加快这个查询?
如果我运行EXPLAIN,这是输出:
+----+-------------+------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------+---------+----------------------------------------+-------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------+---------+----------------------------------------+-------+----------------------------------------------------+
| 1 | PRIMARY | ncp | ref | export_id,category_id,product_id | export_id | 4 | const | 25002 | Using temporary; Using filesort |
| 1 | PRIMARY | ccf5 | eq_ref | PRIMARY,IDX_CATALOG_CATEGORY_FLAT_STORE_5_LEVEL | PRIMARY | 4 | mage.ncp.category_id | 1 | Using where |
| 1 | PRIMARY | nccp | eq_ref | PRIMARY | PRIMARY | 6 | mage.ncp.category_id,const | 1 | Using index |
| 1 | PRIMARY | ncec | ALL | NULL | NULL | NULL | NULL | 1 | Using where; Using join buffer (Block Nested Loop) |
| 1 | PRIMARY | cpf5 | ref | PRIMARY,IDX_CATALOG_PRODUCT_FLAT_5_TYPE_ID,IDX_CATALOG_PRODUCT_FLAT_5_ATTRIBUTE_SET_ID,IDX_CATALOG_PRODUCT_FLAT_5_NAME,IDX_CATALOG_PRODUCT_FLAT_5_PRICE | IDX_CATALOG_PRODUCT_FLAT_5_TYPE_ID | 98 | const | 7245 | Using index condition |
| 1 | PRIMARY | cpr | ref | IDX_CATALOG_PRODUCT_RELATION_CHILD_ID | IDX_CATALOG_PRODUCT_RELATION_CHILD_ID | 4 | mage.cpf5.entity_id | 1 | Using where; Using index |
| 1 | PRIMARY | pcpf | eq_ref | PRIMARY | PRIMARY | 4 | mage.cpr.parent_id | 1 | Using where |
| 1 | PRIMARY | ciss | ref | PRIMARY,IDX_CATALOGINVENTORY_STOCK_STATUS_WEBSITE_ID | IDX_CATALOGINVENTORY_STOCK_STATUS_WEBSITE_ID | 6 | const,mage.cpf5.entity_id | 3 | Using index |
| 1 | PRIMARY | cisi | ref | UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID,IDX_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID | UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID | 4 | mage.cpf5.entity_id | 1 | NULL |
| 1 | PRIMARY | ncmg | eq_ref | PRIMARY | PRIMARY | 6 | mage.cpf5.entity_id,const | 1 | NULL |
| 1 | PRIMARY | nct | eq_ref | PRIMARY | PRIMARY | 4 | mage.cpf5.tax_class_id,const | 1 | Using where; Using index |
| 1 | PRIMARY | cip | eq_ref | PRIMARY,IDX_CATALOG_PRODUCT_INDEX_PRICE_CUSTOMER_GROUP_ID,IDX_CATALOG_PRODUCT_INDEX_PRICE_WEBSITE_ID,IDX_CAT_PRD_IDX_PRICE_WS_ID_CSTR_GROUP_ID_MIN_PRICE | PRIMARY | 8 | mage.cpf5.entity_id,const,const | 1 | NULL |
| 1 | PRIMARY | <derived2> | ref | <auto_key3> | <auto_key3> | 5 | mage.ccf5.level | 113 | Using where |
| 2 | DERIVED | ncp | ref | export_id,category_id,product_id | export_id | 4 | const | 25002 | Using where; Using temporary; Using filesort |
| 2 | DERIVED | ccf5 | eq_ref | PRIMARY | PRIMARY | 4 | mage.ncp.category_id | 1 | Using where |
+----+-------------+------------+--------+----------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------+---------+----------------------------------------+-------+----------------------------------------------------+