没有条件的MongoDB计数 - 立即返回或扫描全表?

时间:2016-04-28 15:14:12

标签: mongodb

我想通过“collection.count()”了解集合中有多少文档,但担心mongodb会像InnoDB一样执行全表扫描。

这是我的代码:

    for (String name : db.getCollectionNames()) {
        if (name.startsWith("system.") || name.startsWith("_meta.")) {
            continue;
        }
        DBCollection collection = db.getCollection(name);
        long count = collection.count();
        ...

我们知道MySQL中的SELECT COUNT(*) FROM some_table在引擎MyISAM和InnoDB下显示出不同的性能。 MyISAM将立即返回,但InnoDB将扫描全表。

更新

根据joao的建议,db.runCommand({explain: {count: "metric.rows", query:{}}})(计算没有条件)得到:

{
    "queryPlanner" : {
            "plannerVersion" : 1,
            "namespace" : "metric.metric.rows",
            "indexFilterSet" : false,
            "winningPlan" : {
                    "stage" : "COUNT"
            },
            "rejectedPlans" : [ ]
    },
    "executionStats" : {
            "executionSuccess" : true,
            "nReturned" : 0,
            "executionTimeMillis" : 0,
            "totalKeysExamined" : 0,
            "totalDocsExamined" : 0,
            "executionStages" : {
                    "stage" : "COUNT",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 1,
                    "advanced" : 0,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "nCounted" : 612,
                    "nSkipped" : 0
            },
            "allPlansExecution" : [ ]
    },
    "serverInfo" : {
            "host" : "hnd2001",
            "port" : 27017,
            "version" : "3.2.6",
            "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
    },
    "ok" : 1
}

db.runCommand({explain: {count: "metric.rows", query:{name: "a"}}})(计算带有条件的)得到了:

{
    "queryPlanner" : {
            "plannerVersion" : 1,
            "namespace" : "metric.metric.rows",
            "indexFilterSet" : false,
            "parsedQuery" : {
                    "name" : {
                            "$eq" : "a"
                    }
            },
            "winningPlan" : {
                    "stage" : "COUNT",
                    "inputStage" : {
                            "stage" : "COLLSCAN",
                            "filter" : {
                                    "name" : {
                                            "$eq" : "a"
                                    }
                            },
                            "direction" : "forward"
                    }
            },
            "rejectedPlans" : [ ]
    },
    "executionStats" : {
            "executionSuccess" : true,
            "nReturned" : 0,
            "executionTimeMillis" : 0,
            "totalKeysExamined" : 0,
            "totalDocsExamined" : 646,
            "executionStages" : {
                    "stage" : "COUNT",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 648,
                    "advanced" : 0,
                    "needTime" : 647,
                    "needYield" : 0,
                    "saveState" : 5,
                    "restoreState" : 5,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "nCounted" : 0,
                    "nSkipped" : 0,
                    "inputStage" : {
                            "stage" : "COLLSCAN",
                            "filter" : {
                                    "name" : {
                                            "$eq" : "a"
                                    }
                            },
                            "nReturned" : 0,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 648,
                            "advanced" : 0,
                            "needTime" : 647,
                            "needYield" : 0,
                            "saveState" : 5,
                            "restoreState" : 5,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "direction" : "forward",
                            "docsExamined" : 646
                    }
            },
            "allPlansExecution" : [ ]
    },
    "serverInfo" : {
            "host" : "hnd2001",
            "port" : 27017,
            "version" : "3.2.6",
            "gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
    },
    "ok" : 1
}

1 个答案:

答案 0 :(得分:2)

您可以将 explain count 一起使用,如下所示:

db.runCommand({explain: {count: "collection", query:{}}})

如此jira task中所述。