MySQL: unique field needs to be an index?

MysqlIndexingFieldUnique

Mysql Problem Overview


I have one UNIQUE field in my table, and I need to search over it quickly. Do I need to index it?

Do searches over unique fields and indexed fields vary in speed or resource usage?

Mysql Solutions


Solution 1 - Mysql

No, you dont have to index it again. When you specify UNIQUE KEY, the column is indexed. So it has no difference in performance with other indexed column (e.g. PRIMARY KEY) of same type.

However if the type is different, there will be little performance difference.

Solution 2 - Mysql

Every UNIQUE field is by definition indexed with a UNIQUE INDEX - this also happens to be the fastest searchable access path.

Solution 3 - Mysql

If the field needs to be UNIQUE then it should either be the PRIMARY KEY or a UNIQUE INDEX.

As for performance between UNIQUE INDEX and INDEX, there is no difference when selecting as both will use the same algorithm i.e. hashing or b-tree. It's just that with a UNIQUE index, especially a numeric i.e. INT one, it will be faster than an index which contains duplicates as algorithms such as b-tree are able to more efficiently get to the requested row(s)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTomSawyerView Question on Stackoverflow
Solution 1 - MysqlShiplu MokaddimView Answer on Stackoverflow
Solution 2 - MysqlEugen RieckView Answer on Stackoverflow
Solution 3 - MysqlSimon at My School PortalView Answer on Stackoverflow