db.getCollection(‘collName’).find({“uuid”:”uuid”,”status”:0}).explain(“executionStats”)
在语句后增加.explain(“executionStats”)
explain结果说明
queryPlanner(查询计划):查询优化选择的计划细节和被拒绝的计划。其可能包括以下值:queryPlanner.namespace-一个字符串,运行查询的指定命名空间queryPlanner.indexFilterSet-一个布尔什,表示MongoDB在查询中是否使用索引过滤queryPlanner.winningPlan-由查询优化选择的计划文档winningPlan.stage-表示查询阶段的字符串winningPlan.inputStage-表示子过程的文档winningPlan.inputStages-表示子过程的文档数组queryPlanner.rejectedPlans-被查询优化备选并被拒绝的计划数组executionStats,(执行状态):被选中执行计划和被拒绝执行计划的详细说明:queryPlanner.nReturned-匹配查询条件的文档数queryPlanner.executionTimeMillis-计划选择和查询执行所需的总时间(毫秒数)queryPlanner.totalKeysExamined-扫描的索引总数queryPlanner.totalDocsExamined-扫描的文档总数queryPlanner.totalDocsExamined-扫描的文档总数queryPlanner.executionStages-显示执行成功细节的查询阶段树executionStages.works-指定查询执行阶段执行的“工作单元”的数量executionStages.advanced-返回的中间结果数executionStages.needTime-未将中间结果推进到其父级的工作周期数executionStages.needYield-存储层要求查询系统产生的锁的次数executionStages.isEOF-指定执行阶段是否已到达流结束queryPlanner.allPlansExecution-包含在计划选择阶段期间捕获的部分执行信息,包括选择计划和拒绝计划serverInfo,(服务器信息):MongoDB实例的相关信息:serverInfo.winningPlan-使用的执行计划winningPlan.shards-包括每个访问片的queryPlanner和serverInfo的文档数组
queryPlanner
winningPlan.stage:最优执行计划的stage
winningPlan.inputStage:用来描述子stage,并且为其父stage提供文档和索引关键字
winningPlan.isMultiKey是否是Multikey,此处返回是false,如果索引建立在array上,此处将是true
executionStats
最理想的状态是nReturned=totalKeysExamined=totalDocsExamined
executionTimeMillis:查询用时
nReturned:查询返回的条目
totalKeysExamined:索引扫描条目
totalDocsExamined:文档扫描条目
executionStages—-
stage:
stage常用类型
COLLSCAN:全表扫描
IXSCAN:索引扫描
FETCH:根据索引去检索指定document
SHARD_MERGE:将各个分片返回数据进行merge
SORT:表明在内存中进行了排序
LIMIT:使用limit限制返回数
SKIP:使用skip进行跳过
IDHACK:针对_id进行查询
SHARDING_FILTER:通过mongos对分片数据进行查询
COUNT:利用db.coll.explain().count()之类进行count运算
COUNTSCAN:count不使用Index进行count时的stage返回
COUNT_SCAN:count使用了Index进行count时的stage返回
SUBPLA:未使用到索引的$or查询的stage返回
TEXT:使用全文索引进行查询时候的stage返回
PROJECTION:限定返回字段时候stage的返回
stage使用指引
对于普通查询,常用stage的组合(查询的时候尽可能用上索引):
Fetch+IDHACK
Fetch+ixscan
Limit+(Fetch+ixscan)
PROJECTION+ixscan
SHARDING_FITER+ixscan
COUNT_SCAN
不希望看到包含如下的stage:
COLLSCAN(全表扫描)
SORT(使用sort但是无index)
不合理的SKIP
SUBPLA(未用到index的$or)
COUNTSCAN(不使用index进行count)
serverInfo
服务器信息包括host、port、version
/* 1 */{"queryPlanner" : {"plannerVersion" : 1,"namespace" : "db.collName","indexFilterSet" : false,"parsedQuery" : {"$and" : [{"status" : {"$eq" : 0.0}},{"uuid" : {"$eq" : "uuid"}}]},"winningPlan" : {"stage" : "FETCH","filter" : {"status" : {"$eq" : 0.0}},"inputStage" : {"stage" : "IXSCAN","keyPattern" : {"uuid" : 1.0},"indexName" : "uuid_1_app_id_1","isMultiKey" : false,"multiKeyPaths" : {"uuid" : []},"isUnique" : false,"isSparse" : false,"isPartial" : false,"indexVersion" : 2,"direction" : "forward","indexBounds" : {"uuid" : ["[\"uuid\", \"uuid\"]"]}}},"rejectedPlans" : [{"stage" : "FETCH","filter" : {"$and" : [{"status" : {"$eq" : 0.0}}]},"inputStage" : {"stage" : "IXSCAN","keyPattern" : {"uuid" : 1.0},"indexName" : "uuid_1","isMultiKey" : false,"multiKeyPaths" : {"uuid" : []},"isUnique" : false,"isSparse" : false,"isPartial" : false,"indexVersion" : 2,"direction" : "forward","indexBounds" : {"uuid" : ["[\"uuid\", \"uuid\"]"]}}}]},"executionStats" : {"executionSuccess" : true,"nReturned" : 0,"executionTimeMillis" : 0,"totalKeysExamined" : 0,"totalDocsExamined" : 0,"executionStages" : {"stage" : "FETCH","filter" : {"status" : {"$eq" : 0.0}},"nReturned" : 0,"executionTimeMillisEstimate" : 0,"works" : 2,"advanced" : 0,"needTime" : 0,"needYield" : 0,"saveState" : 0,"restoreState" : 0,"isEOF" : 1,"invalidates" : 0,"docsExamined" : 0,"alreadyHasObj" : 0,"inputStage" : {"stage" : "IXSCAN","nReturned" : 0,"executionTimeMillisEstimate" : 0,"works" : 1,"advanced" : 0,"needTime" : 0,"needYield" : 0,"saveState" : 0,"restoreState" : 0,"isEOF" : 1,"invalidates" : 0,"keyPattern" : {"uuid" : 1.0,"app_id" : 1.0},"indexName" : "uuid_1_app_id_1","isMultiKey" : false,"multiKeyPaths" : {"uuid" : [],"app_id" : []},"isUnique" : false,"isSparse" : false,"isPartial" : false,"indexVersion" : 2,"direction" : "forward","indexBounds" : {"uuid" : ["[\"uuid\", \"uuid\"]"]},"keysExamined" : 0,"seeks" : 1,"dupsTested" : 0,"dupsDropped" : 0,"seenInvalidated" : 0}}},"serverInfo" : {"host" : "9f5efce339e6","port" : 27017,"version" : "3.6.11","gitVersion" : "b4339db12bf57ffee5b84a95c6919dbd35fe31c9"},"ok" : 1.0}
