Drop a Hashed Shard Key Index
On this page
Starting in MongoDB 5.0.22, you can drop the index for a hashed shard key.
This can speed up data insertion for collections sharded with a hashed
shard key. It can also speed up data ingestion when using
mongosync
.
About this Task
Dropping an unnecessary index can speed up CRUD operations. Each CRUD operation has to update all the indexes related to a document. Removing one index can increase the speed of all CRUD operations.
When you drop a hashed shard key index, the server disables balancing for that collection and excludes the collection from future balancing rounds. In order to include the collection in balancing rounds once again, you must recreate the shard key index.
Important
You should only drop a hashed shard key index from a collection
if a supporting non-hashed index on the shard key exists. If a
supporting non-hashed index does not exist on the shard key, queries
filtering by the shard key perform a collection scan.
To see what indexes exist on a collection, use
db.collection.getIndexes()
.
Steps
Confirm there are no orphaned documents in your collection
Warning
Skipping this step causes $collStats
to report an
incorrect orphaned document count and affects balancing operations
if the index is re-added.
Run the following command on the primary mongod
for every
shard on the cluster:
db.runCommand( { cleanupOrphaned: "<database>.<collection>" } )
cleanupOrphaned
returns either 1
or 0
.
A value of 1
indicates that either:
No orphaned documents remain in the
cleanupOrphaned
namespace on the shardThe collection referenced in the
cleanupOrphaned
namespace is not sharded
A value of 0
indicates that an error has occurred.
Repeat this step on each primary mongod
in the cluster.