简体   繁体   English

用于in和order by子句的MySQL索引

[英]MySQL index for in and order by clause

I have a table with columns 我有一张有栏的桌子

user (id, name, username, company, salary)

What will be the best index strategy for these two queries 这两个查询的最佳索引策略是什么

select * from user where company in ("a", "b") order by salary limit 20

and

select * from user order by salary limit 20 

It's good to be index(company, salary) . 最好是index(company, salary) Consider that ordering of indexes is important to notice. 考虑到索引的顺序很重要。

You could try the following covering index for the first query: 您可以为第一个查询尝试以下覆盖索引:

CREATE INDEX idx1 ON user (company, salary, name, username);

The name and username columns are included to cover the SELECT * , which requires all columns. 包括nameusername name以覆盖SELECT * ,这需要所有列。

For the second query, you may try: 对于第二个查询,您可以尝试:

CREATE INDEX idx2 ON user (salary, company, name, username);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM