查看查询计划、查询计划的执行统计的方法:db.collection.explain()、cursor.explain()、explain
explain输出默认只输出queryPlanner、serverInfo部分,如果需要输出executionStats,则可以指定explain(allPlansExecution)或者explain("executionStats")

queryPlanner:执行计划相关描述

    .COLLSCAN for a collection scan    .IXSCAN for scanning index keys    .FETCH for retrieving documents    .SHARD_MERGE for merging results from shards    .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的返回    .AND_SORTED 表示Index Intersection    .SORT 表示在内存中排序    .EOF 表示结果不存在

If MongoDB can use an index scan to obtain the requested sort order, the result will not include a SORT stage. Otherwise, if MongoDB cannot use the index to sort, the explain result will include a SORT stage.

样例
db.common_log_new.explain("allPlansExecution").find({ctime:{$lte:1529474045}}).limit(10)
{

    "queryPlanner" : {  --queryPlanner模式下并不会去真正进行query语句查询,而是针对query语句进行执行计划分析并选出winning plan。            "plannerVersion" : 1,            "namespace" : "dresslily.common_log_new",            "indexFilterSet" : false,   --指定mongodb是否应用索引过滤            "parsedQuery" : {                    "ctime" : {                            "$lte" : 1529474045   --具体的查询语句                    }            },            "winningPlan" : {   --查询优化器选择的最优执行计划                    "stage" : "LIMIT",  --将检索出来的文档做limit处理                    "limitAmount" : 10,                    "inputStage" : {   --描述子stage的文档,提供文档或者索引给他的父级stage,该值表示是否父级stage只有一个子节点                            "stage" : "FETCH",  --通过返回的index位置去检索具体的文档                            "inputStage" : {                                    "stage" : "IXSCAN", --索引扫描,这个地方可以判断该查询是通过索引还是全集合扫描数据                                    "keyPattern" : {   --所扫描的Index内容                                            "ctime" : 1                                    },                                    "indexName" : "idx_ctime",  --索引名称                                    "isMultiKey" : false,  --是否为多键索引                                    "isUnique" : false,    --是否为唯一索引                                    "isSparse" : false,    --是否为稀疏索引                                    "isPartial" : false,   --是否为部分索引                                    "indexVersion" : 1,                                    "direction" : "forward",  --query的查询顺序,如果用了.sort({w:-1})将显示backward。                                    "indexBounds" : {  --winningplan所扫描的索引范围                                            "ctime" : [                                                    "[-inf.0, 1529474045.0]"                                            ]                                    }                            }                    }            },            "rejectedPlans" : [ ]    },    "executionStats" : {  --query语句查询的过程            "executionSuccess" : true,            "nReturned" : 10,  --查询的返回条数            "executionTimeMillis" : 53, --整体执行时长            "totalKeysExamined" : 10,  --索引扫描条目数            "totalDocsExamined" : 10,  --document扫描条目数,如果索引可以覆盖查询,那么这个地方应该是0,比较好的结果是totalDocsExamined=totalKeysExamined            "executionStages" : {                    "stage" : "LIMIT",  --与queryPlanner.winningPlan.stage部分对应,影响totalKeysExamined与totalDocsExamined                    "nReturned" : 10,                    "executionTimeMillisEstimate" : 0,                    "works" : 11,                      "advanced" : 10,                    "needTime" : 0,                    "needYield" : 0,                    "saveState" : 0,                    "restoreState" : 0,                    "isEOF" : 1,                    "invalidates" : 0,                    "limitAmount" : 10,                    "inputStage" : {                            "stage" : "FETCH",                            "nReturned" : 10,                            "executionTimeMillisEstimate" : 0,                            "works" : 10,                            "advanced" : 10,                            "needTime" : 0,                            "needYield" : 0,                            "saveState" : 0,                            "restoreState" : 0,                            "isEOF" : 0,                            "invalidates" : 0,                            "docsExamined" : 10,                            "alreadyHasObj" : 0,                            "inputStage" : {                                    "stage" : "IXSCAN",                                    "nReturned" : 10,                                    "executionTimeMillisEstimate" : 0,                                    "works" : 10,                                    "advanced" : 10,                                    "needTime" : 0,                                    "needYield" : 0,                                    "saveState" : 0,                                    "restoreState" : 0,                                    "isEOF" : 0,                                    "invalidates" : 0,                                    "keyPattern" : {                                            "ctime" : 1                                    },                                    "indexName" : "idx_ctime",                                    "isMultiKey" : false,                                    "isUnique" : false,                                    "isSparse" : false,                                    "isPartial" : false,                                    "indexVersion" : 1,                                    "direction" : "forward",                                    "indexBounds" : {                                            "ctime" : [                                                    "[-inf.0, 1529474045.0]"                                            ]                                    },                                    "keysExamined" : 10,                                    "dupsTested" : 0,                                    "dupsDropped" : 0,                                    "seenInvalidated" : 0                            }                    }            },            "allPlansExecution" : [ ]    },    "serverInfo" : {            "host" : "dresslilypcmongodb01.globalerow.com",            "port" : 27017,            "version" : "3.2.15",            "gitVersion" : "e11e3c1b9c9ce3f7b4a79493e16f5e4504e01140"    },    "ok" : 1

}

收藏 打印