@@ -33,26 +33,146 @@ View Existing Indexes
33
33
Remove Indexes
34
34
--------------
35
35
36
+ .. tip:: Hide an Index Before Dropping It
37
+
38
+ If you drop an index that is actively used in production, your
39
+ application may incur a performance degradation. Before you drop an
40
+ index, you can evaluate the potential impact of the drop by
41
+ :ref:`hiding the index <hide-existing-index>`.
42
+
43
+ Hidden indexes are not used to support queries. If you hide an index
44
+ and observe substantial negative performance impact, consider keeping
45
+ and unhiding the index so queries can resume using it.
46
+
36
47
.. include:: /includes/driver-remove-indexes-tabs.rst
37
48
38
49
Modify an Index
39
50
---------------
40
51
41
52
.. include:: /includes/driver-examples/driver-example-modify-index-tabs.rst
42
53
43
- .. tabs-drivers::
54
+ Minimize Performance Impact With a Temporary Index
55
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
56
+
57
+ If you drop an index that is actively used in production, your
58
+ application may incur a performance degradation. To ensure queries can
59
+ still use an index during modification, you can create a temporary,
60
+ redundant index that contains the same fields as the modified index.
61
+
62
+ Example
63
+ ```````
64
+
65
+ This example creates a new index and modifies that index to make it
66
+ :ref:`unique <index-type-unique>`.
67
+
68
+ .. procedure::
69
+ :style: normal
70
+
71
+ .. step:: Create a ``siteAnalytics`` collection with an index on the ``url`` field
72
+
73
+ Run this command:
74
+
75
+ .. code-block:: javascript
76
+
77
+ db.siteAnalytics.createIndex( { "url": 1 } )
78
+
79
+ The command returns the name of the index:
80
+
81
+ .. code-block:: javascript
82
+ :copyable: false
83
+
84
+ url_1
85
+
86
+ .. step:: Create a temporary index that contains the ``url`` field
87
+
88
+ Run this command:
89
+
90
+ .. code-block:: javascript
91
+
92
+ db.siteAnalytics.createIndex( { "url": 1, "dummyField": 1 } )
93
+
94
+ The command returns the name of the index:
95
+
96
+ .. code-block:: javascript
97
+ :copyable: false
98
+
99
+ url_1_dummyField_1
100
+
101
+ This temporary index lets you safely drop the original ``{ "url":
102
+ 1 }`` index without impacting performance.
103
+
104
+ .. step:: Drop the original index
105
+
106
+ Run this command:
107
+
108
+ .. code-block:: javascript
109
+
110
+ db.siteAnalytics.dropIndex( { "url": 1 } )
111
+
112
+ The command returns:
113
+
114
+ .. code-block:: javascript
115
+ :copyable: false
116
+
117
+ { nIndexesWas: 3, ok: 1 }
118
+
119
+ .. step:: Recreate the ``{ "url": 1 }`` index with the ``unique`` property
120
+
121
+ Run this command:
122
+
123
+ .. code-block:: javascript
124
+
125
+ db.siteAnalytics.createIndex( { "url": 1 }, { "unique": true } )
126
+
127
+ The command returns the name of the index:
128
+
129
+ .. code-block:: javascript
130
+ :copyable: false
131
+
132
+ url_1
133
+
134
+ The ``url_1`` index is recreated and you can drop the temporary
135
+ index without impacting performance. Queries on the ``url`` field
136
+ can use the new unique index.
137
+
138
+ .. step:: Drop the temporary index
139
+
140
+ Run this command:
141
+
142
+ .. code-block:: javascript
143
+
144
+ db.siteAnalytics.dropIndex( { "url": 1, "dummyField": 1 } )
145
+
146
+ The command returns:
147
+
148
+ .. code-block:: javascript
149
+ :copyable: false
150
+
151
+ { nIndexesWas: 3, ok: 1 }
152
+
153
+ .. step:: Confirm that the index was updated
154
+
155
+ To view the indexes on the ``siteAnalytics`` collection, run this
156
+ command:
157
+
158
+ .. code-block:: javascript
159
+
160
+ db.siteAnalytics.getIndexes()
161
+
162
+ The command returns these indexes, indicating that the ``url_1``
163
+ index is now unique:
44
164
45
- tabs:
46
- - id: compass
47
- content: |
48
- .. seealso::
165
+ .. code-block:: javascript
166
+ :copyable: false
49
167
50
- - :ref:`MongoDB Compass Documentation <compass-index>`
51
- - :compass:`Compass Documentation for Indexes </indexes/>`
168
+ [
169
+ { v: 2, key: { _id: 1 }, name: '_id_' },
170
+ { v: 2, key: { url: 1 }, name: 'url_1', unique: true }
171
+ ]
52
172
53
173
.. _manage-indexes-find-inconsistent-indexes:
54
174
55
- Find Inconsistent Indexes across Shards
175
+ Find Inconsistent Indexes Across Shards
56
176
---------------------------------------
57
177
58
178
A sharded collection has an inconsistent index if the collection does
0 commit comments