MongoDB 基础系列十八:索引之通过 Background 创建索引

前言

此篇博文是 Mongdb 基础系列之一;

本文为作者的原创作品,转载需注明出处;

背景

默认情况下,创建索引的过程中将会阻止其它的操作;当在某个 collection 上创建索引的时候,该 collection 上的所有读、写操作都会被阻塞直到当前的索引创建成功为止;这样的话,会非常的影响 MongoDB 的查询;所以,为了保证 MongoDB 仍然可以对外正常的提供功能,Background 机制诞生了;

创建 Background

当有一个需要被长时间创建的索引,这个时候,可以考虑使用 background 机制,这样,在创建该索引的过程中,MongoDB 仍然能够尽最大可能保证当前可用;比如,你可以在一个庞大的 pepole collection 上对字段 zipcode 通过 background 的方式创建索引;

1
db.people.createIndex( { zipcode: 1}, {background: true} )

默认情况下,background 取值为 false;

同时,你可以和其它的选项结合起来使用,

1
db.people.createIndex( { zipcode: 1}, {background: true, sparse: true } )

这样,可以在后台创建一个稀疏索引;

特性

Background indexing operations run in the background so that other database operations can run while creating the index. However, the mongo shell session or connection where you are creating the index will block until the index build is complete. To continue issuing commands to the database, open another connection or mongo instance.

在后台创建索引的同时其它操作仍然可以进行;但有一个限制,那就是当前用于创建该索引的 connection 或 session 会被阻塞直到索引创建成功为止;所以,在后台创建索引的过程当中,若要继续对该索引进行操作,需要新建一个链接才可以;

Queries will not use partially-built indexes: the index will only be usable once the index build is complete.

并且在索引没有创建完成以前,不可以使用该索引;

在构建过程中被打断如何处理

如果一个后台创建索引的进程被打断了,比如说 MongoDB 被 killed 掉了;这种情况下可以通过 storage.indexBuildRetry 尝试进行重建动作或者是通过 –noIndexBuildRetry 的方式跳过该索引的创建;

如何在从节点上创建索引

Background index 将会首先在主节点上创建,当主节点上创建成功以后,才会在从节点上进行创建;如果你的确有一个非常大的索引需要在从节点上创建,最佳实践是,以 standalone 的模式启动从节点,并依次在从节点上以后台的模式创建索引;

检查 Background 索引创建过程的状态

To see the status of an index build operation, you can use the db.currentOp() method in the mongo shell. To filter the current operations for index creation operations, see Active Indexing Operations for an example.

The msg field will include the percent of the build that is complete.

终止当前 Background 索引的创建过程

To terminate an ongoing index build, use the db.killOp() method in the mongo shell. For index builds, the effects of db.killOp() may not be immediate and may occur well after much of the index build operation has completed.

You cannot terminate a replicated index build on secondary members of a replica set. To minimize the impact of building an index on replica sets, see Build Indexes on Replica Sets.

References

https://docs.mongodb.com/manual/core/index-creation/