MongoDB学习之Explain执行计划
mongodb 3.0和之前版本的explain执行计划有非常巨大的差距,这里只学习介绍3.0以后的用法
支持查看以下几种操作的执行计划

基本的使用方式
db.collection.find().explain(verbose)verbose是explain执行计划的输出模式有以下三种
queryPlanner
这是explain的默认输出方式
包含执行计划的基本详情信息,包含:查询计划、集合信息、查询条件、最佳执行计划、查询方式和mongodb的基础信息
executionStats
这种输出方式会在queryPlanner的基础上输出最佳执行计划的一些统计信息
allPlansExecution
获取所有的执行计划,也就是把集合中所有的每一种索引都生成一个执行计划内容,给与用户判断
详解explain字段
样例
下面是allPlansExecution模式下输出的执行计划,也包含了其它两种模式会输出的内容
{
"queryPlanner":{
"plannerVersion":1,
"namespace":"mock.users",
"indexFilterSet":false,
"parsedQuery":{
"age":{
"$gte":10
}
},
"winningPlan":{
"stage":"FETCH",
"inputStage":{
"stage":"IXSCAN",
"keyPattern":{
"age":1,
"username":1
},
"indexName":"age_1_username_1",
"isMultiKey":false,
"multiKeyPaths":{
"age":[
],
"username":[
]
},
"isUnique":false,
"isSparse":false,
"isPartial":false,
"indexVersion":2,
"direction":"forward",
"indexBounds":{
"age":[
"[10, inf.0]"
],
"username":[
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans":[
{
"stage":"FETCH",
"inputStage":{
"stage":"IXSCAN",
"keyPattern":{
"age":1
},
"indexName":"age_1",
"isMultiKey":false,
"multiKeyPaths":{
"age":[
]
},
"isUnique":false,
"isSparse":false,
"isPartial":false,
"indexVersion":2,
"direction":"forward",
"indexBounds":{
"age":[
"[10, inf.0]"
]
}
}
}
]
},
"executionStats":{ --这个集合是executionStats&allPlansExecution模式才有的
"executionSuccess":true,
"nReturned":680520,
"executionTimeMillis":1121,
"totalKeysExamined":680520,
"totalDocsExamined":680520,
"executionStages":{
"stage":"FETCH",
"nReturned":680520,
"executionTimeMillisEstimate":143,
"works":680521,
"advanced":680520,
"needTime":0,
"needYield":0,
"saveState":680,
"restoreState":680,
"isEOF":1,
"docsExamined":680520,
"alreadyHasObj":0,
"inputStage":{
"stage":"IXSCAN",
"nReturned":680520,
"executionTimeMillisEstimate":43,
"works":680521,
"advanced":680520,
"needTime":0,
"needYield":0,
"saveState":680,
"restoreState":680,
"isEOF":1,
"keyPattern":{
"age":1,
"username":1
},
"indexName":"age_1_username_1",
"isMultiKey":false,
"multiKeyPaths":{
"age":[
],
"username":[
]
},
"isUnique":false,
"isSparse":false,
"isPartial":false,
"indexVersion":2,
"direction":"forward",
"indexBounds":{
"age":[
"[10, inf.0]"
],
"username":[
"[MinKey, MaxKey]"
]
},
"keysExamined":680520,
"seeks":1,
"dupsTested":0,
"dupsDropped":0
}
},
"allPlansExecution":[ --这是allPlansExecution执行才会有的返回集合
{
"nReturned":101,
"executionTimeMillisEstimate":0,
"totalKeysExamined":101,
"totalDocsExamined":101,
"executionStages":{
.......
}
}
]
},
"serverInfo":{
"host":"supman",
"port":27017,
"version":"4.4.10",
"gitVersion":"58971da1ef93435a9f62bf4708a81713def6e88c"
},
"ok":1
}详解
queryPlannerplannerVersion执行计划版本namespace查询的集合indexFilterSet是否使用了索引过滤器(网上搜好多说是是否使用了索引很坑爹)parsedQuery查询条件winningPlan最佳执行计划stage查询方式状态 描述 COLLSCAN全表扫描 IXSCAN索引扫描 FETCH根据索引检索指定文档 SHARD_MERGE将各个分片返回数据进行合并 SORT在内存中进行了排序 LIMIT使用limit限制返回数 SKIP使用skip进行跳过 IDHACK对_id进行查询 SHARDING_FILTER通过mongos对分片数据进行查询 COUNTSCANcount不使用Index进行count时的stage返回 COUNT_SCANcount使用了Index进行count时的stage返回 SUBPLA未使用到索引的$or查询的stage返回 TEXT使用全文索引进行查询时候的stage返回 PROJECTION限定返回字段时候stage的返回 inputStage用来描述子stage,并且为其父stage提供文档和索引关键字,这里面含有着执行计划中比较主要的信息statgekeyPattern扫描的索引的内容indexName索引名称isMultiKey是否是索引数组,即索引建立在数组上则为true``MultiKeyPath若索引建立在数组上,显示索引的字段isUnique是否为唯一索引,这种索引所在的属性的每个值必须都是唯一不重复的,而且在创建时需要指明这是一个唯一索引db.users.createIndex({"username":1},{unique:true"})isSparse是否为稀疏索引,这种索引上面的字段可以不存在,一般用于可选字段创建的索引,比如说email这种在个人信息中可填可不填的字段,同样这种索引在创建的时候也需要指定db.users.createIndex({"email":1},{sparse:true"})isPartial是否为部分索引,这种索引只为满足筛选条件的数据创建索引db.users.createIndex({age:1},{partialFilterExpression:{age:{"$gte":25}}})如上,这种索引只在查询条件为
age ≥ 25的时候生效direction这里代表查询顺序,有两种情况forward或者backward,现在创建一个{age:1}索引db.users.find().sort() --这种情况是forward db.users.find().sort({age:-1}) --这种情况是backwardindexVersion索引版本indexBounds所扫描的索引范围,例如indexBounds: { age: [ '[36, 38]' ] } } }就是代表扫描[36,38]这个区间的age字段的索引rejectedPlans拒绝计划,非最优的执行计划,它的字段与winningPlan一样不再描述- .......
serverInfoMongoDB服务器信息host主机名称port端口version服务版本gitVersiongit版本号
executionStats包含一些统计信息executionSuccesss是否执行成nReturned表示返回的行数executionTimeMillis执行耗时,单位毫秒totalKeysExamined索引扫描次数totalDocsExamined文档扫描次数executionStages成功的计划详情,下面的很多字段在上面已经陈述过了,在这里就不再写了works工作单元数,一个查询会分解成很多小的查询单元advanced优先返回的结果数needTime未将中间结果推进到其父级的工作周期数needYield存储层查询产生锁的次数isEOF指定执行阶段是否达到流结束- ......
inputStageseeks为了完成索引扫描(stage),执行器必须将游标定位到新位置的次数- ......
allPlansExecution含有所有执行计划- .......
撒花结束
哈哈,终于搞完了还有几个字段没有搞清楚dupsTested、saveState,不过有了上面这些内容对整个执行计划的基本拿捏肯定不成问题
转载自:https://segmentfault.com/a/1190000041111851