If I have this index:
(col1, col2, col3)
I know it helps when I search through (col1); (col1, col2); (col1, col2, col3).
If I create another index with the exactly same columns, phpMyAdmin will warn me that one of those indexes may be removed, because they are the same.
However, if I have these indexes:
(col1, col2, col3)
(col1, col2)
(col1)
phpMyAdmin won’t warn me at all.
So my question is, are the last two indexes necessary in any case? I think only the first index is enough.
Thank you.
MySQL will only use one index (the leftmost) to optimize the search. Quoting from the documentation:
“If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3).“
However, if any of the indexes are UNIQUE, then there’s probably a good reason for them to be there.
If during your analysis you find that any of the columns are frequently used apart from one another, then you should consider adding separate indexes for each to optimize those queries.
E.g.