On this page本页内容
Starting in MongoDB 4.2, index builds use an optimized build process that holds an exclusive lock on the collection at the beginning and end of the index build. 从MongoDB 4.2开始,索引构建使用优化的构建过程,在索引构建的开始和结束时对集合持有独占锁。The rest of the build process yields to interleaving read and write operations. 构建过程的其余部分将进行交叉读写操作。For a detailed description of index build process and locking behavior, see Index Build Process.有关索引生成过程和锁定行为的详细描述,请参阅索引生成过程。
Starting in MongoDB 4.4, index builds on a replica set or sharded cluster build simultaneously across all data-bearing replica set members. 从MongoDB 4.4开始,索引构建在副本集上,或者在所有数据承载副本集成员之间同时构建分片集群。The primary requires a minimum number of data-bearing voting members (i.e. commit quorum), including itself, that must complete the build before marking the index as ready for use. primary需要最少数量的数据承载投票成员(即“提交法定人数”),包括它自己,这些成员必须在将索引标记为可供使用之前完成构建。A “voting” member is any replica set member where “投票”成员是members[n].votes
is greater than 0
. members[n].votes
大于0
的任何复制集成员。See Index Builds in Replicated Environments for more information.有关更多信息,请参阅复制环境中的索引构建。
Previous versions of MongoDB supported building indexes either in the foreground or background. 早期版本的MongoDB支持在前台或后台构建索引。Foreground index builds were fast and produced more efficient index data structures, but required blocking all read-write access to the parent database of the collection being indexed for the duration of the build. 前台索引构建速度很快,生成了更高效的索引数据结构,但需要在构建期间阻止对被索引集合的父数据库的所有读写访问。Background index builds were slower and had less efficient results, but allowed read-write access to the database and its collections during the build process.后台索引构建速度较慢,结果效率较低,但在构建过程中允许对数据库及其集合进行读写访问。
Starting in MongoDB 4.2, index builds obtain an exclusive lock on only the collection being indexed during the start and end of the build process to protect metadata changes. 从MongoDB 4.2开始,索引构建在构建过程的开始和结束期间仅对被索引的集合获得独占锁,以保护元数据更改。The rest of the build process uses the yielding behavior of background index builds to maximize read-write access to the collection during the build. 构建过程的其余部分使用后台索引构建的屈服行为,以在构建过程中最大化对集合的读写访问。4.2 index builds still produce efficient index data structures despite the more permissive locking behavior.4.2索引构建仍然会生成高效的索引数据结构,尽管锁定行为更加允许。
The optimized index build performance is at least on par with background index builds. 优化的索引构建性能至少与背景索引构建一致。For workloads with few or no updates received during the build process, optimized index builds can be as fast as a foreground index build on that same data.对于在构建过程中很少或没有收到更新的工作负载,优化的索引构建可以与在相同数据上构建前台索引一样快。
Use 使用db.currentOp()
to monitor the progress of ongoing index builds.db.currentOp()
监视正在进行的索引生成的进度。
MongoDB ignores the 如果将后台索引构建选项指定给background
index build option if specified to createIndexes
or its shell helpers createIndex()
and createIndexes()
.createIndexes
或其shell帮助程序createIndex()
和createIndexes()
,MongoDB将忽略该选项。
For indexes that enforce constraints on the collection, such as unique indexes, the 对于对集合强制执行约束的索引,例如唯一索引,mongod
checks all pre-existing and concurrently-written documents for violations of those constraints after the index build completes. mongod
会在索引构建完成后检查所有预先存在的和同时编写的文档是否违反这些约束。Documents that violate the index constraints can exist during the index build. 索引生成期间,可能存在违反索引约束的文档。If any documents violate the index constraints at the end of the build, the 如果任何文档在构建结束时违反了索引约束,mongod
terminates the build and throws an error.mongod
将终止构建并抛出错误。
For example, consider a populated collection 例如,考虑一个填充的集合inventory
. inventory
。An administrator wants to create a unique index on the 管理员希望在product_sku
field. product_sku
字段上创建唯一索引。If any documents in the collection have duplicate values for 如果集合中的任何文档的product_sku
, the index build can still start successfully. product_sku
值重复,则索引生成仍然可以成功启动。If any violations still exist at the end of the build, the 如果在构建结束时仍然存在任何冲突,mongod
terminates the build and throws an error.mongod
将终止构建并抛出错误。
Similarly, an application can successfully write documents to the 类似地,在索引构建过程中,应用程序可以成功地向inventory
collection with duplicate values of product_sku
while the index build is in progress. inventory
集合写入具有重复product_sku
值的文档。If any violations still exist at the end of the build, the 如果在构建结束时仍然存在任何冲突,mongod
terminates the build and throws an error.mongod
将终止构建并抛出错误。
To mitigate the risk of index build failure due to constraint violations:为了降低因违反约束而导致索引生成失败的风险:
For a sharded collection distributed across multiple shards, one or more shards may contain a chunk with duplicate documents. 对于分布在多个碎片上的碎片集合,一个或多个碎片可能包含包含重复文档的区块。As such, the create index operation may succeed on some of the shards (i.e. the ones without duplicates) but not on others (i.e. the ones with duplicates). 因此,创建索引操作可能会在一些碎片(即没有重复的碎片)上成功,但在其他碎片(即有重复的碎片)上不会成功。To avoid leaving inconsistent indexes across shards, you can issue the 为了避免在碎片之间留下不一致的索引,可以从db.collection.dropIndex()
from a mongos
to drop the index from the collection.mongos
发出db.collection.dropIndex()
命令,从集合中删除索引。
To mitigate the risk of this occurrence, before creating the index:为了降低发生这种情况的风险,在创建索引之前:
Building indexes during time periods where the target collection is under heavy write load can result in reduced write performance and longer index builds.在目标集合处于重写负载的时间段内构建索引可能会导致写性能降低和索引构建时间延长。
Consider designating a maintenance window during which applications stop or reduce write operations against the collection. 考虑指定一个维护窗口,在该窗口中应用程序停止或减少对集合的写入操作。Start the index build during this maintenance window to mitigate the potential negative impact of the build process.在此维护窗口期间启动索引生成,以减轻生成过程的潜在负面影响。
createIndexes
supports building one or more indexes on a collection. 支持在集合上构建一个或多个索引。createIndexes
uses a combination of memory and temporary files on disk to complete index builds. 使用磁盘上的内存和临时文件组合来完成索引构建。The default limit on memory usage for createIndexes
is 200 megabytes (for versions 4.2.3 and later) and 500 (for versions 4.2.2 and earlier), shared between all indexes built using a single createIndexes
command. createIndexes
的默认内存使用限制为200 MB(对于4.2.3及更高版本)和500 MB(对于4.2.2及更低版本),在使用单个createIndexes
命令构建的所有索引之间共享。Once the memory limit is reached, 一旦达到内存限制,createIndexes
uses temporary disk files in a subdirectory named _tmp
within the --dbpath
directory to complete the build.createIndexes
将使用--dbpath
目录中名为_tmp
的子目录中的临时磁盘文件来完成构建。
You can override the memory limit by setting the 通过设置maxIndexBuildMemoryUsageMegabytes
server parameter. maxIndexBuildMemoryUsageMegabytes
服务器参数,可以覆盖内存限制。Setting a higher memory limit may result in faster completion of index builds. 设置更高的内存限制可能会更快地完成索引构建。However, setting this limit too high relative to the unused RAM on your system can result in memory exhaustion and server shutdown.但是,相对于系统上未使用的RAM,将此限制设置得过高可能会导致内存耗尽和服务器关闭。
If the host machine has limited available free RAM, you may need to schedule a maintenance period to increase the total system RAM before you can modify the 如果主机的可用RAM有限,在修改mongod
RAM usage.mongod
RAM使用之前,您可能需要安排一个维护期来增加系统RAM总量。
Requires featureCompatibilityVersion
4.4+
Each 副本集或分片集群中的每个mongod
in the replica set or sharded cluster must have featureCompatibilityVersion set to at least 4.4
to start index builds simultaneously across replica set members.mongod
必须将featureCompatibilityVersion设置为至少4.4
,才能在副本集成员之间同时启动索引构建。
MongoDB 4.4 running MongoDB 4.4运行featureCompatibilityVersion: "4.2"
builds indexes on the primary before replicating the index build to secondaries.featureCompatibilityVersion: "4.2"
将在把索引构建复制到辅助库之前在主库上构建索引。
Starting with MongoDB 4.4, index builds on a replica set or sharded cluster build simultaneously across all data-bearing replica set members. 从MongoDB 4.4开始,索引构建在副本集上,或者在所有数据承载副本集成员之间同时构建分片集群。For sharded clusters, the index build occurs only on shards containing data for the collection being indexed. 对于分片集群,索引构建仅发生在包含被索引集合的数据的分片上。The primary requires a minimum number of data-bearing primary需要最少数量的数据承载voting
members (i.e commit quorum), including itself, that must complete the build before marking the index as ready for use.voting
成员(即“提交法定人数”),包括其自身,这些成员必须在将索引标记为可供使用之前完成构建。
The build process is summarized as follows:构建过程总结如下:
createIndexes
command and immediately creates a “startIndexBuild” oplog entry associated with the index build.createIndexes
命令,并立即创建与索引生成关联的“startIndexBuild”oplog条目。If the secondaries instead replicate an “abortIndexBuild” oplog entry, they abort the index build and discard the build job.如果二级复制“abortIndexBuild”oplog条目,它们将中止索引生成并放弃生成作业。
For sharded clusters, the index build occurs only on shards containing data for the collection being indexed.对于分片集群,索引构建仅发生在包含被索引集合的数据的分片上。
For a more detailed description of the index build process, see Index Build Process.有关索引生成过程的更详细描述,请参阅索引生成过程。
By default, index builds use a commit quorum of 默认情况下,索引生成使用"votingMembers"
, or all data-bearing voting members. "votingMembers"
的提交法定人数,或所有包含数据的投票成员。To start an index build with a non-default commit quorum, specify the commitQuorum parameter to 要使用非默认提交仲裁启动索引生成,请将commitQuorum参数指定给createIndexes
or its shell helpers db.collection.createIndex()
and db.collection.createIndexes()
.createIndexes
或其shell帮助程序db.collection.createIndexes()
和dbcollectioncreateIndexes()
。
To modify the commit quorum required for an in-progress simultaneous index build, use the 要修改正在进行的同步索引生成所需的提交仲裁,请使用setIndexCommitQuorum
command.setIndexCommitQuorum
命令。
Note
Index builds can impact replica set performance. 索引生成可能会影响副本集的性能。For workloads which cannot tolerate performance decrease due to index builds, consider performing a rolling index build process. 对于无法容忍因索引生成而导致性能降低的工作负载,请考虑执行滚动索引生成过程。Rolling index builds take at most one replica set member out at a time, starting with the secondary members, and builds the index on that member as a standalone. 滚动索引生成一次最多提取一个副本集成员,从次要成员开始,并将索引作为独立成员在该成员上生成。Rolling index builds require at least one replica set election.滚动索引生成需要至少选择一个副本集。
mongod
mongod
上¶If the primary 如果主mongod
shuts down during the index build, the build progress is lost. mongod
在索引生成期间关闭,则生成进度将丢失。The mongod
automatically recovers the index build and restarts it from the beginning.mongod
会自动恢复索引构建,并从头开始重新启动。
mongod
mongod
上¶If a secondary shuts down during the index build, the index build job is persisted. 如果在索引生成过程中关闭了辅助系统,则索引生成作业将被持久化。Restarting the 重新启动mongod
automatically recovers the index build and restarts it from the beginning.mongod
会自动恢复索引生成,并从头开始重新启动。
Prior to MongoDB 4.4, the startup process stalls behind any recovered index builds. 在MongoDB 4.4之前,启动过程会在任何恢复的索引构建之后暂停。The secondary could fall out of sync with the replica set and require resynchronization. 辅助服务器可能与副本集不同步,需要重新同步。Starting in MongoDB 4.4, the 从MongoDB 4.4开始,mongod
can perform the startup process while the recovering index builds.mongod
可以在构建恢复索引的同时执行启动过程。
If you restart the 如果以独立方式重新启动mongod
as a standalone (i.e. removing or commenting out replication.replSetName
or omitting --replSetName
), the mongod
cannot restart the index build. mongod
(即删除或注释掉replication.replSetName
或省略--replSetName
),mongod
将无法重新启动索引构建。The build remains in a paused state until it is manually 生成将保持暂停状态,直到手动删除(dropped
.dropped
)。
mongod
mongod
上¶If the 如果mongod
shuts down during the index build, the index build job and all progress is lost. mongod
在索引构建期间关闭,则索引构建作业和所有进度都将丢失。Restarting the 重新启动mongod
does not restart the index build. mongod
不会重新启动索引生成。You must re-issue the 必须重新发出createIndex()
operation to restart the index build.createIndex()
操作才能重新启动索引生成。
Starting in version 4.4, MongoDB can pause an in-progress index build to perform a rollback.从4.4版开始,MongoDB可以暂停正在进行的索引构建以执行回滚。
Prior to MongoDb 4.4, rollbacks could start only after all in-progress index builds finished.在MongoDb 4.4之前,只有在所有进行中的索引构建完成后才能开始回滚。
A sharded collection has an inconsistent index if the collection does not have the exact same indexes (including the index options) on each shard that contains chunks for the collection. 如果一个分片集合在包含集合块的每个分片上没有完全相同的索引(包括索引选项),则该分片集合的索引不一致。Although inconsistent indexes should not occur during normal operations, inconsistent indexes can occur, such as:虽然正常操作期间不应出现不一致的索引,但也可能出现不一致的索引,例如:
unique
key constraint and one shard contains a chunk with duplicate documents. Starting in MongoDB 4.4 (and in MongoDB 4.2.6), the config server primary periodically checks for index inconsistencies across the shards for sharded collections. 从MongoDB 4.4(和MongoDB 4.2.6)开始,配置服务器主服务器会定期检查碎片集合中碎片之间的索引不一致。To configure these periodic checks, see 要配置这些定期检查,请参阅启用enableShardedIndexConsistencyCheck
and shardedIndexConsistencyCheckIntervalMS
.enableShardedIndexConsistencyCheck
和shardedIndexConsistencyCheckIntervalMS
。
The command 命令serverStatus
returns the field shardedIndexConsistency
to report on index inconsistencies when run on the config server primary.serverStatus
返回字段shardedIndexConsistency
,以报告在配置服务器主服务器上运行时的索引不一致。
To check if a sharded collection has inconsistent indexes, see Find Inconsistent Indexes across Shards.要检查分片集合是否有不一致的索引,请参阅在分片之间查找不一致的索引。
To see the status of an index build operation, you can use the 要查看索引生成操作的状态,可以在db.currentOp()
method in the mongo
shell. mongo
shell中使用db.currentOp()
方法。To filter the current operations for index creation operations, see Active Indexing Operations for an example.要筛选索引创建操作的当前操作,请参阅活动索引操作的示例。
The msg
field includes a percentage-complete measurement of the current stage in the index build process.msg
字段包括索引构建过程中当前阶段的完整测量百分比。
Use the 使用dropIndexes
command or its shell helpers dropIndex()
or dropIndexes()
to terminate an in-progress index build. dropIndexes
命令或其shell助手dropIndex()
或dropIndexes()
终止正在进行的索引生成。See Abort In-Progress Index Builds for more information.有关更多信息,请参阅中止进行中的索引生成。
Do not use 不要使用killOp
to terminate an in-progress index builds in replica sets or sharded clusters.killOp
终止副本集或分片集群中正在进行的索引生成。
The following table describes each stage of the index build process:下表描述了索引构建过程的每个阶段:
Lock | mongod obtains an exclusive X lock on the the collection being indexed. mongod 在被索引的集合上获得独占X锁。mongod does not yield this lock.mongod 不会释放这个锁。 |
Initialization |
|
Lock | mongod downgrades the exclusive X collection lock to an intent exclusive IX lock. mongod 将独占X 集合锁降级为意向独占IX 锁。mongod periodically yields this lock to interleaving read and write operations.mongod 周期性地将该锁产生给交错读写操作。 |
| |
Process Side Writes Table |
|
| |
Lock | mongod upgrades the intent exclusive IX lock on the collection to a shared S lock. mongod 将集合上的意向排斥IX 锁升级为共享S 锁。 |
| |
Lock | mongod upgrades the shared S lock on the collection to an exclusive X lock on the collection. mongod 将集合上的共享S 锁升级为集合上的独占X 锁。mongod does not yield this lock.mongod 不会释放这个锁。 |
| |
| |
mongod updates the index metadata to mark the index as ready for use.mongod 更新索引元数据,将索引标记为可供使用。 | |
Lock | mongod releases the X lock on the collection.mongod 会释放集合上的X锁。 |
See also参阅