Reputation: 941
Does creating an index on two columns 'x' and 'y' makes queries faster when searching on column x only? For example I have a table with columns 'ID', 'x' and 'y'. And I have two questions:
When I create two separate indexes for 'x' and 'y', will it help (make things faster) with a query like this:
select * from `table` where x=1 and y=2
? I believe it will not. But not sure.
When I create one index on two colums 'x' and 'y' will it help with a query like:
select * from `table` where x=1
? That is, searching only on one of these columns.
Upvotes: 1
Views: 57
Reputation: 6654
EXPLAIN
will show if it is used. If it is not, one must add a combined index.(x,y)
as a key and (y,x)
. A rear part of a combined index can only be used, if the parts before it were used. For SELECT...WHERE x=1
the index (x,y)
can be used, whereas the index (y,x)
cannot.Upvotes: 2