Skip to content

Commit 5bf9672

Browse files
committed
DOCSP-1557: index builds and replica sets/sharded clusters
1 parent 698280b commit 5bf9672

File tree

6 files changed

+119
-126
lines changed

6 files changed

+119
-126
lines changed

source/core/index-creation.txt

Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
.. _index-creation-operations:
33
.. _index-operations:
44

5-
======================
6-
Index Build Operations
7-
======================
5+
================================================
6+
Index Build Operations on a Populated Collection
7+
================================================
88

99
.. default-domain:: mongodb
1010

@@ -14,24 +14,32 @@ Index Build Operations
1414
:depth: 1
1515
:class: singlecol
1616

17-
By default, creating an index blocks all other operations on a
18-
database. When building an index on a collection, the database that
19-
holds the collection is unavailable for read or write operations until
20-
the index build completes. Any operation that requires a read or write
21-
lock on all databases (e.g. :command:`listDatabases`) will wait for the
22-
foreground index build to complete.
17+
By default, creating an index on a populated collection blocks all
18+
other operations on a database. When building an index on a populated
19+
collection, the database that holds the collection is unavailable for
20+
read or write operations until the index build completes. Any operation
21+
that requires a read or write lock on all databases (e.g.
22+
:command:`listDatabases`) will wait for the foreground index build to
23+
complete.
2324

2425
.. index:: index; background creation
2526
.. _index-creation-background:
2627

2728
Background Construction
2829
-----------------------
2930

30-
For potentially long running index building operations, consider the
31-
``background`` operation so that the MongoDB database remains available
32-
during the index building operation. For example, to create an index in
33-
the background of the ``zipcode`` field of the ``people`` collection,
34-
issue the following:
31+
.. note::
32+
33+
The following section refers to building indexes on a standalone.
34+
For a replica set or a sharded cluster, use a rolling index build.
35+
See :doc:`/tutorial/build-indexes-on-replica-sets` for details.
36+
37+
For potentially long running index building operations on standalone
38+
deployments, consider the ``background`` option so that the MongoDB
39+
database remains available during the index building operation.
40+
41+
For example, to create an index in the background of the ``zipcode``
42+
field of the ``people`` collection, issue the following:
3543

3644
.. code-block:: javascript
3745

@@ -49,13 +57,6 @@ following:
4957
Behavior
5058
~~~~~~~~
5159

52-
As of MongoDB version 2.4, a :program:`mongod` instance can build more
53-
than one index in the background concurrently.
54-
55-
.. versionchanged:: 2.4
56-
Before 2.4, a :program:`mongod` instance could only build one
57-
background index per database at a time.
58-
5960
Background indexing operations run in the background so that other database
6061
operations can run while creating the index. However, the :program:`mongo`
6162
shell session or connection where you are creating
@@ -75,7 +76,6 @@ usable once the index build is complete.
7576
:dbcommand:`compact`. These operations will return an error during
7677
background index builds.
7778

78-
7979
Performance
8080
~~~~~~~~~~~
8181

@@ -84,18 +84,9 @@ slower than the normal "foreground" index builds. If the index is
8484
larger than the available RAM, then the incremental process can take
8585
*much* longer than the foreground build.
8686

87-
If your application
88-
includes :method:`~db.collection.createIndex()`
89-
operations, and an index *doesn't* exist for other operational
90-
concerns, building the index can have a severe impact on the
91-
performance of the database.
92-
93-
To avoid performance issues, make sure that your application checks
94-
for the indexes at start up using the
95-
:method:`~db.collection.getIndexes()` method or the :api:`equivalent
96-
method for your driver <>` and terminates if the proper indexes do not
97-
exist. Always build indexes in production instances using separate
98-
application code, during designated maintenance windows.
87+
Building an index can have a severe impact on the performance of the
88+
database. If possible, build indexes during designated maintenance
89+
windows.
9990

10091
.. versionchanged:: 3.4
10192

@@ -116,43 +107,35 @@ To start the :program:`mongod` after a failed index build, use the
116107

117108
.. _index-creation-building-indexes-on-secondaries:
118109

119-
Building Indexes on Secondaries
120-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110+
Build Indexes on Replica Sets and Sharded Clusters
111+
--------------------------------------------------
121112

122-
.. versionchanged:: 2.6
123-
Secondary members can now build indexes in the
124-
background. Previously all index builds on secondaries were in the
125-
foreground.
113+
To minimize the impact of building an index on replica sets and sharded
114+
clusters with replica set shards, use a rolling index build procedure
115+
as described on :doc:`/tutorial/build-indexes-on-replica-sets`.
126116

127-
A foreground index build on a :term:`primary` requires a global DB lock. It
128-
replicates as a foreground index build on :term:`replica set`
129-
:term:`secondaries <secondary>`, and the replication worker takes a global DB
130-
lock that queues reads and writes to all databases on the indexing server.
117+
If not using the rolling index build procedure:
131118

132-
A background index build on a primary replicates as background index builds on
133-
secondaries. The replication worker does not take a global DB lock, and
134-
secondary reads are not affected.
119+
- A foreground index build on a :term:`primary` requires a DB
120+
lock. It replicates as a foreground index build on :term:`replica
121+
set` :term:`secondaries <secondary>`, and the replication worker
122+
takes a global DB lock that queues reads and writes to all databases
123+
on the indexing server.
135124

136-
In both cases, index operations on replica set secondaries begin after the
137-
primary finishes building the index.
125+
- A background index build on a primary replicates as background index
126+
builds on secondaries. The replication worker does not take a global
127+
DB lock, and secondary reads are not affected.
138128

139-
To build large indexes on secondaries the best approach is to
140-
restart one secondary at a time in :term:`standalone` mode and build
141-
the index. After building the index, restart as a member of the
142-
replica set, allow it to catch up with the other members of the set,
143-
and then build the index on the next secondary. When all the
144-
secondaries have the new index, step down the primary, restart it as a
145-
standalone, and build the index on the former primary.
129+
- For both foreground and background index builds on the primary, the
130+
index operations on replica set secondaries begin after the primary
131+
finishes building the index.
146132

147133
The amount of time required to build the index on a secondary must be
148134
within the window of the :term:`oplog`, so that the secondary can
149135
catch up with the primary.
150136

151-
Indexes on secondary members in "recovering" mode are always built in
152-
the foreground to allow them to catch up as soon as possible.
153-
154137
See :ref:`index-building-replica-sets` for a complete procedure for
155-
building indexes on secondaries.
138+
building indexes on replica sets and sharded clusters.
156139

157140
.. index:: index; name
158141
.. _index-names:
@@ -206,15 +189,11 @@ method in the :program:`mongo` shell. For index builds, the effects of
206189
:method:`db.killOp()` may not be immediate and may occur well after
207190
much of the index build operation has completed.
208191

209-
You cannot terminate a *replicated* index build on secondary members
210-
of a replica set. To minimize the impact of building an index on
211-
replica sets, see :doc:`/tutorial/build-indexes-on-replica-sets`.
212-
213-
.. versionchanged:: 2.4
192+
You cannot terminate a *replicated* index build on secondary members of
193+
a replica set. To minimize the impact of building an index on replica
194+
sets and sharded clusters with replica set shards, see
195+
:doc:`/tutorial/build-indexes-on-replica-sets`.
214196

215-
Before MongoDB 2.4, you could *only* terminate *background* index
216-
builds. After 2.4, you can terminate both *background* index builds
217-
and foreground index builds.
218197

219198
.. seealso::
220199
:method:`db.currentOp()`, :method:`db.killOp()`

source/includes/fact-index-build-memory-limit.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ only one collection at a time and has no risk of exceeding the memory
1010
limit. However, it is possible for a user to start foreground index
1111
builds on multiple collections in multiple databases simultaneously
1212
and potentially consume an amount of memory greater than the limit
13-
set in :parameter:`maxIndexBuildMemoryUsageMegabytes`.
13+
set in :parameter:`maxIndexBuildMemoryUsageMegabytes`.
14+
15+
.. tip::
16+
17+
To minimize the impact of building an index on replica sets and
18+
sharded clusters with replica set shards, use a rolling index build
19+
procedure as described on
20+
:doc:`/tutorial/build-indexes-on-replica-sets`.

source/reference/command/createIndexes.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,27 @@ Definition
6363
Considerations
6464
--------------
6565

66+
Index Names
67+
~~~~~~~~~~~
68+
6669
An index name, including the :term:`namespace`, cannot be longer than
6770
the :ref:`Index Name Length <limit-index-name-length>` limit.
6871

72+
Replica Sets and Sharded Clusters
73+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74+
75+
To minimize the impact of building an index on replica sets and sharded
76+
clusters, use a rolling index build procedure
77+
as described on :doc:`/tutorial/build-indexes-on-replica-sets`.
78+
6979
Behavior
7080
--------
7181

7282
Concurrency
7383
~~~~~~~~~~~
7484

75-
Non-background indexing operations block all other operations on a
76-
database.
85+
Foreground indexing operations on a populated collection block all
86+
other operations on a database.
7787

7888
Multiple Index Builds
7989
~~~~~~~~~~~~~~~~~~~~~

source/reference/method/db.collection.createIndex.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ Definition
1919

2020
.. versionchanged:: 3.2
2121

22-
Starting in MongoDB 3.2, MongoDB disallows the creation of
23-
:ref:`version 0 <3.2-version-0-indexes>` indexes. To upgrade
24-
existing version 0 indexes, see :ref:`3.2-version-0-indexes`.
22+
MongoDB disallows the creation of :ref:`version 0
23+
<3.2-version-0-indexes>` indexes. To upgrade existing version 0
24+
indexes, see :ref:`3.2-version-0-indexes`.
2525

2626
.. include:: /includes/apiargs/method-db.collection.createIndex-param.rst
2727

28+
To minimize the impact of building an index on replica sets and
29+
sharded clusters, use a rolling index build procedure as described
30+
on :doc:`/tutorial/build-indexes-on-replica-sets`.
31+
2832
.. _ensureIndex-options:
2933
.. _createIndex-options:
3034

@@ -119,6 +123,7 @@ described here.
119123

120124
Foreground index builds block all other operations on the database.
121125

126+
122127
.. include:: /includes/extracts/createIndex-behavior.rst
123128

124129
- .. include:: /includes/fact-index-key-length-operation-behaviors.rst

source/reference/method/db.collection.createIndexes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Definition
2121

2222
.. include:: /includes/apiargs/method-db.collection.createIndexes-param.rst
2323

24+
To minimize the impact of building an index on replica sets and sharded
25+
clusters, use a rolling index build procedure
26+
as described on :doc:`/tutorial/build-indexes-on-replica-sets`.
27+
2428
.. _createIndexes-options:
2529

2630
Options

source/tutorial/build-indexes-on-replica-sets.txt

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,43 @@ Build Indexes on Replica Sets
1515
:depth: 1
1616
:class: singlecol
1717

18-
For replica sets, secondaries will begin building indexes *after* the
19-
:term:`primary` finishes building the index. In :term:`sharded clusters
20-
<sharded cluster>`, the :program:`mongos` will send :method:`createIndex()
21-
<db.collection.createIndex()>` to the primary members of the replica
22-
set for each shard, which then replicate to the secondaries after the
23-
primary finishes building the index.
24-
25-
To minimize the impact of building an index on your replica set, you
26-
can use the following procedure to build indexes.
18+
To minimize the impact of building an index on replica sets and sharded
19+
clusters with replica set shards, use the following procedure to build
20+
indexes in a rolling fashion.
2721

22+
This procedure *does* take one member out of the replica set at a time.
23+
However, this procedure will only affect one member of the set at a
24+
time rather than *all* secondaries at the same time.
2825

2926
Considerations
3027
--------------
3128

32-
.. important::
29+
Unique Indexes
30+
~~~~~~~~~~~~~~
3331

34-
To create :ref:`unique indexes <index-type-unique>` using the
35-
following procedure, you must stop all writes to the collection
36-
during this procedure.
32+
To create :ref:`unique indexes <index-type-unique>` using the following
33+
procedure, you must stop all writes to the collection during this
34+
procedure.
3735

38-
If you cannot stop all writes to the collection during this
39-
procedure, do not use the procedure on this page. Instead, build
40-
your unique index on the collection by:
36+
If you cannot stop all writes to the collection during this procedure,
37+
do not use the procedure on this page. Instead, build your unique index
38+
on the collection by:
4139

42-
- issuing :method:`db.collection.createIndex()` on the primary for a
43-
replica set, or
40+
- issuing :method:`db.collection.createIndex()` on the primary for a
41+
replica set, or
4442

45-
- issuing :method:`db.collection.createIndex()` on the
46-
:program:`mongos` for a sharded cluster.
43+
- issuing :method:`db.collection.createIndex()` on the
44+
:program:`mongos` for a sharded cluster.
4745

48-
- Ensure that your :term:`oplog` is large enough to permit the
49-
indexing or re-indexing operation to complete without falling
50-
too far behind to catch up. See the :ref:`oplog sizing
51-
<replica-set-oplog-sizing>` documentation for additional
52-
information.
5346

54-
- This procedure *does* take one member out of the replica set at a
55-
time. However, this procedure will only affect one member of the
56-
set at a time rather than *all* secondaries at the same time.
47+
Oplog Size
48+
~~~~~~~~~~
49+
50+
Ensure that your :term:`oplog` is large enough to permit the indexing
51+
or re-indexing operation to complete without falling too far behind to
52+
catch up. See the :ref:`oplog sizing <replica-set-oplog-sizing>`
53+
documentation for additional information.
54+
5755

5856
Procedure
5957
---------
@@ -129,41 +127,31 @@ Modify the port number (e.g. ``27017``) or the replica set name
129127

130128
Allow replication to catch up on this member.
131129

132-
Build Indexes on all Secondaries
133-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130+
Repeat the Procedure for the Remaining Secondaries
131+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134132

135-
For each secondary in the set, build an index according to the
136-
following steps:
133+
Once the member catches up with the other members of the set, repeat
134+
the procedure one member at a time for the remaining secondary members:
137135

138136
#. :ref:`tutorial-index-on-replica-sets-stop-one-member`
137+
139138
#. :ref:`tutorial-index-on-replica-sets-build-index`
139+
140140
#. :ref:`tutorial-index-on-replica-sets-restart-mongod`
141141

142142
Build the Index on the Primary
143143
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144144

145-
To build an index on the primary you can either:
146-
147-
#. :ref:`Build the index in the background
148-
<index-creation-background>` on the primary.
149-
150-
#. Step down the primary using the :method:`rs.stepDown()` method in
151-
the :program:`mongo` shell to cause the current primary to become a
152-
secondary graceful and allow the set to elect another member as
153-
primary.
145+
When all the secondaries have the new index, step down the primary,
146+
restart it as a standalone, and build the index on the former primary:
154147

155-
Then repeat the index building procedure, listed below, to build the
156-
index on the primary:
148+
#. Use the :method:`rs.stepDown()` method in the :program:`mongo` shell
149+
to step down the primary. Upon successful stepdown, the current primary
150+
becomes a secondary and the replica set members elect a new primary.
157151

158-
#. :ref:`tutorial-index-on-replica-sets-stop-one-member`
152+
#. :ref:`tutorial-index-on-replica-sets-stop-one-member`
159153

160-
#. :ref:`tutorial-index-on-replica-sets-build-index`
154+
#. :ref:`tutorial-index-on-replica-sets-build-index`
161155

162-
#. :ref:`tutorial-index-on-replica-sets-restart-mongod`
156+
#. :ref:`tutorial-index-on-replica-sets-restart-mongod`
163157

164-
Building the index on the background, takes longer than the foreground
165-
index build and results in a less compact index structure. Additionally,
166-
the background index build may impact write performance on the
167-
primary. However, building the index in the background allows the set to
168-
be continuously up for write operations while MongoDB builds the
169-
index.

0 commit comments

Comments
 (0)