|
| 1 | +.. _drop-a-hashed-shard-key-index: |
| 2 | + |
| 3 | +============================= |
| 4 | +Drop a Hashed Shard Key Index |
| 5 | +============================= |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +.. include:: /includes/drop-hashed-shard-key-index-main.rst |
| 16 | + |
| 17 | +About this Task |
| 18 | +--------------- |
| 19 | + |
| 20 | +Dropping an `unnecessary index |
| 21 | +<http://www.mongodb.com/developer/products/mongodb/schema-design-anti-pattern-unnecessary-indexes/>`_ |
| 22 | +can speed up CRUD operations. Each CRUD operation has to update all the |
| 23 | +indexes related to a document. Removing one index can increase the |
| 24 | +speed of all CRUD operations. |
| 25 | + |
| 26 | +When you drop a hashed shard key index, the server disables balancing |
| 27 | +for that collection and excludes the collection from future balancing |
| 28 | +rounds. In order to include the collection in balancing rounds once |
| 29 | +again, you must recreate the shard key index. |
| 30 | + |
| 31 | +Steps |
| 32 | +----- |
| 33 | + |
| 34 | +.. procedure:: |
| 35 | + :style: normal |
| 36 | + |
| 37 | + .. step:: Stop the balancer |
| 38 | + |
| 39 | + Run the following command to stop the balancer: |
| 40 | + |
| 41 | + .. code-block:: javascript |
| 42 | + |
| 43 | + sh.stopBalancer() |
| 44 | + |
| 45 | + You can only run ``sh.stopBalancer()`` on ``mongos``. |
| 46 | + ``sh.stopBalancer()`` produces an error if run on ``mongod``. |
| 47 | + |
| 48 | + .. step:: Confirm there are no orphaned documents in your collection |
| 49 | + |
| 50 | + Starting in MongoDB 6.0.3, you can run an aggregation using the |
| 51 | + :pipeline:`$shardedDataDistribution` stage to confirm no orphaned |
| 52 | + documents remain: |
| 53 | + |
| 54 | + .. code-block:: javascript |
| 55 | + |
| 56 | + db.aggregate([ |
| 57 | + { $shardedDataDistribution: {} }, |
| 58 | + { $match: { "ns": "<database>.<collection>" } } |
| 59 | + ]) |
| 60 | + |
| 61 | + ``$shardedDataDistribution`` has output similar to the following: |
| 62 | + |
| 63 | + .. include:: /includes/shardedDataDistribution-output-example.rst |
| 64 | + |
| 65 | + Ensure that ``"numOrphanedDocs"`` is ``0`` for each shard in the |
| 66 | + cluster. |
| 67 | + |
| 68 | + .. step:: Drop the hashed shard key index |
| 69 | + |
| 70 | + Run the following command to drop the index: |
| 71 | + |
| 72 | + .. code-block:: javascript |
| 73 | + |
| 74 | + db.collection.dropIndex("<index name>") |
| 75 | + |
| 76 | + .. step:: Restart the balancer |
| 77 | + |
| 78 | + Run the following command to restart the balancer on the cluster: |
| 79 | + |
| 80 | + .. code-block:: javascript |
| 81 | + |
| 82 | + sh.startBalancer() |
| 83 | + |
| 84 | + |
| 85 | +Learn More |
| 86 | +---------- |
| 87 | + |
| 88 | +- :ref:`sharding-hashed` |
| 89 | +- :ref:`sharding-balancing` |
| 90 | +- :method:`db.collection.dropIndex()` |
| 91 | +- :method:`sh.stopBalancer()` |
| 92 | +- :method:`sh.startBalancer()` |
| 93 | +- :method:`sh.getBalancerState()` |
| 94 | + |
0 commit comments