Imagine a table with multiple columns, say, id, a, b, c, d, e
. I usually select by id
, however, there are multiple queries in the client app that uses various conditions over subsets of the columns.
When MySQL executes a query on a single table with multiple WHERE conditions on multiple columns, can it really make use of indexes created on different columns? Or the only way to make it fast is to create multi-column indexes for all possible queries?
Classically, MySQL can use one index per table reference in a given query. However, in more recent versions of MySQL, an operation called an
index merge
can take place and allow MySQL to use more than one index per table.http://openquery.com/blog/mysql-50-index-merge-using-multiple-indexes
Yes, MySQL can use multiple index for a single query. The optimizer will determine which indexes will benefit the query. You can use
EXPLAIN
to obtain information about how MySQL executes a statement. You can add or ignore indexes using hints like so:I would suggest reading up on how MySQL uses indexes.
Just a few excerpts: