About every second application I look at has some tables which have redundant or duplicate indexes so its the time to speak about these a bit.
So what is duplicate index ? This is when table has multiple
indexes defined on the same columns. Sometimes it is indexes with
different names, sometimes it is different keywords used to
define the index. For example it is quite frequite to see
something like
PRIMARY KEY(id), UNIQUE KEY id(id), KEY id2(id)
The logic I heard behind this often - create primary key as object identifier, now we create UNIQUE because we want it to be UNIQUE and we create KEY so it it can be used in the queries. This is wrong and hurts MySQL Performance. It is enough to create PRIMARY KEY and it will enforce unique values and will be used in the queries.
The other case is simply having multiple keys on same column(s) - I guess someone thought key would make sense while did not …
[Read more]