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,
"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(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"
]
]
}
}
As you see, in the "$or query with sort()", it took 3800 ms, when the
normal query with sort() took 116ms.
Any advice?
I believe the issue you are running into is expressed in this JIRA
ticket: https://jira.mongodb.org/browse/SERVER-1205
I believe the query optimizer is choosing to use the name index and
walk it backwards. As it goes through the index it compares the user
and owner attributes to your parameters and collects them in sorted
order. As a result the nscanned objects is much larger than in the
other two cases.
I would vote up the issue to prioritize it.
-Tyler
On 12月1日, 上午3时46分, Tyler Brock <ty...@10gen.com> wrote:
> Gen,
>
> I believe the issue you are running into is expressed in this JIRA
> ticket:https://jira.mongodb.org/browse/SERVER-1205
>
> I believe the query optimizer is choosing to use the name index and
> walk it backwards. As it goes through the index it compares the user
> and owner attributes to your parameters and collects them in sorted
> order. As a result the nscanned objects is much larger than in the
> other two cases.
>
> I would vote up the issue to prioritize it.
>
> -Tyler
>