MongoDB limit () and sort () optimization

I am trying to understand why this is happening:

db.items.find({uid: {$in:[34, 54, 53,1,2,3,5,6,7]} }).limit(40).sort({_id:-1}).explain()

      

returns me:

"cursor" : "BtreeCursor _id_-1_uid_1 multi",
"nscanned" : 167,
"nscannedObjects" : 40,
"n" : 40,
...

      

However, without sorting

db.items.find({uid: {$in:[34, 54, 53,1,2,3,5,6,7]} }).limit(40).explain()

      

returns me:

"cursor" : "BtreeCursor uid_1 multi",
"nscanned" : 40,
"nscannedObjects" : 40,
"n" : 40,
...

      

So what I don't understand is why when I add sorting it is looking at more documents? I am sorting by id, also I have this index to try to improve things "_id_-1_uid_1", but still looking at a lot of documents.

This is a huge problem in my case, because this is my local database, so only a few documents were scanned, however, on my live site, I have millions of documents that were scanned for some reason, which I don't know what they are ...

Can anyone please explain to me what is going on?

+3


source to share


1 answer


Currently (2012/3/14) this is an unresolved error:



https://jira.mongodb.org/browse/SERVER-3310

+3


source







All Articles