我有一个 300 万条记录的表,称为“交易”。
CREATE TABLE transactions(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
lookupAId int(6) NOT NULL,
.....
updateDate TIMESTAMP
)
在最坏的情况下,用户将不指定任何过滤器,查询将如下所示:
select * from transactions
join lookupA on (well indexed columns)
.. ( 12 lookup table joins)
order by updateDate limit 500
如果没有 order by 子句,查询将以毫秒为单位运行,但如果使用 order by 则需要大约一分钟。该表预计将增长到 12-1500 万条记录。
- 我的 SLA 是在一秒内获得结果,这在 MySql 中可能吗?
- 如何优化 order by 子句以使其执行。
我在 AWS 中的 xLarge 内存优化 RDS 实例中运行 MySql 5.7
UPDATE 1 updateDate 有时间分量并已索引(B 树,非唯一)
更新 2 这有效,尽管我不知道为什么
SELECT * FROM (select * from transactions order by updateDate) transactions
join lookupA on (well indexed columns)
.. ( 12 lookup table joins)
limit 500
最佳答案
在用 limit 限制查询大小之前,MySQL 可能在查询上做了很多工作。这似乎是 MySQL 的一个已知弱点。
尝试在子查询中从事务中进行选择,以在进行联接之前限制结果集大小。
SELECT * FROM (select * from transactions order by updateDate limit 500) transactions
join lookupA on (well indexed columns)
.. ( 12 lookup table joins)
关于mysql - 在 MySql 中优化 order by 带限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267087/