简体   繁体   中英

How to use composite index in range query in mysql

I have a table "test" with two field, field1 and field2, and an composite index created field1_field2(field1, field2). here is a sql : select * from test force index(field1_field2) where field1 > 100 and field2 = 2 limit 200

and it doesnt run very well. I Explained it and found that only field1 condition used by index query, field2 condition was ignored....

I want know why, and I need a perfectly answer

there are lots of data with field1 > 100 and lots of data with field2 = 2

For that query, you need INDEX(field2, field1) , in that order. Put the = columns first in an index. Once you hit a "range" (such as > ), that's the last part of the index that will be used.

Here's an Index Cookbook .

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