我在运行MySQL的查询时遇到问题。感谢您的帮助,了解如何进行优化。SQL查询需要的时间太长 - 临时表
讨论中的查询表上运行约8000行,看起来像这样:
price_id int(5) Primary Key with auto increment
product_id int(5)
price_amount float
price_date date
基本上,它拥有的几款产品的历史价格,这样就可以生成相关的任何历史性的日子发票。
有问题的查询必须提供与特定日期相关的产品列表的最新价格。例如,获得的product_id 1,2和4,以9月1日的相关价格,这将是:
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的默认值)。
有没有人有任何想法如何我可以加快这个查询或设计一个新的查询,但做得更好,但效率更高?
查询优化和关系代数! –