Unique indexes are a strange beast. They have no impact on standard databases that use B-Trees, such as MongoDB and MySQL, but may be horribly painful for databases that use write optimized data structures, like TokuMX’s Fractal Tree(R) indexes. How? They essentially drag the Fractal Tree index down to the B-Tree’s level of performance.
When a user declares a unique index, the user tells the
database, “please help me and enforce uniqueness on this index.” So,
before doing any insertion into a unique index, the database must first
verify that the key being inserted does not already exist. If the
possible location of the key is not in memory, which may happen if the working set
does not fit in memory, then the database MUST perform an I/O to bring the contents of the potential location
into memory (be it a leaf node in
a tree, or an offset into a memory mapped file), in order to check
whether the key exists in that location.
I/Os are expensive. A hard disk has only so many I/O’s available per second, so we should be wary of ever using one when we don’t need to.
This is where the difference in impact that unique indexes
have on B-Trees v. write-optimized data structures comes into play. For
the B-Tree, the I/O does not matter, because the subsequent insertion
would perform the I/O anyway to insert the key. But a Fractal Tree
index does not require an I/O on insertion. For Fractal Tree indexes,
this I/O is a big cost!
So essentially, unique keys drag the performance of Fractal Tree indexes down to a B-Tree’s level, eliminating one of the biggest innovations of TokuMX and TokuDB for MySQL.
So, the moral of the story is this: Don’t create a unique index unless you REALLY, REALLY have to. Your performance may suffer greatly. Don’t create unique indexes because you happen to know an index will be unique. Only do so if your application depends on the database enforcing uniqueness. And if it really does, you may want to try to find a way to change your application.