In mongodb there are multiple types of index. For this question I'm interested in the ascending (or descending) index which can be used for sorting and the hash index which according to the documentation is "primarily used with sharded clusters to support hashed shard keys" (source) ensuring "a more even distribution of data"(source)
I know that you can't create an index like: db.test.ensureIndex( { "key": "hashed", "sortOrder": 1 } )
because you get an error
{
"createdCollectionAutomatically" : true,
"numIndexesBefore" : 1,
"errmsg" : "exception: Currently only single field hashed index supported.",
"code" : 16763,
"ok" : 0
}
My question:
Between the indices:
db.test.ensureIndex( { "key": 1 } )
db.test.ensureIndex( { "key": "hashed" } )
For the query db.products.find( { key: "a" } )
, which one is more performant?, is the hashed
key O(1)
How I got to the question:
Before I knew that you could not have multi-key indices with hashed
, I created an index of the form db.test.ensureIndex( { "key": 1, "sortOrder": 1 } )
, and while creating it I wondered if the hashed index was more performant than the ascending one (hash usually is O(1)
). I left the key as it is now because (as I mentioned above) db.test.ensureIndex( { "key": "hashed", "sortOrder": 1 } )
was not allowed. But the question of is the hashed index faster for searches by a key stayed in my mind.
The situation in which I made the index was:
I had a collection that contained a sorted list of documents classified by keys.
e.g.
{key: a, sortOrder: 1, ...}
, {key: a, sortOrder: 2, ...}
, {key: a, sortOrder: 3, ...}
, {key: b, sortOrder: 1, ...}
, {key: b, sortOrder: 2, ...}
, ...
Since I used the key
to classify and the sortOrder for pagination, I always queried filtering with one value for the key
and using the sortOrder
for the order of the documents.
That means that I had two possible queries:
- For the first page
db.products.find( { key: "a" } ).limit(10).sort({"sortOrder", 1})
- And for the other pages
db.products.find( { key: "a" , sortOrder: { $gt: 10 } } ).limit(10).sort({"sortOrder", 1})
In this specific scenario, searching with O(1)
for the key and O(log(n))
for the sortOrder would have been ideal, but that wasn't allowed.