Indexes索引
On this page本页内容
Indexes support efficient execution of queries in MongoDB. 索引支持在MongoDB中高效执行查询。Without indexes, MongoDB must scan every document in a collection to return query results. 在没有索引的情况下,MongoDB必须扫描集合中的每个文档才能返回查询结果。If an appropriate index exists for a query, MongoDB uses the index to limit the number of documents it must scan.如果查询存在适当的索引,MongoDB会使用该索引来限制它必须扫描的文档数量。
Although indexes improve query performance, adding an index has negative performance impact for write operations. 尽管索引可以提高查询性能,但添加索引会对写入操作的性能产生负面影响。For collections with a high write-to-read ratio, indexes are expensive because each insert must also update any indexes.对于具有高写/读比率的集合,索引是昂贵的,因为每次插入还必须更新任何索引。
Use Cases使用案例
If your application is repeatedly running queries on the same fields, you can create an index on those fields to improve performance. 如果应用程序在同一字段上重复运行查询,则可以在这些字段上创建索引以提高性能。For example, consider the following scenarios:例如,考虑以下场景:
state , city , and zipcode . state 、city 和zipcode 等字段。location object to improve performance for queries on any field in that object.location 对象上创建索引,以提高该对象中任何字段的查询性能。 | |
inventory 商品,以确定哪些商品inventory 不足。item and quantity fields to improve query performance.item 字段和quantity 字段上创建一个索引,以提高查询性能。 |
Get Started开始
To learn how to perform common tasks with indexes, see these pages:要了解如何使用索引执行常见任务,请参阅以下页面:
Details详细信息
Indexes are special data structures that store a small portion of the collection's data set in an easy-to-traverse form. MongoDB indexes use a B-tree索引是一种特殊的数据结构,以易于遍历的形式存储集合数据集的一小部分。MongoDB索引使用B树 data structure.
数据结构。
The index stores the value of a specific field or set of fields, ordered by the value of the field. 索引存储特定字段或字段集的值,按字段值排序。The ordering of the index entries supports efficient equality matches and range-based query operations. 索引项的排序支持高效的相等匹配和基于范围的查询操作。In addition, MongoDB can return sorted results using the ordering in the index.此外,MongoDB可以使用索引中的排序返回排序结果。
Restrictions限制
Certain restrictions apply to indexes, such as the length of the index keys or the number of indexes per collection. 某些限制适用于索引,例如索引键的长度或每个集合的索引数。For details, see Index Limitations.有关详细信息,请参阅索引限制。
Default Index默认索引
MongoDB creates a unique index on the _id field during the creation of a collection. 在创建集合的过程中,MongoDB会在_id
字段上创建一个唯一的索引。The _id
index prevents clients from inserting two documents with the same value for the _id
field. _id
索引防止客户端插入两个具有相同_id
字段值的文档。You cannot drop this index.不能删除此索引。
Index Names索引名称
The default name for an index is the concatenation of the indexed keys and each key's direction in the index (索引的默认名称是索引键和索引中每个键的方向(1
or -1
) using underscores as a separator. 1
或-1
)的串联,使用下划线作为分隔符。For example, an index created on 例如,在{ item : 1, quantity: -1 }
has the name item_1_quantity_-1
.{ item : 1, quantity: -1 }
上创建的索引的名称为item_1_quantity_-1
。
You cannot rename an index once created. Instead, you must drop and recreate the index with a new name.索引一旦创建,就不能重命名。相反,您必须删除索引并使用新名称重新创建索引。
To learn how to specify the name for an index, see Specify an Index Name.要了解如何指定索引的名称,请参阅指定索引名称。
Index Build Performance索引生成性能
Applications may encounter reduced performance during index builds, including limited read/write access to the collection. 应用程序在索引生成过程中可能会遇到性能下降的问题,包括对集合的读/写访问受限。For more information on the index build process, see Index Builds on Populated Collections, including the Index Builds in Replicated Environments section.有关索引生成过程的详细信息,请参阅填充集合上的索引生成,包括复制环境中的索引生成部分。
Learn More了解更多信息
MongoDB provides a number of different index types to support specific types of data and queries.MongoDB提供了许多不同的索引类型来支持特定类型的数据和查询。To learn more, see Index Types.要了解更多信息,请参阅索引类型。To learn what properties and behaviors you can specify in your index, see Index Properties.要了解可以在索引中指定哪些属性和行为,请参阅索引属性。To understand considerations you may need to make when you create an index, see Indexing Strategies.要了解创建索引时可能需要考虑的事项,请参阅索引策略。To learn about the performance impact of indexes, see Operational Factors and Data Models.要了解索引对性能的影响,请参阅操作因素和数据模型。