2
2
.. _index-creation-operations:
3
3
.. _index-operations:
4
4
5
- ======================
6
- Index Build Operations
7
- ======================
5
+ ================================================
6
+ Index Build Operations on a Populated Collection
7
+ ================================================
8
8
9
9
.. default-domain:: mongodb
10
10
@@ -14,24 +14,32 @@ Index Build Operations
14
14
:depth: 1
15
15
:class: singlecol
16
16
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.
23
24
24
25
.. index:: index; background creation
25
26
.. _index-creation-background:
26
27
27
28
Background Construction
28
29
-----------------------
29
30
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:
35
43
36
44
.. code-block:: javascript
37
45
@@ -49,13 +57,6 @@ following:
49
57
Behavior
50
58
~~~~~~~~
51
59
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
-
59
60
Background indexing operations run in the background so that other database
60
61
operations can run while creating the index. However, the :program:`mongo`
61
62
shell session or connection where you are creating
@@ -75,7 +76,6 @@ usable once the index build is complete.
75
76
:dbcommand:`compact`. These operations will return an error during
76
77
background index builds.
77
78
78
-
79
79
Performance
80
80
~~~~~~~~~~~
81
81
@@ -84,18 +84,9 @@ slower than the normal "foreground" index builds. If the index is
84
84
larger than the available RAM, then the incremental process can take
85
85
*much* longer than the foreground build.
86
86
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.
99
90
100
91
.. versionchanged:: 3.4
101
92
@@ -116,43 +107,35 @@ To start the :program:`mongod` after a failed index build, use the
116
107
117
108
.. _index-creation-building-indexes-on-secondaries:
118
109
119
- Building Indexes on Secondaries
120
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110
+ Build Indexes on Replica Sets and Sharded Clusters
111
+ --------------------------------------------------
121
112
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`.
126
116
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:
131
118
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.
135
124
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.
138
128
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.
146
132
147
133
The amount of time required to build the index on a secondary must be
148
134
within the window of the :term:`oplog`, so that the secondary can
149
135
catch up with the primary.
150
136
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
-
154
137
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 .
156
139
157
140
.. index:: index; name
158
141
.. _index-names:
@@ -206,15 +189,11 @@ method in the :program:`mongo` shell. For index builds, the effects of
206
189
:method:`db.killOp()` may not be immediate and may occur well after
207
190
much of the index build operation has completed.
208
191
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`.
214
196
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.
218
197
219
198
.. seealso::
220
199
:method:`db.currentOp()`, :method:`db.killOp()`
0 commit comments