The Case for MongoDB Hashed Indexes

GraphViz Example: hashtable.dotIn addition to scalar indexes (ascending, descending), MongoDB also supports ‘hashed’ indexes.  When you use a hashed index on a field, MongoDB computes a hash of the field value and stores the hash in the index. Hashed indexes support only equality comparison and do not support range queries, and are typically used in sharding scenarios.

Creating a MongoDB Hashed Index

You can use the following syntax to create a hashed index:

db.collection.ensureIndex({'field':'hashed'});

You can create both a scalar index and a hashed index on the same field.

MongoDB Sharding

Typically, MongoDB sharding is implemented using ‘range-based’ partitioning. In this approach, each shard is assigned a range of values of the shard key.  If the shard key is monotonically increasing like timestamps or objectID, this can sometimes result in a ‘hot’ shard because the most recent values always end up being routed to the same shard.

The way to get around this is to use ‘hash-based partitioning’.  A hash of the shard key is computed and this hash value is used to route to a shard instead of the actual value. This helps to distribute load evenly across all the shards instead of sending all the newest data to the same shard. Hash-based partitioning is implemented using hashed indexes on your shard key.  For more information, refer to the sharding documentation.

Hashed Index Pros & Cons

  • Pros

    Hashed indexes tend to be smaller than the scalar indexes because only a hash of the key is stored instead of the full key. E.g. In a simple test with 100k documents, we added hashed and scalar indexes on a string field – ‘fieldName’. As shown below, the hashed index tends to be considerably smaller than the scalar indexes:

    "indexSizes" : {
            "_id_" : 811008,
            "firstName_1" : 4415488,
            "firstName_hashed" : 1490944
    }
    
  • Cons

    Does not support range queries. If you run a range query on a hashed index it will result in a index scan.

MongoDB Hashed Indexes for Shards - Pro & ConsClick To Tweet

Hashed Index Constraints

  1. Hashed indexes do not support arrays.
  2. Hashed indexes cannot be compound indexes.
  3. You cannot add unique constraints on hashed indexes.
    1.  


Dharshan is the founder of ScaleGrid.io (formerly MongoDirector.com). He is an experienced MongoDB developer and administrator. He can be reached for further comment at @dharshanrg


33 Shares
+12
Tweet
Share
Share31
Pin