首页 > 解决方案 > MongoDB 使用索引计数比不使用索引计数慢

问题描述

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

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

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

返回金额大约需要 1200 毫秒。

当我做同样的事情时,但这次WITH index on akey 大约需要 12000 毫秒。

技术细节

  1. 索引是使用以下方法创建的:db.test.createIndex({a: 1})

  2. 为了等价,当使用find而不是count它时,它会在 0.002 毫秒后提供结果。

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

这是执行统计:

/* 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
}

这是怎么回事?

标签: mongodbindexingnosqlcounting

解决方案


推荐阅读