HiveBrain v1.2.0
Get Started
← Back to all entries
gotchasqlMinor

Is there any performance difference between UNIQUE INDEX and INDEX with the same cardinality on MySQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
uniquesamethecardinalitywithanydifferencemysqlbetweenperformance

Problem

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.

Solution

First, I must emphasize that you should use UNIQUE when you need the uniqueness check, not for performance considerations.

That said, here are the small performance considerations:

INSERT checks for uniqueness before acknowledging the insert. This slows down the response time (a little) for INSERTs. A plain INDEX is delayed and does not actually get performed until later. See "Change buffering". This speeds up non-unique index maintenance (a little).

A SELECT, when using a non-unique index will (unless something else stops it), will read rows until the value is different. With a UNIQUE index, it knows to stop after one row is found. (Again, a minor performance difference.)

Context

StackExchange Database Administrators Q#269293, answer score: 5

Revisions (0)

No revisions yet.