Queries that require a blocking sort always fetch the full document from the collection to retrieve the sort key, even if the query plan includes an index scan over an index that has the sort key as a part of its key pattern. These queries should be covered instead.
To reproduce:
> db.foo.drop() true > db.foo.ensureIndex({a: 1, b: 1}) > db.foo.find({a: {$gt: 0}}, {a: 1, b: 1, _id: 0}).sort({b: 1}).explain(true).stats { "stage" : "PROJECTION", "nReturned" : 0, "executionTimeMillis" : 2, "works" : 4, "advanced" : 0, "needTime" : 0, "isEOF" : 1, "invalidates" : 0, "transformBy" : { "_id" : 0, "a" : 1, "b" : 1 }, "inputStage" : { "stage" : "SORT", "nReturned" : 0, "executionTimeMillis" : 2, "works" : 4, "advanced" : 0, "needTime" : 2, "isEOF" : 1, "invalidates" : 0, "sortPattern" : { "b" : 1 }, "memUsage" : 0, "memLimit" : 33554432, "inputStage" : { "stage" : "KEEP_MUTATIONS", "nReturned" : 0, "executionTimeMillis" : 0, "works" : 2, "advanced" : 0, "needTime" : 1, "isEOF" : 1, "invalidates" : 0, "inputStage" : { "stage" : "FETCH", "nReturned" : 0, "executionTimeMillis" : 0, "works" : 2, "advanced" : 0, "needTime" : 1, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 0, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 0, "executionTimeMillis" : 0, "works" : 1, "advanced" : 0, "needTime" : 1, "isEOF" : 1, "invalidates" : 0, "keysExamined" : 0, "keyPattern" : { "a" : 1, "b" : 1 }, "isMultiKey" : false, "indexBounds" : "field #0['a']: (0.0, inf.0], field #1['b']: [MinKey, MaxKey]", "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0, "matchTested" : 0 } } } } }
- duplicates
-
SERVER-3473 scan and order always loads the full document
- Closed
-
SERVER-5019 covered indexes are not implemented for scan and order sorts
- Closed
- is duplicated by
-
SERVER-23999 Query fully covered by index still reads documents
- Closed