On this page本页内容
An index supports a query when the index contains all the fields scanned by the query. 当索引包含查询扫描的所有字段时,索引支持查询。The query scans the index and not the collection. 查询扫描索引,而不是集合。Creating indexes that support queries results in greatly increased query performance.创建支持查询的索引可以大大提高查询性能。
This document describes strategies for creating indexes that support queries.本文档描述了创建支持查询的索引的策略。
If you only ever query on a single key in a given collection, then you need to create just one single-key index for that collection. 如果只查询给定集合中的单个键,那么只需要为该集合创建一个单键索引。For example, you might create an index on 例如,您可以在category
in the product
collection:product
集合中的category
上创建索引:
If you sometimes query on only one key and at other times query on that key combined with a second key, then creating a compound index is more efficient than creating a single-key index. 如果有时只查询一个键,而有时又查询该键与第二个键的组合,那么创建复合索引比创建单个键索引更有效。MongoDB will use the compound index for both queries. MongoDB将对这两个查询使用复合索引。For example, you might create an index on both 例如,可以在category
and item
.category
和item
上创建索引。
This allows you both options. 这让你有两种选择。You can query on just 您可以只查询category
, and you also can query on category
combined with item
. category
,也可以查询与item
组合的category
。A single compound index on multiple fields can support all the queries that search a “prefix” subset of those fields.多个字段上的单个复合索引可以支持搜索这些字段的“前缀”子集的所有查询。
Example示例
The following index on a collection:集合上的以下索引:
Can support queries that the following indexes support:可以支持以下索引支持的查询:
There are some situations where the prefix indexes may offer better query performance: for example if 在某些情况下,前缀索引可能提供更好的查询性能:例如,如果z
is a large array.z
是一个大数组。
The { x: 1, y: 1, z: 1 }
index can also support many of the same queries as the following index:{ x: 1, y: 1, z: 1 }
索引还可以支持许多与以下索引相同的查询:
Also, 另外,{ x: 1, z: 1 }
has an additional use. { x: 1, z: 1 }
还有一个额外的用途。Given the following query:给出以下问题:
The { x: 1, z: 1 }
index supports both the query and the sort operation, while the { x: 1, y: 1, z: 1 }
index only supports the query. { x: 1, z: 1 }
索引支持查询和排序操作,而{ x: 1, y: 1, z: 1 }
索引只支持查询。For more information on sorting, see Use Indexes to Sort Query Results.有关排序的详细信息,请参阅使用索引对查询结果进行排序。
Starting in version 2.6, MongoDB can use index intersection to fulfill queries. 从2.6版开始,MongoDB可以使用索引交集来完成查询。The choice between creating compound indexes that support your queries or relying on index intersection depends on the specifics of your system. 创建支持查询的复合索引还是依赖索引交集取决于系统的具体情况。See Index Intersection and Compound Indexes for more details.有关详细信息,请参阅索引交集和复合索引。
To use an index for string comparisons, an operation must also specify the same collation. 要使用索引进行字符串比较,操作还必须指定相同的排序规则。That is, an index with a collation cannot support an operation that performs string comparisons on the indexed fields if the operation specifies a different collation.也就是说,如果操作指定了不同的排序规则,则具有排序规则的索引不能支持对索引字段执行字符串比较的操作。
For example, the collection 例如,集合myColl
has an index on a string field category
with the collation locale "fr"
.myColl
在排序规则区域设置为"fr"
的字符串字段category
上有一个索引。
The following query operation, which specifies the same collation as the index, can use the index:以下查询操作指定了与索引相同的排序规则,可以使用索引:
However, the following query operation, which by default uses the “simple” binary collator, cannot use the index:但是,以下查询操作(默认情况下使用“简单”二进制排序器)不能使用索引:
For a compound index where the index prefix keys are not strings, arrays, and embedded documents, an operation that specifies a different collation can still use the index to support comparisons on the index prefix keys.对于索引前缀键不是字符串、数组和嵌入文档的复合索引,指定不同排序规则的操作仍然可以使用索引来支持索引前缀键的比较。
For example, the collection 例如,集合myColl
has a compound index on the numeric fields score
and price
and the string field category
; the index is created with the collation locale "fr"
for string comparisons:myColl
在数字字段score
和price
以及字符串字段类别上有一个复合索引;索引是使用排序规则区域设置"fr"
创建的,用于字符串比较:
The following operations, which use 以下使用"simple"
binary collation for string comparisons, can use the index:"simple"
二进制排序规则进行字符串比较的操作可以使用索引:
The following operation, which uses 以下操作使用"simple"
binary collation for string comparisons on the indexed category
field, can use the index to fulfill only the score: 5
portion of the query:"simple"
二进制排序规则对索引category
字段进行字符串比较,可以使用索引只完成查询的score: 5
部分: