Mongodb查询执行时间
Posted
技术标签:
【中文标题】Mongodb查询执行时间【英文标题】:Mongodb query execution time 【发布时间】:2019-04-27 11:58:00 【问题描述】:我在下面使用了 Query 花费了很多时间
查询
db.saleOrder.find("currentStatus._id":"147","_id":1).limit(10).explain("executionStats")
ExecutionStats 结果
"queryPlanner" :
"plannerVersion" : 1,
"namespace" : "db_erp_tube.saleOrder",
"indexFilterSet" : false,
"parsedQuery" :
"currentStatus._id" :
"$eq" : "147"
,
"winningPlan" :
"stage" : "LIMIT",
"limitAmount" : 10,
"inputStage" :
"stage" : "PROJECTION",
"transformBy" :
"_id" : 1
,
"inputStage" :
"stage" : "COLLSCAN",
"filter" :
"currentStatus._id" :
"$eq" : "147"
,
"direction" : "forward"
,
"rejectedPlans" : [ ]
,
"executionStats" :
"executionSuccess" : true,
"nReturned" : 10,
"executionTimeMillis" : 8673,
"totalKeysExamined" : 0,
"totalDocsExamined" : 3458482,
"executionStages" :
"stage" : "LIMIT",
"nReturned" : 10,
"executionTimeMillisEstimate" : 8460,
"works" : 3458484,
"advanced" : 10,
"needTime" : 3458473,
"needYield" : 0,
"saveState" : 27019,
"restoreState" : 27019,
"isEOF" : 1,
"invalidates" : 0,
"limitAmount" : 10,
"inputStage" :
"stage" : "PROJECTION",
"nReturned" : 10,
"executionTimeMillisEstimate" : 8450,
"works" : 3458483,
"advanced" : 10,
"needTime" : 3458473,
"needYield" : 0,
"saveState" : 27019,
"restoreState" : 27019,
"isEOF" : 0,
"invalidates" : 0,
"transformBy" :
"_id" : 1
,
"inputStage" :
"stage" : "COLLSCAN",
"filter" :
"currentStatus._id" :
"$eq" : "147"
,
"nReturned" : 10,
"executionTimeMillisEstimate" : 8400,
"works" : 3458483,
"advanced" : 10,
"needTime" : 3458473,
"needYield" : 0,
"saveState" : 27019,
"restoreState" : 27019,
"isEOF" : 0,
"invalidates" : 0,
"direction" : "forward",
"docsExamined" : 3458482
,
"serverInfo" :
"host" : "172.16.109",
"port" : 27017,
"version" : "4.0.0",
"gitVersion" : "3b07af3d4f471ae89e8186d33bbb1d5259597d51"
,
"ok" : 1,
"operationTime" : Timestamp(1556365275, 114),
"$clusterTime" :
"clusterTime" : Timestamp(1556365275, 114),
"signature" :
"hash" : BinData(0,"ppu91nKmeiC//+UvdsEbjrBTDLU="),
"keyId" : NumberLong("6633468944474701825")
【问题讨论】:
尝试索引 currentStatus._id 然后检查 stat! 【参考方案1】:该查询的执行时间超过 8 秒(8673 毫秒),因为它必须在返回 10 个文档之前扫描 saleOrder 集合中的所有 3458482 个文档。
这在解释输出的过滤阶段进行了说明。
"inputStage" :
"stage" : "COLLSCAN",
"filter" :
"currentStatus._id" :
"$eq" : "147"
,
COLLSCAN 表示全集合扫描,
"totalDocsExamined" : 3458482,
这是在返回结果之前扫描的文档数。
"executionTimeMillis" : 8673,
这是运行查询所用的总时间。
正如@the_mahasagar 建议的那样,在 currentStatus._id 上建立索引可以大大加快查询速度,请使用以下命令。
db.saleOrder.createIndex( "currentStatus._id": 1)
【讨论】:
以上是关于Mongodb查询执行时间的主要内容,如果未能解决你的问题,请参考以下文章