Skip to content

Commit 3b6abfd

Browse files
author
Sam Kleinman
committed
DOCS-231 final component of indexing administration document
1 parent 5f37461 commit 3b6abfd

File tree

1 file changed

+155
-11
lines changed

1 file changed

+155
-11
lines changed

draft/administration/indexes.txt

Lines changed: 155 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ and ``price`` fields of the ``products`` collection:
5454

5555
db.products.ensureIndex( { item: 1, category: 1, price: 1 } )
5656

57+
58+
.. note::
59+
60+
To build indexes for a :term:`replica set`, before version 2.2,
61+
see :ref:`index-building-replica-sets`.
62+
5763
.. [#ensure] As the name suggests, :func:`ensureIndex() <db.collection.ensureIndex()>`
5864
only creates an index if an index of the same specification does
5965
not already exist.
@@ -128,18 +134,74 @@ and *not* for either key individually.
128134
Removal
129135
~~~~~~~
130136

131-
:func:`db.collection.dropIndex()`
137+
To remove an index, use the :func:`db.collection.dropIndex()` method,
138+
as in the following example:
139+
140+
.. code-block:: javascript
141+
142+
db.accounts.dropIndex( { tax-id: 1 } )
143+
144+
This will remove the index on the ``tax-id`` field in the ``accounts``
145+
collection. The shell provides the following document after completing
146+
the operation:
147+
148+
.. code-block:: javascript
149+
150+
{ "nIndexesWas" : 3, "ok" : 1 }
132151

133-
:func:`db.collection.dropIndexes()`
152+
Where the value of ``nIndexesWas`` reflects the number of indexes
153+
*before* removing this index. You can also use the
154+
:func:`db.collection.dropIndexes()` to remove *all* indexes, except
155+
for the :ref:`_id index <index-type-primary>` from a collection.
134156

135-
:dbcommand:`deleteIndexes`
157+
These shell helpers provide wrappers around the
158+
:dbcommand:`deleteIndexes` :term:`database command`. Your :ref:`client
159+
library </applications/drivers>` may have a different or additional
160+
interface for these operations.
136161

137162
Rebuilding
138163
~~~~~~~~~~
139164

140-
:func:`db.collection.reIndex()`
165+
If you need to rebuild indexes for a collection you can use the
166+
:func:`db.collection.reIndex()` method. This will drop all indexes,
167+
including the :ref:`_id index <index-type-primary>`, and then rebuild
168+
all indexes. For examples:
141169

142-
:dbcommand:`reIndex`
170+
.. code-block:: javascript
171+
172+
db.accounts.reIndex()
173+
174+
MongoDB will return the following document when the operation
175+
completes:
176+
177+
.. code-block:: javascript
178+
179+
{
180+
"nIndexesWas" : 2,
181+
"msg" : "indexes dropped for collection",
182+
"nIndexes" : 2,
183+
"indexes" : [
184+
{
185+
"key" : {
186+
"_id" : 1,
187+
"tax-id" : 1
188+
},
189+
"ns" : "records.accounts",
190+
"name" : "_id_"
191+
}
192+
],
193+
"ok" : 1
194+
}
195+
196+
This shell helper provides a wrapper around the
197+
:dbcommand:`reIndex` :term:`database command`. Your :ref:`client
198+
library </applications/drivers>` may have a different or additional
199+
interface for this operation.
200+
201+
.. note::
202+
203+
To rebuild indexes for a :term:`replica set`, before version 2.2,
204+
see :ref:`index-rebuilding-replica-sets`.
143205

144206
Special Creation Options
145207
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -150,28 +212,86 @@ Special Creation Options
150212
Background
151213
``````````
152214

215+
To create an index in the background you can specify :ref:`background
216+
construction <index-creation-background>`. Consider the following
217+
prototype invocation of :func:`db.collection.ensureIndex()`:
218+
153219
.. code-block:: javascript
154220

155221
db.collection.ensureIndex( { a: 1 }, { background: true } )
156222

157223
Drop Duplicates
158224
```````````````
159225

226+
To force the creation of a :ref:`unique index <index-type-unique>`
227+
Inez, you can use the ``dropDups`` option. This will force MongoDB to
228+
create a unique index by deleting documents with duplicate
229+
values. Consider the following prototype invocation of
230+
:func:`db.collection.ensureIndex()`:
231+
160232
.. code-block:: javascript
161233

162234
db.collection.ensureIndex( { a: 1 }, { dropDups: true } )
163235

164-
Rebuilding Indexes Replica Set
165-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
236+
See the full documentation of :ref:`duplicate dropping
237+
<index-creation-duplicate-dropping>` for more information.
238+
239+
.. warning::
240+
241+
Specifying ``{ dropDups: true }`` will delete data from your
242+
database. Use with extreme caution.
243+
244+
.. _index-building-replica-sets:
245+
246+
Building Indexes Replica Set
247+
----------------------------
166248

167249
In Version 1.8 and 2.0
168-
``````````````````````
250+
~~~~~~~~~~~~~~~~~~~~~~
251+
252+
For Version 1.8 and 2.0, :ref:`background index creation operations <index-creation-background>`
253+
become *foreground* indexing operations on :term:`secondary` members
254+
of replica sets. Because this can block replication on the
255+
secondaries, especially with long running operations use the following
256+
procedure for all non-trivial index builds:
257+
258+
#. Stop the :program:`mongod` process on one secondary. Restart the
259+
:program:`mongod` process *without* the
260+
:option:`--replSet <mongod --replSet>` option. This instance is
261+
now in "standalone" mode.
262+
263+
#. Create the new index or rebuild the index on this :program:`mongod`
264+
instance.
265+
266+
#. Restart the :program:`mongod` instance with the
267+
:option:`--replSet <mongod --replSet>` option. Allow replication to
268+
catch up on this node.
269+
270+
#. Replete this operation on all of the remaining secondaries.
271+
272+
#. Run :func:`rs.stepDown()` on the :term:`primary` member of the set,
273+
and then run this procedure on the former primary.
274+
275+
.. warning::
276+
277+
Ensure that your :ref:`oplog` is large enough to permit the
278+
indexing or re-indexing operation to complete without falling too
279+
far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
280+
documentation for additional information.
281+
282+
.. note::
283+
284+
This procedure *does* block indexing on one member of the replica
285+
set at a time. However, the foreground indexing operation is more
286+
efficient than the background index operation, and will only affect
287+
one secondary at a time rather than *all* secondaries at the same
288+
time.
169289

170290
.. rebuild indexes on secondaries and cycle so that there's never a
171291
failover.
172292

173293
In Version 2.2
174-
``````````````
294+
~~~~~~~~~~~~~~
175295

176296
.. versionchanged:: 2.2
177297

@@ -181,10 +301,34 @@ Rebuild operation on :term:`secondary` members of :term:`replica sets
181301
``{ background: true }`` option for replica sets.
182302

183303
Measuring Index Utilization
184-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
304+
---------------------------
305+
306+
.. TODO expand the .explain and .hint documentation here.
307+
308+
Query performance is a good general indicator of index utilization;
309+
however, for more precise insight into index use, MongoDB provides the
310+
following tools:
185311

186-
- :func:`cursor.explain()`
312+
- :func:`explain() <cursor.explain()>`
313+
314+
Append the :func:`explain() <cursor.explain()>` method to any cursor
315+
(e.g. a query) to return a document with statistics about the query
316+
process, including the index used, and the number of documents
317+
scanned.
187318

188319
- :func:`cursor.hint()`
189320

321+
Append the :func:`hint() <cursor.explain()>` to any cursor (e.g. a
322+
query) with the name of an index as the argument to *force* MongoDB
323+
to use a specific index to fulfill the query. Consider the following
324+
example:
325+
326+
.. code-block:: javascript
327+
328+
db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { zipcode: 1 } )
329+
190330
- :status:`indexCounters`
331+
332+
Use the :status:`indexCounters` data in the output of
333+
:dbcommand:`serverStatus` for insight into database-wise index
334+
utilization.

0 commit comments

Comments
 (0)