带索引的MongoDB计数要比不带索引的计数慢

时间:2018-11-24 12:44:40

标签: mongodb indexing nosql counting

我创建了一个包含1,505,000个文档的Mongo集合。

a键上没有索引,而我正在这样做:

db.test.count({a: {$ne: "some_string"}})

返回金额大约需要1200ms。

当我做同样的事情时,但是这次a键上的 WITH 索引,大约需要12000毫秒。

技术细节

  1. 索引是使用db.test.createIndex({a: 1})

  2. 创建的
  3. 对于等价而言,当使用find代替count时,它将在0.002 ms之后提供结果。

  4. 我在本地主机上使用MongoDB。

这是executionStats:

/* 1 */
{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "db.test",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$nor" : [ 
                {
                    "a" : {
                        "$eq" : "some_string"
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "COUNT",
            "inputStage" : {
                "stage" : "FETCH",
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "keyPattern" : {
                        "a" : 1.0
                    },
                    "indexName" : "a_1",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "a" : []
                    },
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "a" : [ 
                            "[MinKey, \"some_string\")", 
                            "(\"some_string\", MaxKey]"
                        ]
                    }
                }
            }
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 0,
        "executionTimeMillis" : 10318,
        "totalKeysExamined" : 1505000,
        "totalDocsExamined" : 1505000,
        "executionStages" : {
            "stage" : "COUNT",
            "nReturned" : 0,
            "executionTimeMillisEstimate" : 9916,
            "works" : 1505001,
            "advanced" : 0,
            "needTime" : 1505000,
            "needYield" : 0,
            "saveState" : 12102,
            "restoreState" : 12102,
            "isEOF" : 1,
            "invalidates" : 0,
            "nCounted" : 1505000,
            "nSkipped" : 0,
            "inputStage" : {
                "stage" : "FETCH",
                "nReturned" : 1505000,
                "executionTimeMillisEstimate" : 9736,
                "works" : 1505001,
                "advanced" : 1505000,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 12102,
                "restoreState" : 12102,
                "isEOF" : 1,
                "invalidates" : 0,
                "docsExamined" : 1505000,
                "alreadyHasObj" : 0,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 1505000,
                    "executionTimeMillisEstimate" : 2479,
                    "works" : 1505001,
                    "advanced" : 1505000,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 12102,
                    "restoreState" : 12102,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "a" : 1.0
                    },
                    "indexName" : "a_1",
                    "isMultiKey" : false,
                    "multiKeyPaths" : {
                        "a" : []
                    },
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 1,
                    "direction" : "forward",
                    "indexBounds" : {
                        "a" : [ 
                            "[MinKey, \"some_string\")", 
                            "(\"some_string\", MaxKey]"
                        ]
                    },
                    "keysExamined" : 1505000,
                    "seeks" : 1,
                    "dupsTested" : 0,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0
                }
            }
        }
    },
    "serverInfo" : {
        "host" : "razs-MacBook",
        "port" : 27017,
        "version" : "3.4.6",
        "gitVersion" : ""
    },
    "ok" : 1.0
}

怎么了?

0 个答案:

没有答案