3

我有 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   
  1. 我不知道为什么mysql必须使用临时的;在这种情况下使用文件排序
  2. 为什么mysql不能启动select from shipping?然后加入商店。它开始从商店选择然后加入发货?我尝试运行 EXPLAIN,有时 mysql start select from table product_stores
  3. 请帮我优化表、索引...以提高性能。

(我使用的是 mysql 5.0.95)

这是表结构:

4

2 回答 2

1

您的查询将足够慢,因为您的查询连接策略需要太多 I/O

让我为您的查询 I/O 起草计算,以便理解如下:

 1. JOIN shipments (200K records) and product_stores (8K records) 

         200K x 8 K = 1600K I/O
 2. Then, JOIN to stores (45 records)

         1600K x 45 = 75000K I/O

 3. Then, JOIN to districts (698 records)

         75000K x 698 = 50256000K I/O

 4. Then, Filter the result (by storeId), so need to read the result I/O again

         50256000K + 50256000K = **100512000K I/O (TOTAL I/O)** 

 So, total I/O on memory of your query is 100512000K I/O. 

为了提高您的查询性能,您需要重新考虑您的查询加入计划/策略

例如:

 1. Read shipments (200K records) and Filter storeId  (assume: result is 8 record)

         200K  + 8 = 208K I/O
 2. Then, JOIN to product_stores (8K records)

         208K x 8K = 1664K I/O

 3. Then, JOIN to stores (45 records)

         1664K x 45K = 74880K I/O

 4. Then, finally JOIN to districts (698 records).

         74880K + 698 = **52266240 I/O (TOTAL I/O)** 

 So, total I/O on memory of your query is 52266240  I/O. (greatly reduce I/O then ever)

因此,您可以通过以上考虑的方式来提高查询性能。

我希望它可以帮助你。

于 2012-08-30T08:58:59.427 回答
0

我只是在尝试解决方案。希望这应该减少执行时间

SELECT `s`. * , `ps`.`productCode` , `ps`.`productName` , `st`.`name` AS `storeName` , `d`.`name` AS `shipToDistrictName`
FROM `shipments` AS `s`
JOIN `product_stores` AS `ps` ON s.productStoreId = ps.id
JOIN `stores` AS `st` ON (s.storeId = st.id AND s.storeId IN (1, 2, 3, 4, 6, 9, 14, 16, 22, 26, 30))
LEFT JOIN `districts` AS `d` ON s.shipToDistrictId = d.id
GROUP BY `s`.`id`
ORDER BY `s`.`id` DESC
LIMIT 100

这会将记录数限制为仅具有 storeId 的那些记录,并进一步以减少的记录数进行连接,从而减少执行时间。

希望能帮助到你...

于 2012-08-30T06:53:28.557 回答