Mysql SQL查询耗时太长-临时表
我在MySQL上的sql查询中遇到了问题,它的运行时间太长了。非常感谢您对如何优化它的帮助 该查询在一个大约有8000行的表上运行,如下所示:Mysql SQL查询耗时太长-临时表,mysql,Mysql,我在MySQL上的sql查询中遇到了问题,它的运行时间太长了。非常感谢您对如何优化它的帮助 该查询在一个大约有8000行的表上运行,如下所示: price_id int(5) Primary Key with auto increment product_id int(5) price_amount float price_date date Status Time Sorting result 0.000050
price_id int(5) Primary Key with auto increment
product_id int(5)
price_amount float
price_date date
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)
基本上,它保存多个产品的历史价格,因此您可以生成与任何历史日期相关的发票
有问题的查询必须给出与某个日期相关的产品列表的最新价格。例如,要获得产品编号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
)
因此,我可以从数字上猜测,大部分时间都花在创建临时表上。最大堆和临时表大小参数的默认值为16M
有没有人知道我如何加快这个查询的速度,或者设计一个新的查询,这样做同样但效率更高?尝试在结果表上使用dinamic from和INTERNAL联接
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
尝试在结果表上使用dinamic from和INTERNAL联接
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
使用相关查询,有时mysql在子查询中会遇到问题
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
)
使用相关查询,有时mysql在子查询中会遇到问题
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
)
如果您可以自由添加索引,请按1个产品id、2个价格日期降序对表进行索引,您可以执行以下操作:
SELECT *
FROM prices
WHERE product_id in (1,2,4) AND price_date <= '2016-09-01'
ORDER BY price_date DESC
LIMIT 1
如果您可以自由添加索引,请按1个产品id、2个价格日期降序对表进行索引,您可以执行以下操作:
SELECT *
FROM prices
WHERE product_id in (1,2,4) AND price_date <= '2016-09-01'
ORDER BY price_date DESC
LIMIT 1
查询优化和关系代数!查询优化和关系代数!那不是只会返回一个产品吗?我们需要这三种产品的价格,那不是只退回一种产品吗?我们需要这三种产品的价格