Use $ or and sort () to query performance analysis at the same time in MongoDB

Source: Internet
Author: User

This article introduces how to use $ or and sort () to query performance analysis in MongoDB. If you need it, let's take a look at it. If you don't design thousands of records for your website, the server will be stuck.

The Code is as follows: Copy code

Mongos> db. find ({"user": "jhon"}). sort ({"name ":
1}). limit (100). explain ()
{
"Cursor": "BtreeCursor user_1 ",
"Nscanned": 10100,
"NscannedObjects": 10100,
"N": 100,
"ScanAndOrder": true,
"Millennium": 116,
"NYields": 0,
"NChunkSkips": 0,
"IsMultiKey": false,
"IndexOnly": false,
"IndexBounds ":{
"User ":[
[
"Jhon ",
"Jhon"
]
]
}
}

Second, I do $ or query with sort ():
Mongos> db. find ({"$ or": [{"user": "jhon" },{ "owner ":
"Jhon"}]}). sort ({"name": 1}). limit (100). explain ()
{
"Cursor": "BtreeCursor name_1 ",
"Nscanned": 1010090,
"NscannedObjects": 1010090,
"N": 100,
"Millennium": 3800,
"NYields": 0,
"NChunkSkips": 0,
"IsMultiKey": false,
"IndexOnly": false,
"IndexBounds ":{
"Name ":[
[
{
"$ MinElement": 1
},
{
"$ MaxElement": 1
}
]
]
}
}

Last, I do $ or query without sort ():
Mongos> db. find ({"$ or": [{"user": "jhon" },{ "owner ":
"Jhon"}]}). limit (100). explain ()
{
"Cursor": "BtreeCursor user_1 ",
"Nscanned": 100,
"NscannedObjects": 100,
"N": 100,
"Millis": 0,
"NYields": 0,
"NChunkSkips": 0,
"IsMultiKey": false,
"IndexOnly": false,
"IndexBounds ":{
"User ":[
[
"Jhon ",
"Jhon"
]
]
}
}

We can see that:

In the first query, sort () alone performs well.

In the second query, when $ or and sort () are used together, the performance is poor.

In the third query, $ or is used separately, and the performance is good.

 

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.