My collection has an index {a:1, b:1}, however the query
db.test.find({b:2}, {a:1,_id:0}).sort({a:1})
is not coverd (totalDocsExamined is the total number of docs in this collection). below is execution plan output. I understand the ESR rule, but even in that case, both a and b are in the same index tree, and should be able to serve: scan first (sort for free) and filter.
Why the engine is instead fetching value of b from disk ?
"explainVersion" : "1",
"queryPlanner" : {
"namespace" : "test.test",
"indexFilterSet" : false,
"parsedQuery" : {
"b" : {
"$eq" : 2
}
},
"maxIndexedOrSolutionsReached" : false,
"maxIndexedAndSolutionsReached" : false,
"maxScansToExplodeReached" : false,
"winningPlan" : {
"stage" : "PROJECTION_SIMPLE",
"transformBy" : {
"a" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"b" : {
"$eq" : 2
}
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1,
"b" : 1
},
"indexName" : "a_1_b_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"a" : [ ],
"b" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[MinKey, MaxKey]"
],
"b" : [
"[MinKey, MaxKey]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 6,
"totalDocsExamined" : 6,
"executionStages" : {
"stage" : "PROJECTION_SIMPLE",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 7,
"advanced" : 1,
"needTime" : 5,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"transformBy" : {
"a" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"b" : {
"$eq" : 2
}
},
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 7,
"advanced" : 1,
"needTime" : 5,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"docsExamined" : 6,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 6,
"executionTimeMillisEstimate" : 0,
"works" : 7,
"advanced" : 6,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"keyPattern" : {
"a" : 1,
"b" : 1
},
"indexName" : "a_1_b_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"a" : [ ],
"b" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[MinKey, MaxKey]"
],
"b" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 6,
"seeks" : 1,
"dupsTested" : 0,
"dupsDropped" : 0
}
}
}
},
"command" : {
"find" : "test",
"filter" : {
"b" : 2
},
"sort" : {
"a" : 1
},
"projection" : {
"a" : 1,
"_id" : 0
},
"$db" : "test"
},