【问题标题】:MongoDB possibly scanning documents for an operation that could be covered by an indexMongoDB 可能会扫描文档以查找可能被索引覆盖的操作
【发布时间】:2016-04-14 10:37:21
【问题描述】:

我有一个集合,每个文档中都有一个 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 似乎表明正在扫描所有文档以对它们进行计数,而此操作可以仅使用索引来执行。 怎么了?这是正常的吗?正在对集合进行全面扫描吗?

谢谢

【问题讨论】:

    标签: mongodb mongodb-indexes


    【解决方案1】:

    检查所有返回的文档,索引仅用于过滤而不是检索文档。

    如果您查看您的说明,您会注意到文档数等于检查的文档数。

    为什么?当您获取整个文档时,您的索引仅包含一个字段,mongodb 所做的是查询索引中的键,然后去集合以获取文档。

    当索引包含所有投影字段时,不需要检查文档的唯一情况是覆盖查询。

    在此链接查看更多信息:https://docs.mongodb.com/manual/core/query-optimization/#covered-query

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多