Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of a unique index in MongoDB

I've tried to search through Mongo documentation, but can't really find any details on whether queries on unique indexes will be faster than queries on non-unique indexes (given the same data)

So I understand that a unique index will have high selectivity and good performance. But, given two fields whose concatenation is unique, would a non-unique compound index perform slower than a unique compound index?

I am assuming that unique indexes can slow down inserts as the uniqueness must be verified. But is the read performance improvement of a unique index, if any, really worth it?

like image 722
Zaid Masud Avatar asked Sep 10 '12 11:09

Zaid Masud


1 Answers

A quick grep of the source tree seems to indicate that unique indexes are only used on insert, so there shouldn't be any performance benefit or detriment between a query that returns one document, whether the index is unique or not.

MongoDB indexes are implemented as btrees, so it wouldn't make any logical sense for them to perform any differently whether the index is unique or not.

like image 195
Chris Heald Avatar answered Oct 11 '22 22:10

Chris Heald