Index Intersection索引交点

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

{ qty: 1 }
{ item: 1 }

MongoDB can use the intersection of the two indexes to support the following query:MongoDB可以使用两个索引的交集来支持以下查询:

db.orders.find( { item: "abc123", qty: { $gt: 15 } } )

To determine if MongoDB used index intersection, run explain(); the results of explain() will include either an AND_SORTED stage or an AND_HASH stage.要确定MongoDB是否使用了索引交集,请运行explain()explain()的结果将包括AND_SORTED阶段或AND_HASH阶段。

Index Prefix Intersection索引前缀交集

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

{ qty: 1 }
{ status: 1, ord_date: -1 }

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可以使用两个索引的交集:

db.orders.find( { qty: { $gt: 10 } , status: "A" } )

Index Intersection and Compound Indexes索引交与复合索引

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字段之前:

{ status: 1, ord_date: -1 }

The compound index can support the following queries:复合索引可以支持以下查询:

db.orders.find( { status: { $in: ["A", "P" ] } } )
db.orders.find(
   {
     ord_date: { $gt: new Date("2014-02-01") },
     status: {$in:[ "P", "A" ] }
   }
)

But not the following two queries:但不是以下两个问题:

db.orders.find( { ord_date: { $gt: new Date("2014-02-01") } } )
db.orders.find( { } ).sort( { ord_date: 1 } )

However, if the collection has two separate indexes:但是,如果集合有两个单独的索引:

{ status: 1 }
{ ord_date: -1 }

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 and Sort索引交叉和排序

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集合具有以下索引:

{ qty: 1 }
{ status: 1, ord_date: -1 }
{ status: 1 }
{ ord_date: -1 }

MongoDB cannot use index intersection for the following query with sort:MongoDB无法将索引交集用于以下带有排序的查询:

db.orders.find( { qty: { $gt: 10 } } ).sort( { status: 1 } )

That is, MongoDB does not use the { qty: 1 } index for the query, and the separate { status: 1 } or the { status: 1, ord_date: -1 } index for the sort.也就是说,MongoDB不对查询使用{qty:1}索引,对排序使用单独的{status:1}{status:1,ord_date:-1}索引。

However, MongoDB can use index intersection for the following query with sort since the index { status: 1, ord_date: -1 } can fulfill part of the query predicate.但是,MongoDB可以使用索引交集进行以下带有排序的查询,因为索引{status:1,ord_date:-1}可以完成部分查询谓词。

db.orders.find( { qty: { $gt: 10 } , status: "A" } ).sort( { ord_date: -1 } )