【发布时间】:2017-09-08 12:57:24
【问题描述】:
我有一个比这个更复杂的查询(获得最便宜的价格),但这代表性能问题很好。所以查询不能改变。
我尝试创建不同的索引来加快订购速度。
需要什么索引才能更快地获得这个 1.4 秒的持续查询?如果我删除 ORDER BY 查询持续 0.05 秒,但我需要排序。
查询
SELECT id AS pid
FROM prices pt
WHERE pt.id = (
SELECT pt2.id
FROM prices pt2
WHERE pt2.oid=pt.oid
ORDER BY pt2.price
LIMIT 1
)
解释
1 PRIMARY pt index NULL id_price 12 NULL 9144 Using where; Using index
2 DEPENDENT SUBQUERY pt2 ref oid,oid_price oid 4 oid 23 Using where; Using filesort
索引
PRIMARY PRIMARY 9144 id
price INDEX 703 price
oid INDEX 397 oid
id_price INDEX 9144 id,price,oid
oid_price INDEX 4572 oid,price
【问题讨论】:
-
查看Groupwise Max 以获得更高效的 SQL。
标签: mysql performance indexing filesort