[英]How to make order by faster?
I have a table with several columns to order by. 我有一张桌子,上面有几列要排序。 Those columns are float values with their corresponding indexes on them. 这些列是带有相应索引的浮点值 。 The table has around 5 million rows . 该表大约有500万行 。
The table structure id something like that: 该表的结构ID是这样的:
MyTable(id,a,b,c,d,e,f,g,h)
MyWords(id,word)
TableWords(wordID,MyTableID)
I need to run this query in a faster way which is basically a 100 top. 我需要以更快的方式运行此查询,该方式基本上是100强。 I would like to have some suggestions on how to do that better: 我想对如何做得更好提出一些建议:
select sql_no_cache a.*
from MyWords w
left join TableWords wl on w.id = wl.wordID
left join MyTable a on wl.MyTableID = a.id
where w.word = 'WordToSearch'
order by a.a desc
limit 100
The indexes: 指标:
ALTER TABLE `a` ADD INDEX `a` (`a`);
ALTER TABLE `a` ADD INDEX `b` (`b`);
Using explain: 使用说明:
1 SIMPLE w const uniqueWord,fulltext_word uniqueWord 63 const 1 Using temporary; Using filesort
1 SIMPLE wl ref wordID wordID 4 const 5597
1 SIMPLE a eq_ref PRIMARY,id id 4 test.wl.a 1
Describe TableWords: 描述TableWords:
MyTableID int(11) NO MUL
wordID int(11) NO MUL
Describe MyWords: 描述MyWords:
id int(11) NO PRI auto_increment
word varchar(20) YES UNI
Describe MyTable: 描述MyTable:
id int(11) NO PRI auto_increment
a float YES MUL
b float YES MUL
I can order by my table and it is fast, but in the moment I make a left join to that table it doesnt use the indexes anymore. 我可以按表排序,而且速度很快,但是在我向该表进行左连接时,它不再使用索引了。 I put more data in my question. 我在问题中放入了更多数据。
Thanks 谢谢
You need to provide a lot more information if you are looking for specific optimizations. 如果您正在寻找特定的优化,则需要提供更多的信息。 However in a general sense here is some things you can look into. 但是,从一般意义上讲,您可以研究以下内容。 I'll provide a short list with a link to the full details. 我将提供一个简短列表,其中包含指向完整详细信息的链接。
Optimize Order By: https://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html 优化订购者: https : //dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
Optimize Limit: https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html 优化限制: https : //dev.mysql.com/doc/refman/5.7/en/limit-optimization.html
The only way to speed it up is to have word
and a
in the same table and have INDEX(word, a)
. 加快速度的唯一方法是在同一表中包含word
和a
并具有INDEX(word, a)
。
By doing that, both the filtering ( WHERE word = '...'
) and the ordering can be done in an index. 通过这样做, 既过滤( WHERE word = '...'
)和排序可以在索引中进行。 That allows LIMIT 100
to simply peel off the first hundred rows -- instead of collecting all the filtered rows, sorting them, and only then being able to peel off 100. 这使LIMIT 100
可以简单地剥离前一百行-而不是收集所有过滤的行,对其进行排序,然后才可以剥离100。
There is another requirement: 'WordToSearch' must map to only one a.id
. 还有另一个要求:“ WordToSearch”必须仅映射到一个a.id
It is unclear from your description whether the joins are 1:1 or 1:many or many:many. 从您的描述中不清楚联接是1:1还是1:许多或许多:许多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.