mongodb AND查询遇到多个index时候可能会做交集——和复合索引不同

关于MongoDB中索引文档的一个问题?

 -

To illustrate index intersection, consider a collection orders that has the following indexes:


{ qty: 1 }
{ item: 1 }

MongoDB can use the intersection of the two indexes to support the following query:


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

上面是MongoDB的索引文档,说下面这个查询能交叉利用上面两个索引进行优化,可是根据我的理解,需要建立一个多重索引才行,如下:
{ qty: 1, item: 1 }

建立两个索引是建立两个独立的B+树,多重索引则是建立一颗B+树,如果两颗B+树是独立的,怎么才能交叉利用呢?

还有下面这个例子,也不知道如何才能混合利用起来?

Consider a collection orders with the following indexes:


{ 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:


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

 

看问题似乎很神奇。。不过实际看过去也没啥变化。。


determine if MongoDB used index intersection, run explain(); the results of explain()will include either an AND_SORTED stage or an AND_HASH stage.



归并排序或hash 组合。 类似join的做法。

Stages are descriptive of the operation; e.g.

  • COLLSCAN for a collection scan
  • IXSCAN for scanning index keys
  • FETCH for retrieving documents
  • SHARD_MERGE for merging results from shards

Index Intersection

For an index intersection plan, the result will include either an AND_SORTED stage or an AND_HASH stage with an inputStages array that details the indexes; e.g.:

{
   "stage" : "AND_SORTED",
   "inputStages" : [
      {
         "stage" : "IXSCAN",
         ...
      },
      {
         "stage" : "IXSCAN",
         ...
      }
   ]
}

In previous versions of MongoDB, cursor.explain() returned the cursor field with the value of Complex Plan for index intersections.

 

转自:http://www.ihowandwhy.com/z/%E5%85%B3%E4%BA%8EMongoDB%E4%B8%AD%E7%B4%A2%E5%BC%95%E6%96%87%E6%A1%A3%E7%9A%84%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%9F

posted @ 2017-04-25 18:46  bonelee  阅读(2465)  评论(0编辑  收藏  举报