MongoDB and query may overlap when encountering multiple index--unlike composite indexes

Source: Internet
Author: User

A question about the index document in MongoDB?

-

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


{Qty:1} {Item:1}

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


Db.orders.find ({item: "Abc123", Qty: {$gt: 15}})

The above is the index of MongoDB document, said the following query can be used to cross the above two indexes to optimize, but according to my understanding, need to establish a multi-index before the line, as follows:
{qty:1, item:1}

Building two indexes is to build two separate B + trees, multiple indexes are to build a B + tree, if two B + trees are independent, how can cross-use it?

And here's an example of how you can mix and use it.

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


Db.orders.find ({qty: {$gt: ten}, Status: "A"})

It seems amazing to see the question. But the actual look of the past has not changed.


Determine if MongoDB used index intersection, run explain (); The results of explain () would include either an and_sorted stage or an and_hash stage.



Merge sort or hash combination. Similar to join practices.

Stages is descriptive of the operation; e.g.

    • COLLSCANFor a collection scan
    • IXSCANFor scanning index keys
    • FETCHFor retrieving documents
    • SHARD_MERGEFor merging results from shards
Index intersection

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

 { "stage" :  "and_sorted"  span class= "s2" > "inputstages" : [{ "stage" :  "IXSCAN"  ... }, { "stage" :  "IXSCAN" } ]}          /span>               

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

Transferred from: 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 query may overlap when encountering multiple index--unlike composite indexes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.