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