1

If I apply the "UNIQUE" constraint to a column, will the database engine traverse the whole table for an occurrence thus affecting performance?

If I have a table with millions of entries, will the engine have to check every field in the column to see if the input matches one of the fields?

How does this work?

Manan Adhvaryu
  • 323
  • 3
  • 14

1 Answers1

1

No. A unique constraint creates a unique index. The index is used for validating the constraint. The index can also be used for other purposes as well.

As far as I know, all databases, including MySQL, implement such an index using a B-tree. A lookup is O(log(n)) where n is the number of records in the table.

The difference between a unique index and unique constraint is cosmetic. A violation of a unique constraint returns the constraint name; a violation of a unique index returns the index name.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786