我有 4 个表:出货量(200K 记录)、商店(45 条记录)、product_stores(8K 记录)、地区(698 条记录)。以下查询需要很长时间才能执行(12 秒):
SELECT `s`. * , `p`.`productCode` , `p`.`productName` , `st`.`name` AS `storeName` , `d`.`name` AS `districtName`
FROM `shipments` AS `s`
JOIN `product_stores` AS `p` ON s.productStoreId = p.id
JOIN `stores` AS `st` ON s.storeId = st.id
LEFT JOIN `districts` AS `d` ON s.districtId = d.id
WHERE (s.storeId IN (1, 2, 3, 4, 6, 9, 14, 16, 22, 26, 30))
GROUP BY `s`.`id`
ORDER BY `s`.`id` DESC
LIMIT 100
EXPLAIN 查询返回以下结果:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE st ALL PRIMARY NULL NULL NULL 45 Using where; Using temporary; Using filesort
1 SIMPLE s ref fk_shipments_stores1_idx,fk_shipments_product_stor... fk_shipments_stores1_idx 4 st.id 482
1 SIMPLE p eq_ref PRIMARY PRIMARY 4 s.productStoreId 1
1 SIMPLE d eq_ref PRIMARY PRIMARY 4 s.districtId 1
- 我不知道为什么mysql必须使用临时的;在这种情况下使用文件排序
- 为什么mysql不能启动select from shipping?然后加入商店。它开始从商店选择然后加入发货?我尝试运行 EXPLAIN,有时 mysql start select from table product_stores
- 请帮我优化表、索引...以提高性能。
(我使用的是 mysql 5.0.95)
这是表结构: