I very often search the table posts for values in the columns user+status and user+time.
SELECT * FROM `posts` WHERE `user`='xxx' and `status`='active'
SELECT * FROM `posts` WHERE `user`='xxx' and `time`>...
Thus I have set up two indices (user, status) and (user, time)
I’m aware, that writing processes are slowed down the more indices need to be updated. But I think in this case it is useful to have both indices, since reading operations outnumber writing operations by far.
Anyway, PHPMyAdmin gives a Warning saying “More than one index has been created for the column user”. Can I just ignore this warning? I checked the WordPress DB tables and saw that they have put a column at the second position, if it already had an index.
comment_approved_date_gmt = INDEX(comment_approved, comment_date_gmt)
comment_date_gmt = INDEX(comment_date_gmt)
Why don’t they use only one two column index (INDEX(comment_date_gmt, comment_approved)), that would save INDEX(comment_date_gmt)? and why is it disadvantageous to have two indices starting with the same column-name?
Is there a general rule, which column should go first in my query? For example the one with the lowest number of different entries (e.G. status) and afterwards the one with a higher number of different values (e.g. user names)
Yes, the order of columns in an index matters.
Think of an analogy to a telephone book. It’s like an index on (last_name, first_name). Looking up a person by last name, you use the sorted order of the phone book to help you find them quickly.
But if you only know the person’s first name, they are scattered throughout the book. To find one, you’d have to search the book page by page.
Yes, indexes can be redundant.
Any query that is searching for last_name can use a single-column index on (last_name), or it can get the same benefit from a two-column index on (last_name, first_name). So why create both indexes?
There’s a tool pt-duplicate-key-checker that can help you identify redundant indexes. I’ve never come across a database that didn’t have at least a few such indexes.
phpMyAdmin is wrong.
If phpMyAdmin is warning about the indexes (user, status) and (user, time), then it’s being over-zealous, because these indexes are not redundant with respect to each other. Basically, an index is redundant if its columns comprise a left-prefix of the columns in another index. So an index (A) is redundant with respect to an index (A, B), but an index (A, C) is distinct from (A, B) and both may be used by different queries.
PS: I cover these points and more in my presentation How to Design Indexes, Really.