做为近年最为火热的文档型数据库,MongoDB受到了愈来愈多人的关注,可是因为国内的MongoDB相关技术分享屈指可数,很多朋友向我抱怨无从下手。数据库
《MongoDB干货系列》将从实际应用的角度来进行MongoDB的一些列干货的分享,将覆盖调优,troubleshooting等方面,但愿能对你们带来帮助。架构
若是但愿了解更多MongoDB基础的信息,还请你们Google下。fetch
要保证数据库处于高效、稳定的状态,除了良好的硬件基础、高效高可用的数据库架构、贴合业务的数据模型以外,高效的查询语句也是不可少的。那么,如何查看并判断咱们的执行计划呢?咱们今天就来谈论下MongoDB的执行计划分析。优化
MongoDB 3.0以后,explain的返回与使用方法与以前版本有了很多变化,介于3.0以后的优秀特点,本文仅针对MongoDB 3.0+的explain进行讨论。spa
现版本explain有三种模式,分别以下:rest
queryPlannercode
executionStats索引
allPlansExecutionip
因为文章字数缘由,本系列将分为三个部分。
第一部分
第二部分
第三部分文档
本文是第一部分,主要针对queryPlanner,与executionStats进行分析。
queryPlanner是现版本explain的默认模式,queryPlanner模式下并不会去真正进行query语句查询,而是针对query语句进行执行计划分析并选出winning plan。
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "game_db.game_user", "indexFilterSet" : false, "parsedQuery" : { "w" : { "$eq" : 1 } }, "winningPlan" : { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "w" : 1, "n" : 1 }, "indexName" : "w_1_n_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "w" : [ "[1.0, 1.0]" ], "n" : [ "[MinKey, MaxKey]" ] } } }, "rejectedPlans" : [ { "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "w" : 1, "v" : 1 }, "indexName" : "w_1_v_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "w" : [ "[1.0, 1.0]" ], "v" : [ "[MinKey, MaxKey]" ] } } } ] },
先来看queryPlanner模式的各个返回意义。
explain.queryPlanner
queryPlanner的返回。
explain.queryPlanner.namespace
顾名思义,该值返回的是该query所查询的表。
explain.queryPlanner.indexFilterSet
针对该query是否有indexfilter(会在后文进行详细解释)。
explain.queryPlanner.winningPlan
查询优化器针对该query所返回的最优执行计划的详细内容。
explain.queryPlanner.winningPlan.stage
最优执行计划的stage,这里返回是FETCH,能够理解为经过返回的index位置去检索具体的文档(stage有数个模式,将在后文中进行详解)。
explain.queryPlanner.winningPlan.inputStage
explain.queryPlanner.winningPlan.stage的child stage,此处是IXSCAN,表示进行的是index scanning。
explain.queryPlanner.winningPlan.keyPattern
所扫描的index内容,此处是w:1与n:1。
explain.queryPlanner.winningPlan.indexName
winning plan所选用的index。
explain.queryPlanner.winningPlan.isMultiKey
是不是Multikey,此处返回是false,若是索引创建在array上,此处将是true。
explain.queryPlanner.winningPlan.direction
此query的查询顺序,此处是forward,若是用了.sort({w:-1})将显示backward。
explain.queryPlanner.winningPlan.indexBounds
winningplan所扫描的索引范围,此处查询条件是w:1,使用的index是w与n的联合索引,故w是[1.0,1.0]而n没有指定在查询条件中,故是[MinKey,MaxKey]。
explain.queryPlanner.rejectedPlans
其余执行计划(非最优而被查询优化器reject的)的详细返回,其中具体信息与winningPlan的返回中意义相同,故不在此赘述。
executionStats的返回中多了以下:
"executionStats" : { "executionSuccess" : true, "nReturned" : 29861, "executionTimeMillis" : 23079, "totalKeysExamined" : 29861, "totalDocsExamined" : 29861, "executionStages" : { "stage" : "FETCH", "nReturned" : 29861, "executionTimeMillisEstimate" : 22685, "works" : 29862, "advanced" : 29861, "needTime" : 0, "needFetch" : 0, "saveState" : 946, "restoreState" : 946, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 29861, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 29861, "executionTimeMillisEstimate" : 70, "works" : 29862, "advanced" : 29861, "needTime" : 0, "needFetch" : 0, "saveState" : 946, "restoreState" : 946, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "w" : 1, "n" : 1 }, "indexName" : "w_1_n_1", "isMultiKey" : false, "direction" : "forward", "indexBounds" : { "w" : [ "[1.0, 1.0]" ], "n" : [ "[MinKey, MaxKey]" ] }, "keysExamined" : 29861, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0, "matchTested" : 0 } } },
executionStats模式中,咱们主要须要注意的返回有以下几个
executionStats.executionSuccess
是否执行成功
executionStats.nReturned
查询的返回条数
executionStats.executionTimeMillis
总体执行时间
executionStats.totalKeysExamined
索引扫描次数
executionStats.totalDocsExamined
document扫描次数
以上几个很是好理解,咱们就不在这里详述,后文的案例中会有分析。
executionStats.executionStages.stage
这里是FETCH去扫描对于documents
executionStats.executionStages.nReturned
因为是FETCH,因此这里该值与executionStats.nReturned一致
executionStats.executionStages.docsExamined
与executionStats.totalDocsExamined一致
executionStats.inputStage中的与上述理解方式相同
还有一些文档中没有描述的返回如:
“works” : 29862,
“advanced” : 29861,
“isEOF” : 1,
这些值都会在explan之初初始化:
mongo/src/mongo/db/exec/plan_stats.h
struct CommonStats { CommonStats(const char* type) : stageTypeStr(type), works(0), yields(0), unyields(0), invalidates(0), advanced(0), needTime(0), needYield(0), executionTimeMillis(0), isEOF(false) {}
以works为例,查看源码中发现,每次操做会加1,且会把执行时间记录在executionTimeMillis中。
mongo/src/mongo/db/exec/fetch.cpp
++_commonStats.works; // Adds the amount of time taken by work() to executionTimeMillis. ScopedTimer timer(&_commonStats.executionTimeMillis);
而在查询结束EOF,works又会加1,advanced不加。
mongo/src/mongo/db/exec/eof.cpp
PlanStage::StageState EOFStage::work(WorkingSetID* out) { ++_commonStats.works; // Adds the amount of time taken by work() to executionTimeMillis. ScopedTimer timer(&_commonStats.executionTimeMillis); return PlanStage::IS_EOF; }
故正常的返回works会比nReturned多1,这时候isEOF为true(1):
mongo/src/mongo/db/exec/eof.cpp
bool EOFStage::isEOF() { return true; } unique_ptr<PlanStageStats> EOFStage::getStats() { _commonStats.isEOF = isEOF(); return make_unique<PlanStageStats>(_commonStats, STAGE_EOF); }
advanced的返回值在命中的时候+1,在skip,eof的时候不会增长如:
mongo/src/mongo/db/exec/skip.cpp
if (PlanStage::ADVANCED == status) { // If we're still skipping results... if (_toSkip > 0) { // ...drop the result. --_toSkip; _ws->free(id); ++_commonStats.needTime; return PlanStage::NEED_TIME; } *out = id; ++_commonStats.advanced; return PlanStage::ADVANCED;
第一部分完。
下一部分将针对IndexFilter与Stage进行分析。
尽请期待。