FAQ: Indexes常见问题解答:索引

On this page本页内容

This document addresses some common questions regarding MongoDB indexes. 本文讨论了有关MongoDB索引的一些常见问题。For more information on indexes, see Indexes.有关索引的更多信息,请参阅索引

How do I create an index?如何创建索引?

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()

Note注意

Index builds can impact performance; see How does an index build affect database performance?. 索引构建会影响性能;查看索引生成如何影响数据库性能?Administrators should consider the performance implications before building indexes.管理员应该在构建索引之前考虑性能含义。

How does an index build affect database performance?索引构建如何影响数据库性能?

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中更改

  • For feature compatibility version (fcv) "4.2", MongoDB uses an optimized build process that only holds the exclusive lock at the beginning and end of the index build. 对于功能兼容性版本(fcv)“4.2”,MongoDB使用了一个优化的构建过程,它只在索引构建的开始和结束时持有独占锁。The rest of the build process yields to interleaving read and write operations.构建过程的其余部分将进行交叉读写操作。
  • For feature compatibility version (fcv) "4.0", the default foreground index build process holds the exclusive lock for the entire index build. 对于功能兼容性版本“4.0”,默认的前台索引生成过程对整个索引生成持有独占锁。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.索引生成和删除后的所有进一步复制块。

How do I see what indexes exist on a collection?如何查看集合上存在哪些索引?

To list a collection's indexes, use the db.collection.getIndexes() method.要列出集合的索引,请使用db.collection.getIndexes()方法。

How can I see if a query uses an index?如何查看查询是否使用索引?

To inspect how MongoDB processes a query, use the explain() method.要检查MongoDB如何处理查询,请使用explain()方法。

How do I determine which fields to index?如何确定要索引的字段?

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.有关更多信息,请参阅索引的操作注意事项索引策略

How can I see the size of an index?如何查看索引的大小?

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中最近值的索引

How do write operations affect indexes?写操作如何影响索引?

Write operations may require updates to indexes:写操作可能需要更新索引:

  • If a write operation modifies an indexed field, MongoDB updates all indexes that have the modified field as a key.如果写入操作修改了索引字段,MongoDB将更新所有以该修改字段为键的索引。

Therefore, if your application is write-heavy, indexes might affect performance.因此,如果应用程序写得很重,索引可能会影响性能。

←  FAQ: MongoDB FundamentalsFAQ: Concurrency →