简体   繁体   中英

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) . 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.

For the second query, you may try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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