我在每个文档中都有一个locked
字段的集合。
我有以下索引:
{
locked : 1
}
当我通过计数操作执行此解释时
db.scheduled.find({locked: false}).explain({executionStats:1})
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "connectivity_recruiter.scheduled",
"indexFilterSet" : false,
"parsedQuery" : {
"locked" : {
"$eq" : false
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"locked" : 1
},
"indexName" : "locked_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"locked" : [
"[false, false]"
]
}
}
},
.....
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 53045,
"executionTimeMillis" : 299,
"totalKeysExamined" : 53045,
"totalDocsExamined" : 53045,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 53045,
"executionTimeMillisEstimate" : 180,
"works" : 53046,
"advanced" : 53045,
"needTime" : 0,
"needFetch" : 0,
"saveState" : 417,
"restoreState" : 417,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 53045,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 53045,
"executionTimeMillisEstimate" : 70,
"works" : 53046,
"advanced" : 53045,
"needTime" : 0,
"needFetch" : 0,
"saveState" : 417,
"restoreState" : 417,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"locked" : 1
},
"indexName" : "locked_1",
"isMultiKey" : false,
"direction" : "forward",
"indexBounds" : {
"locked" : [
"[false, false]"
]
},
"keysExamined" : 53045,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0
}
},
...........
}
totalDocsExamined似乎表明正在扫描所有文档以便对其进行计数,而此操作可以仅使用索引来执行。 怎么了?这是正常的吗?是否正在对该集合进行全面扫描?
由于
答案 0 :(得分:0)
检查了所有返回的文档,索引仅用于过滤不检索文档。
如果你查看你的解释,你会注意到文档的数量等于所检查文档的数量。
为什么?在您获取整个文档时,您的索引只包含一个字段,mongodb的工作是查询键的索引,然后转到集合以获取文档。
当索引包含所有投影字段时,唯一的情况是不需要检查文档是否为覆盖查询。
点击此链接查看更多信息:https://docs.mongodb.com/manual/core/query-optimization/#covered-query