Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: unique field needs to be an index?

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?

like image 684
TomSawyer Avatar asked Feb 22 '12 10:02

TomSawyer


People also ask

Is unique key an index in MySQL?

MySQL allows another constraint called the UNIQUE INDEX to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which is not possible with the primary key constraint.

Do we need to create index on unique key?

Benefits of a Unique IndexProvided that the data in each column is unique, you can create both a unique clustered index and multiple unique nonclustered indexes on the same table. Unique indexes ensure the data integrity of the defined columns.


2 Answers

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.

like image 56
Shiplu Mokaddim Avatar answered Sep 24 '22 21:09

Shiplu Mokaddim


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

like image 35
Eugen Rieck Avatar answered Sep 23 '22 21:09

Eugen Rieck