Reputation: 11545
example
SELECT * FROM table WHERE column1 = x AND column2 = y
Assuming both column1 and column2 are indexed, does the order they appear in the WHERE clause matter?
one of the columns can exclude a higher number of records than the other. Should I put that first, or the column that excludes less records?
Upvotes: 1
Views: 154
Reputation: 562398
No -- boolean expressions with AND
are commutative. The optimizer should be able to figure out which column to use for each component of the index, regardless of what order you write your expression.
Your example is pretty straightforward, but this changes for lots of other cases:
OR
instead of AND
>
or !=
for range comparison instead of equality comparisonFor more information, see my presentation How to Design Indexes, Really.
Upvotes: 1