I know there are lost of similar questions to this one, but none that really answers this:
Is there ANY difference in performance in using an unique index in a primary key, using a MyISAM table on a server running MySQL 5.5?
If you need more informations, I can provide all.
I just want to know if it is worth (lets say, if the 'seek' time decreases by more than 5 milliseconds) to do that change or if I'm committing a hideous crime.
And yes, this is a serious question.
Best Answer
A
PRIMARY KEY
is, by definition,UNIQUE
andNOT NULL
, so by adding a secondUNIQUE
keyword on it you are actually creating a separate index. This does not only make things less efficient but, in some cases, it will make your queries less performant.You can check that two index were created by doing:
Both in MyISAM and InnoDB, it will require additional space, it will make the writes slower (because the index has to be updated synchronously at write time), and it may waste optimizer (even if microseconds) time deciding which index is the best one. MySQL would usually select the PRIMARY key in MyISAM, but if for any reason both indexes were used, you will also waste memory.
If you do not believe me, believe MySQL:
A run of pt-duplicate-key-checker is usually enough to detect those duplicates.
If you mean using
UNIQUE NOT NULL
instead ofPRIMARY KEY
, I would not expect a difference on MyISAM in most cases (although some tools work better with a PK, like row-based replication); it is not the case with InnoDB, in which the PK is fundamental on its structure (clustered rows around it).