2

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

2
  • Try swapping your index columns to (field2, field1). Commented Oct 24, 2016 at 8:43
  • Also, read up here. Commented Oct 24, 2016 at 9:17

1 Answer 1

6

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 .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.