On this page本页内容
MongoDB can use the intersection of multiple indexes to fulfill queries. MongoDB可以使用多个索引的交集来完成查询。In general, each index intersection involves two indexes; however, MongoDB can employ multiple/nested index intersections to resolve a query.通常,每个索引交叉点包含两个索引;然而,MongoDB可以使用多个/嵌套索引交叉点来解析查询。
To illustrate index intersection, consider a collection 若要说明索引交集,请考虑具有以下索引的集合orders
that has the following indexes:orders
:
MongoDB can use the intersection of the two indexes to support the following query:MongoDB可以使用两个索引的交集来支持以下查询:
To determine if MongoDB used index intersection, run 要确定MongoDB是否使用了索引交集,请运行explain()
; the results of explain() will include either an AND_SORTED
stage or an AND_HASH
stage.explain()
;explain()的结果将包括AND_SORTED
阶段或AND_HASH
阶段。
With index intersection, MongoDB can use an intersection of either the entire index or the index prefix. 通过索引交集,MongoDB可以使用整个索引或索引前缀的交集。An index prefix is a subset of a compound index, consisting of one or more keys starting from the beginning of the index.索引前缀是复合索引的子集,由从索引开头开始的一个或多个键组成。
Consider a collection 考虑具有以下索引的集合orders
with the following indexes:orders
:
To fulfill the following query which specifies a condition on both the 为了完成以下查询,即指定qty
field and the status
field, MongoDB can use the intersection of the two indexes:qty
字段和status
字段的条件,MongoDB可以使用两个索引的交集:
Index intersection does not eliminate the need for creating compound indexes. 索引交集并不能消除创建复合索引的需要。However, because both the list order (i.e. the order in which the keys are listed in the index) and the sort order (i.e. ascending or descending), matter in compound indexes, a compound index may not support a query condition that does not include the index prefix keys or that specifies a different sort order.但是,由于列表顺序(即索引中键的列出顺序)和排序顺序(即升序或降序)在复合索引中都很重要,因此复合索引可能不支持不包含索引前缀键或指定不同排序顺序的查询条件。
For example, if a collection 例如,如果集合orders
has the following compound index, with the status
field listed before the ord_date
field:orders
具有以下复合索引,且status
字段列在ord_date
字段之前:
The compound index can support the following queries:复合索引可以支持以下查询:
But not the following two queries:但不是以下两个问题:
However, if the collection has two separate indexes:但是,如果集合有两个单独的索引:
The two indexes can, either individually or through index intersection, support all four aforementioned queries.这两个索引可以单独使用,也可以通过索引交叉使用,支持上述四种查询。
The choice between creating compound indexes that support your queries or relying on index intersection depends on the specifics of your system.创建支持查询的复合索引还是依赖索引交集取决于系统的具体情况。
Index intersection does not apply when the 当sort()
operation requires an index completely separate from the query predicate.sort()
操作要求索引与查询谓词完全分离时,索引交集不适用。
For example, the 例如,orders
collection has the following indexes:orders
集合具有以下索引:
MongoDB cannot use index intersection for the following query with sort:MongoDB无法将索引交集用于以下带有排序的查询:
That is, MongoDB does not use the 也就是说,MongoDB不对查询使用{ qty: 1 }
index for the query, and the separate { status: 1 }
or the { status: 1, ord_date: -1 }
index for the sort.{qty:1}
索引,对排序使用单独的{status:1}
或{status:1,ord_date:-1}
索引。
However, MongoDB can use index intersection for the following query with sort since the index 但是,MongoDB可以使用索引交集进行以下带有排序的查询,因为索引{ status: 1, ord_date: -1 }
can fulfill part of the query predicate.{status:1,ord_date:-1}
可以完成部分查询谓词。