On this page本页内容
This document addresses some common questions regarding MongoDB indexes. 本文讨论了有关MongoDB索引的一些常见问题。For more information on indexes, see Indexes.有关索引的更多信息,请参阅索引。
To create an index on a collection, use the 要在集合上创建索引,请使用db.collection.createIndex()
method. db.collection.createIndex()
方法。Creating an index is an administrative operation. 创建索引是一项管理操作。In general, applications should not call 一般来说,应用程序不应该定期调用db.collection.createIndex()
on a regular basis.db.collection.createIndex()
。
Index builds can impact performance; see How does an index build affect database performance?. 索引构建会影响性能;查看索引生成如何影响数据库性能?。Administrators should consider the performance implications before building indexes.管理员应该在构建索引之前考虑性能含义。
MongoDB index builds against a populated collection require an exclusive read-write lock against the collection. 针对已填充集合的MongoDB索引生成需要针对该集合的独占读写锁。Operations that require a read or write lock on the collection must wait until the 需要对集合设置读或写锁的操作必须等待mongod
releases the lock.mongod
释放锁。
Changed in version 4.2.在版本4.2中更改。
"4.2"
, MongoDB uses an optimized build process that only holds the exclusive lock at the beginning and end of the index build. "4.0"
, the default foreground index build process holds the exclusive lock for the entire index build. background
index builds do nottake an exclusive lock during the build process.For more information on the index build process, see Index Builds on Populated Collections.有关索引生成过程的更多信息,请参阅在填充的集合上生成索引。
Index builds on replica sets have specific performance considerations and risks. 基于副本集的索引构建有特定的性能考虑因素和风险。See Index Builds in Replicated Environments for more information. 有关更多信息,请参阅复制环境中的索引构建。To minimize the impact of building an index on replica sets, including shard replica sets, use a rolling index build procedure as described in Rolling Index Builds on Replica Sets.要将在副本集(包括分片副本集)上建立索引的影响降至最低,请使用在副本集上建立滚动索引中所述的滚动索引建立过程。
To return information on currently running index creation operations, see Active Indexing Operations. 要返回有关当前正在运行的索引创建操作的信息,请参阅活动索引操作。To kill a running index creation operation on a primary or standalone 要终止主mongod
, use db.killOp()
. mongod
或独立mongod
上正在运行的索引创建操作,请使用db.killOp()
。The partially built index will be deleted.部分构建的索引将被删除。
You cannot terminate a replicated index build on secondary members of a replica set. 不能终止副本集辅助成员上的复制索引生成。You must first 必须首先drop
the index on the primary. drop
主目录上的索引。The secondaries will replicate the drop operation and drop the indexes after the index build completes. 在索引构建完成后,二级缓存将复制删除操作并删除索引。All further replication blocks behind the index build and drop.索引生成和删除后的所有进一步复制块。
To list a collection's indexes, use the 要列出集合的索引,请使用db.collection.getIndexes()
method.db.collection.getIndexes()
方法。
To inspect how MongoDB processes a query, use the 要检查MongoDB如何处理查询,请使用explain()
method.explain()
方法。
A number of factors determine which fields to index, including selectivity, the support for multiple query shapes, and size of the index. 许多因素决定了要索引哪些字段,包括选择性、对多个查询形状的支持以及索引的大小。For more information, see Operational Considerations for Indexes and Indexing Strategies.有关更多信息,请参阅索引的操作注意事项和索引策略。
The db.collection.stats()
includes an indexSizes
document which provides size information for each index on the collection.db.collection.stats()
包含一个indexSizes
文档,该文档为集合中的每个索引提供大小信息。
Depending on its size, an index may not fit into RAM. 根据其大小,索引可能不适合RAM。An index fits into RAM when your server has enough RAM available for both the index and the rest of the working set. 当您的服务器有足够的RAM可用于索引和工作集的其余部分时,索引就可以放入RAM中。When an index is too large to fit into RAM, MongoDB must read the index from disk, which is a much slower operation than reading from RAM.当索引太大而无法装入RAM时,MongoDB必须从磁盘读取索引,这比从RAM读取要慢得多。
In certain cases, an index does not need to fit entirely into RAM. 在某些情况下,索引不需要完全适合RAM。For details, see Indexes that Hold Only Recent Values in RAM.有关详细信息,请参阅仅保存RAM中最近值的索引。
Write operations may require updates to indexes:写操作可能需要更新索引:
Therefore, if your application is write-heavy, indexes might affect performance.因此,如果应用程序写得很重,索引可能会影响性能。