"collection.count()"を使ってコレクション内のドキュメントの数を知りたいですが、mongodbがInnoDBのようなフルテーブルスキャンを実行することを心配しています。条件なしのMongoDBカウント - すぐに戻るか、フルテーブルをスキャンしますか?
は、ここに私のコードです:
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はテーブル全体をスキャンします。
UPDATE:
ジョアンの提案によると、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
}
心配ですか?何が実験からあなたを止める?基準! – Saleem