Skip to content

Commit 9b87d10

Browse files
author
Sam Kleinman
committed
merge: DOCS-330 changes consistent with final reviews of the indexing docs
2 parents 0a17607 + 1dd4f9a commit 9b87d10

File tree

5 files changed

+310
-221
lines changed

5 files changed

+310
-221
lines changed

draft/administration/indexes.txt

Lines changed: 86 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ of the ``people`` collection:
3838

3939
.. code-block:: javascript
4040

41-
db.people.ensureIndex( { phone-number: 1 } )
42-
43-
TODO: you need ""s around phone-number, otherwise it's invalid JS (phone minus number).
41+
db.people.ensureIndex( { "phone-number": 1 } )
4442

4543
To create a :ref:`compound index <index-type-compound>`, use an
4644
operation that resembles the following prototype:
@@ -57,21 +55,18 @@ collection:
5755

5856
db.products.ensureIndex( { item: 1, category: 1, price: 1 } )
5957

60-
.. note::
61-
62-
To build indexes for a :term:`replica set`, before version 2.2,
63-
see :ref:`index-building-replica-sets`.
58+
Some drivers may specify indexes, using ``NumberLong(1)`` rather than
59+
``1`` as the specification. This does not have any affect on the
60+
resulting index.
6461

65-
TODO: I don't think anything changed about replica set index builds for 2.2...
62+
.. include:: /includes/note-build-indexes-on-replica-sets.rst
6663

6764
.. [#ensure] As the name suggests, :func:`ensureIndex() <db.collection.ensureIndex()>`
6865
only creates an index if an index of the same specification does
6966
not already exist.
7067

71-
Sparse
72-
``````
73-
74-
TODO: Sparse? Maybe "Types of Indexes->Sparse"?
68+
Sparse Indexes
69+
``````````````
7570

7671
To create a :ref:`sparse index <index-type-sparse>` on a field, use an
7772
operation that resembles the following prototype:
@@ -91,16 +86,13 @@ without the ``twitter_name`` field.
9186

9287
.. note::
9388

94-
MongoDB cannot create sparse compound indexes.
89+
Sparse indexes can affect the results returned by the query,
90+
particularly with respect to sorts on fields *not* included in the
91+
index. See the :ref:`sparse index <index-type-sparse>` section for
92+
more information.
9593

96-
TODO: is this true? I thought that it could.
97-
98-
TODO: Is there more doc on spare indexes somewhere? Seems like this is missing
99-
some info like getting different results back when the index is used, null
100-
counts as existing, etc.
101-
102-
Unique
103-
``````
94+
Unique Indexes
95+
``````````````
10496

10597
To create a :ref:`unique indexes <index-type-unique>`, consider the
10698
following prototype:
@@ -109,21 +101,17 @@ following prototype:
109101

110102
db.collection.ensureIndex( { a: 1 }, { unique: true } )
111103

112-
For example, you may want to create a unique index on the ``tax-id:``
104+
For example, you may want to create a unique index on the ``"tax-id":``
113105
of the ``accounts`` collection to prevent storing multiple account
114106
records for the same legal entity:
115107

116108
.. code-block:: javascript
117109

118-
db.accounts.ensureIndex( { tax-id: 1 }, { unique: true } )
119-
120-
TODO: tax-id should be in ""s.
110+
db.accounts.ensureIndex( { "tax-id": 1 }, { unique: true } )
121111

122112
The :ref:`_id index <index-type-primary>` is a unique index. In some
123-
situations you may want to use the ``_id`` field for these primary
124-
data rather than using a unique index on another field.
125-
126-
TODO: "for these primary data"?
113+
situations you may consider using ``_id`` field itself for this kind
114+
of data rather than using a unique index on another field.
127115

128116
In many situations you will want to combine the ``unique`` constraint
129117
with the ``sparse`` option. When MongoDB indexes a field, if a
@@ -155,11 +143,9 @@ as in the following example:
155143

156144
.. code-block:: javascript
157145

158-
db.accounts.dropIndex( { tax-id: 1 } )
146+
db.accounts.dropIndex( { "tax-id": 1 } )
159147

160-
TODO: ""s!
161-
162-
This will remove the index on the ``tax-id`` field in the ``accounts``
148+
This will remove the index on the ``"tax-id"`` field in the ``accounts``
163149
collection. The shell provides the following document after completing
164150
the operation:
165151

@@ -216,16 +202,7 @@ This shell helper provides a wrapper around the :dbcommand:`reIndex`
216202
</applications/drivers>` may have a different or additional interface
217203
for this operation.
218204

219-
.. note::
220-
221-
To rebuild indexes for a :term:`replica set`, before version 2.2,
222-
see :ref:`index-rebuilding-replica-sets`.
223-
224-
TODO: again, this probably isn't different in 2.2
225-
226-
TODO: one thing that I would appreciate you mentioning is that some drivers may
227-
create indexes like {a : NumberLong(1)} _which is fine_ and doesn't break
228-
anything so stop complaining about it.
205+
.. include:: /includes/note-build-indexes-on-replica-sets.rst
229206

230207
Special Creation Options
231208
~~~~~~~~~~~~~~~~~~~~~~~~
@@ -235,7 +212,8 @@ Special Creation Options
235212
TTL collections use a special ``expire`` index option. See
236213
:doc:`/tutorial/expire-data` for more information.
237214

238-
TODO: Are 2d indexes getting a mention?
215+
.. TODO: insert link here to the geospatial index documents when
216+
they're published.
239217

240218
Background
241219
``````````
@@ -248,26 +226,17 @@ prototype invocation of :func:`db.collection.ensureIndex()`:
248226

249227
db.collection.ensureIndex( { a: 1 }, { background: true } )
250228

251-
TODO: what does it mean to build an index in the background? You might want to
252-
mention:
253-
* performance implications
254-
* that this type of index build can be killed
255-
* that this blocks the connection you sent the ensureindex on, but ops from
256-
other connections can proceed in
257-
* that indexes are created on the foreground on secondaries in 2.0,
258-
which blocks replication & slave reads. In 2.2, it does not block reads (but
259-
still blocks repl).
229+
Consider the section on :ref:`background index construction
230+
<index-creation-background>` for more information about these indexes
231+
and their implications.
260232

261233
Drop Duplicates
262234
```````````````
263235

264236
To force the creation of a :ref:`unique index <index-type-unique>`
265-
index
266-
267-
TODO: " on a collection with duplicate values in the field to be indexed "
268-
269-
you can use the ``dropDups`` option. This will force MongoDB to
270-
create a *unique* index by deleting documents with duplicate values
237+
index on a collection with duplicate values in the field you are
238+
indexing you can use the ``dropDups`` option. This will force MongoDB
239+
to create a *unique* index by deleting documents with duplicate values
271240
when building the index. Consider the following prototype invocation
272241
of :func:`db.collection.ensureIndex()`:
273242

@@ -280,82 +249,63 @@ See the full documentation of :ref:`duplicate dropping
280249

281250
.. warning::
282251

283-
Specifying ``{ dropDups: true }`` will delete data from your
252+
Specifying ``{ dropDups: true }`` may delete data from your
284253
database. Use with extreme caution.
285254

286-
TODO: I'd say it "may" delete data from your DB, not like it's going to go all
287-
Shermanesque on your data.
288-
289255
.. _index-building-replica-sets:
290256

291257
Building Indexes on Replica Sets
292258
--------------------------------
293259

294-
.. versionchanged:: 2.2
295-
Index rebuilding operations on :term:`secondary` members of
296-
:term:`replica sets <replica set>` now run as normal background
297-
index operations. Run :func:`ensureIndex()
298-
<db.collection.ensureIndex()>` normally with the ``{ background:
299-
true }`` option for replica sets. Alternatively, you may always use
300-
the following operation to isolate and control the impact of
301-
indexing building operations on a set as a whole.
302-
303-
TODO: I think there needs to be a huge mention that this still blocks
304-
replication, so the procedure below is recommended.
305-
306-
.. admonition:: For Version 1.8 and 2.0
307-
308-
:ref:`Background index creation operations
309-
<index-creation-background>` became *foreground* indexing
310-
operations on :term:`secondary` members of replica sets. These
311-
foreground operations will block all replication on the
312-
secondaries,
260+
Consideration
261+
~~~~~~~~~~~~~
313262

314-
TODO: and don't allow any reads to go through.
263+
:ref:`Background index creation operations
264+
<index-creation-background>` become *foreground* indexing operations
265+
on :term:`secondary` members of replica sets. These foreground
266+
operations will block all replication on the secondaries, and don't
267+
allow any reads. As a result in most cases use the following procedure
268+
to build indexes on secondaries.
315269

316-
and can impact performance of the entire set. To build
317-
indexes with minimal impact on a replica set, use the following
318-
procedure for all non-trivial index builds:
270+
Procedure
271+
~~~~~~~~~
319272

320-
#. Stop the :program:`mongod` process on one secondary. Restart the
321-
:program:`mongod` process *without* the :option:`--replSet <mongod --replSet>`
322-
option. This instance is now in "standalone" mode.
273+
#. Stop the :program:`mongod` process on one secondary. Restart the
274+
:program:`mongod` process *without* the :option:`--replSet <mongod --replSet>`
275+
option and running on a different port. [#different-port]_ This
276+
instance is now in "standalone" mode.
323277

324-
TODO: generally we recommend running it on a different port, too, so that apps
325-
& other servers in the set don't try to contact it.
278+
#. Create the new index or rebuild the index on this :program:`mongod`
279+
instance.
326280

327-
#. Create the new index or rebuild the index on this :program:`mongod`
328-
instance.
281+
#. Restart the :program:`mongod` instance with the
282+
:option:`--replSet <mongod --replSet>` option. Allow replication
283+
to catch up on this member.
329284

330-
#. Restart the :program:`mongod` instance with the
331-
:option:`--replSet <mongod --replSet>` option. Allow replication
332-
to catch up on this member.
285+
#. Repeat this operation on all of the remaining secondaries.
333286

334-
#. Replete this operation on all of the remaining secondaries.
287+
#. Run :func:`rs.stepDown()` on the :term:`primary` member of the
288+
set, and then repeat this procedure on the former primary.
335289

336-
#. Run :func:`rs.stepDown()` on the :term:`primary` member of the
337-
set, and then run this procedure on the former primary.
338-
339-
.. warning::
290+
.. warning::
340291

341-
Ensure that your :ref:`oplog` is large enough to permit the
342-
indexing or re-indexing operation to complete without falling
343-
too far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
344-
documentation for additional information.
292+
Ensure that your :ref:`oplog` is large enough to permit the
293+
indexing or re-indexing operation to complete without falling
294+
too far behind to catch up. See the ":ref:`replica-set-oplog-sizing`"
295+
documentation for additional information.
345296

346-
.. note::
297+
.. note::
347298

348-
This procedure *does* block indexing on one member of the
349-
replica set at a time. However, the foreground indexing
350-
operation is more efficient than the background index operation,
351-
and will only affect one secondary at a time rather than *all*
352-
secondaries at the same time.
299+
This procedure *does* take one member out of the replica set at a
300+
time. However, this procedure will only affect one member of the
301+
set at a time rather than *all* secondaries at the same time.
353302

354-
For the best results, always create indexes *before* you begin
355-
inserting data into a collection.
303+
.. [#different-port] By running the :program:`mongod` on a different
304+
port, you ensure that the other members of the replica set and all
305+
clients will not contact the member while you are building the
306+
index.
356307

357-
TODO: well, sort of. That'll build the indexes fast, but make the inserts
358-
slower. Overall, it's faster to insert data, then build indexes.
308+
.. _indexes-measuring-use:
359309

360310
Measuring Index Use
361311
-------------------
@@ -374,31 +324,41 @@ following tools:
374324
- :func:`cursor.hint()`
375325

376326
Append the :func:`hint() <cursor.hint()>` to any cursor (e.g.
377-
query) with the name
378-
379-
TODO: this isn't "the name of an index." I'd say just "with the index." The
380-
name of an index is a string like "zipcode_1".
381-
382-
of an index as the argument to *force* MongoDB
327+
query) with the index as the argument to *force* MongoDB
383328
to use a specific index to fulfill the query. Consider the following
384329
example:
385330

386331
.. code-block:: javascript
387332

388333
db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { zipcode: 1 } )
389334

390-
391335
You can use :func:`hint() <cursor.hint()>` and :func:`explain()
392336
<cursor.explain()>` in conjunction with each other to compare the
393-
effectiveness of a specific index.
337+
effectiveness of a specific index. Specify the ``$natural`` operator
338+
to the :func:`hint() <cursor.hint()>` method to prevent MongoDB from
339+
using *any* index:
340+
341+
.. code-block:: javascript
394342

395-
TODO: mention $natural to force no index usage?
343+
db.people.find( { name: "John Doe", zipcode: { $gt: 63000 } } } ).hint( { $natural: 1 } )
396344

397345
- :status:`indexCounters`
398346

399347
Use the :status:`indexCounters` data in the output of
400348
:dbcommand:`serverStatus` for insight into database-wise index
401349
utilization.
402350

403-
TODO: I'd like to see this also cover how to track how far an index build has
404-
gotten and how to kill an index build.
351+
Monitoring and Controlling Index Building
352+
-----------------------------------------
353+
354+
.. TODO insert links to the values in the inprog array following the
355+
completion of DOCS-162
356+
357+
To see the status of the indexing processes, you can use the
358+
:func:`db.currentOp()` method in the :program:`mongo` shell. The value
359+
of the ``query`` field and the ``msg`` field will indicate if the
360+
operation is an index build. The ``msg`` field also indicates the
361+
percent of the build that is complete.
362+
363+
If you need to terminate an ongoing index build, You can use the
364+
:func:`db.killOP()` method in the :program:`mongo` shell.

0 commit comments

Comments
 (0)