SQL查询需要太长时间 - 临时表

时间:2016-11-02 18:01:07

标签: mysql

我在MySQL上运行sql查询时出现问题。非常感谢您对如何优化它的帮助。

有问题的查询在一个包含大约8000行的表上运行,如下所示:

price_id int(5) Primary Key with auto increment
product_id int(5) 
price_amount float
price_date date

基本上,它保留了几种产品的历史价格,因此您可以生成与任何历史日期相关的发票。

有问题的查询必须为我提供与特定日期相关的产品列表的最新价格。例如,要获得与9月1日相关的product_id 1,2和4的相关价格,它将是:

SELECT * 
FROM prices
WHERE price_id IN (
     SELECT max(prices.price_id) 
     FROM prices
     WHERE product_id in (1,2,4) AND price_date <= '2016-09-01' 
     GROUP BY product_id
)

现在,此查询针对aaaaaages运行。它有所不同,但现在,例如,对于3个product_ids,运行大约需要58秒。通常发票中约有120种产品(需要几分钟才能返回结果),所以你可以想象出麻烦......

我在phpmyadmin中分析了查询,基本上它看起来像这样:

Status                        Time 
Sorting result                0.000050 
Sending data                  0.000025 
executing                     0.000009 
Copying to tmp table          0.008586 
Sorting result                0.000052 
Sending data                  0.000026 
executing                     0.000006 
Copying to tmp table          0.008118 
Sorting result                0.000057 
Sending data                  0.000029 
executing                     0.000011 
Copying to tmp table          0.007498 
Sorting result                0.000047 
Sending data                  0.000021 
executing                     0.000005 
Copying to tmp table          0.008479 
Sorting result                0.000056 
Sending data                  0.000031 
executing                     0.000011 
Copying to tmp table          0.007371 
Sorting result                0.000059 
Sending data                  0.000031 
executing                     0.000011 
Copying to tmp table          0.006702 
Sorting result                0.000045 
Sending data                  0.000019 
executing                     0.000005 
Copying to tmp table          0.005319 
Sorting result                0.000034 
Sending data                  0.000015 
executing                     0.000005 
Copying to tmp table          0.005302 
Sorting result                0.000035 
Sending data                  0.000016 
executing                     0.000005 
Copying to tmp table          0.005207 
Sorting result                0.000031 
Sending data                  0.000014 
executing                     0.000005 
Copying to tmp table          0.005243 
Sorting result                0.000034 
Sending data                  0.000016 
executing                     0.000005 
Copying to tmp table          0.005236 
Sorting result                0.000035 
Sending data                  0.000016 
executing                     0.000005 
Copying to tmp table          0.005185 
Sorting result                0.000035 
Sending data                  0.000015 
executing                     0.000005 
Copying to tmp table          0.005256 
Sorting result                0.000033 
Sending data                  0.000017 
executing                     0.000005 
Copying to tmp table          0.005160 
Sorting result                0.000025 
Sending data                  0.000014 
executing                     0.000005 
Copying to tmp table          0.005149 
Sorting result                0.000024 
Sending data                  0.000013 
executing                     0.000005 
Copying to tmp table          0.005356 
Sorting result                0.000038 
Sending data                  0.000016 
executing                     0.000005 
Copying to tmp table          0.005221 
Sorting result                0.000034 
Sending data                  0.000016 
executing                     0.000005 
Copying to tmp table          0.005189 
Sorting result                0.000033 
Sending data                  0.000015 
executing                     0.000005 
Copying to tmp table          0.005370 
Sorting result                0.000038 
Sending data                  0.000017 
executing                     0.000005 
Copying to tmp table          0.005208 
Sorting result                0.000035 
Sending data                  0.000017 
executing                     0.000006 
Copying to tmp table          0.005209 
Sorting result                0.000036 
Sending data                  0.000054 
end                           0.000019 
removing tmp table            0.000021 
end                           0.000008 
query end                     0.000006 
closing tables                0.000020 
freeing items                 0.000052 
Waiting for query cache lock  0.000006 
freeing items                 0.000852 
Waiting for query cache lock  0.000012 
freeing items                 0.000004 
storing result in query cache 0.000019 
logging slow query            0.000005 
logging slow query            0.000010 
cleaning up                   0.000009 

Showing rows 0 - 2 ( 3 total, Query took 58.6074 sec)

所以我猜数字,大部分时间用于创建临时表。 (最大堆和临时表大小参数的默认值为16M)。

有没有人知道我如何加快这个查询或设计一个新的查询来做同样但更有效的方法?

3 个答案:

答案 0 :(得分:0)

尝试在结果表

上使用来自()和内连接的dinamic
  select * from prices as a 
  inner join (      
       SELECT max(prices.price_id )  as max_price_id
       FROM prices
       WHERE product_id in (1,2,4) AND price_date <= '2016-09-01' 
       GROUP BY product_id ) t on t.max_price_id = a.price_id

答案 1 :(得分:0)

使用相关查询,有时mysql在IN子查询中存在问题

SELECT prices.* 
FROM prices 
WHERE EXISTS (
     SELECT max(p.price_id) 
     FROM prices p
     WHERE p.product_id in (1,2,4) AND p.price_date <= '2016-09-01' 
     AND prices.price_id = p.price_id
     GROUP BY p.product_id
)

答案 2 :(得分:0)

如果您可以自由添加索引,请按(1)product_id,(2)price_date(降序)索引表格,您可以这样做:

SELECT *
FROM prices
WHERE product_id in (1,2,4) AND price_date <= '2016-09-01' 
ORDER BY price_date DESC
LIMIT 1