mongodb AND查詢遇到多個index時候可能會做交集——和複合索引不同

來源:互聯網
上載者:User

標籤:mongod   alert   term   index   turn   script   www   變化   puts   

關於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

mongodb AND查詢遇到多個index時候可能會做交集——和複合索引不同

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.