Indexing as a Background Operation

Prior to 2.1.0, slaves and replica secondaries build indexes in the foreground even if background:true is specified. The slave/secondary will block queries while the index builds on it. Indexing on the secondaries begins after the index is completely built on the primary.

v1.4+

By default the ensureIndex() operation is blocking, and stops other operations on the database from proceeding until completed.

To build an index in the background, add background:true to your index options. Examples:

> db.things.ensureIndex({x:1}, {background:true});
> db.things.ensureIndex({name:1}, {background:true, unique:true,
... dropDups:true});

Prior to 2.1.0, the index build operation is not a background build when it replicates to secondaries. This could be a significant issue if you are reading from your secondaries – in this case back up each replica set member one by one – see the doc page for this.

With background mode enabled, other non-indexing operations, including writes, will not be obstructed during index creation. The index is not used for queries until the build is complete.  Further calls to ensureIndex() while the background indexing is progressing will fail with a message indicating a background operation is in progress.

Although the operation is 'background' in the sense that other operations may run concurrently, the command will not return to the shell prompt until completely finished. To do other operations at the same time, open a separate mongo shell instance.

Please note that background mode building uses an incremental approach to building the index which is slower than the default foreground mode: time to build the index will be greater. If the resulting index is larger than RAM, background indexing can be much slower.

While the index build progresses, it is possible to see that the operation is still in progress with the db.currentOp() command (will be shown as an insert to system.indexes).  You may also use db.killOp() to terminate the index build on the primary.

While the build progresses, the index is visible in system.indexes, but it is not used for queries until building completes.

Notes

  • Only one index build at a time, whether foreground or background, is permitted per collection.
  • Some administrative operations, such as repairDatabase, are disallowed while a background indexing job is in progress.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

PLEASE POST QUESTIONS IN THE USER GROUPS FORUM. Post non-question comments and helpful hints here.

blog comments powered by Disqus