MySQL Performance: UNIQUE INDEX vs INDEX with Same Cardinality

indexMySQL

I have a table that has id and created_at.

Column     | Non Unique | Cardinality
-------------------------------------
id         | 0          | 1000
created_at | 1          | 1000

id is unique, and created_at is non unique.

I want to know if there's any performance improvement if I convert the created_at index from non unique to unique.

Best Answer

There will be no difference for read operations, once written the entries in the index will be the same.

In theory there is a slight difference in update performance as the engine needs to enforce uniqueness in a unique index, but in reality this is one going to be at most a few CPU cycles per row difference so will be unnoticeable.

Only create a unique index for values that you know should be unique. It is quite conceivable that two items could be created at the same time (or at least within the granularity of the data type you are using for that column).