關於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 scanIXSCAN
for scanning index keysFETCH
for retrieving documentsSHARD_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