$or and sort () query performance analysis in MongoDB

Source: Internet
Author: User
Tags mongodb sort
The code is as follows Copy Code

Mongos> Db.find ({"User": "Jhon"}). Sort ({"Name":
1}). Limit. Explain ()
{
"Cursor": "Btreecursor user_1",
"nscanned": 10100,
"Nscannedobjects": 10100,
"N": 100,
"Scanandorder": true,
"Millis": 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. Explain ()
{
"Cursor": "Btreecursor name_1",
"nscanned": 1010090,
"Nscannedobjects": 1010090,
"N": 100,
"Millis": 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. 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"
]
]
}
}

You can see:

In the first query, it was a good performance when you used the sort () alone.

In the second query, when the union uses $or and sort (), performance is poor.

In the third query, using $or alone, the performance is very 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.