Docs HomeMongoDB Manual

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

This document addresses some common questions regarding MongoDB indexes. For more information on indexes, see indexes.本文档解决了一些关于MongoDB索引的常见问题。有关索引的详细信息,请参阅索引

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.24.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. background index builds do not take an exclusive lock during the build process.对于功能兼容性版本(fcv)“4.0”,默认的前台索引生成过程将为整个索引生成保留独占锁。background索引生成在生成过程中不使用独占锁。

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.要最大限度地减少对副本集(包括分片副本集)生成索引的影响,请使用滚动索引生成过程,如对副本集生成滚动索引中所述。

How do I monitor index build progress?如何监控索引生成进度?

To return information on currently running index creation operations, see Active Indexing Operations.若要返回有关当前正在运行的索引创建操作的信息,请参阅活动索引操作

How do I terminate an index build?如何终止索引生成?

To terminate an in-progress index build, use the db.collection.dropIndex() or its shell helpers dropIndex() or dropIndexes. 若要终止正在进行的索引生成,请使用db.collection.dropIndex()或其shell助手dropIndex()dropIndexesDo not use db.killOp() to terminate in-progress index builds in replica sets or sharded clusters.不要使用db.killOp()终止副本集中或分片集群中正在进行的索引构建。

You cannot terminate a replicated index build on secondary members of a replica set. 不能终止复制副本集的辅助成员上的复制索引生成。You must first drop the index on the primary. Starting in version 4.4, the primary stops the index build and creates an associated abortIndexBuild oplog entry. 必须首先删除主目录上的索引。从版本4.4开始,主进程停止索引生成并创建一个相关的abortIndexBuild oplog条目。Secondaries that replicate the abortIndexBuild oplog entry stop the in-progress index build and discard the build job.复制abortIndexBuild操作日志项的辅助操作将停止正在进行的索引生成并放弃生成作业。

To learn more, see Stop In-Progress Index Builds.要了解更多信息,请参阅停止正在进行的索引生成

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.因此,如果您的应用程序写得很重,索引可能会影响性能。