最近几年,nosql数据库发展迅猛,mongo无疑是最闪耀的那颗明星;以前我们部门的系统,用到数据库时基本上mysql是标配;现在越来越多的项目都开始选择mongo(无论自己搭建还是使用sa的ocean);无论是mysql还是mongo,数据库是一个系统最容易出现问题、瓶颈的地方。
mysql出现问题时,相信大家都有一套完善的调试、调优方法,从最基础的查看slow log,query log到mysql explain查询索引分析等;而由于在mongo方面的技术积累没有mysql那么多,出现性能问题时,往往需要去花很大的精力进行调优。
mongo中索引跟mysql中索引同样重要,没有索引,每次查找都需要遍历全表。
mongo的索引类型包括如下几种:
索引的一些特征:
与mysql相同,mongo也可以通过使用explain命令来查看mongo的执行情况,不同的是mongo的explain输出要复杂的多,mongo3.0版本对于explain做了很大的调整,本文只讨论3.0以后版本的explain情况。
包括count, distinct, group, find, findAndModify, delete,update等操作均可以执行explain。
explain有三种执行模式:
shell环境下可以通过db.collection.explain()、cursor.explain()、db.runCommand()三种方法来执行explain,pymongo可以通过db.command、cursor.explain()来获取explain的结果。
注意:aggrgate仅仅会在queryplanner模式下运行explain
##explain结果分析
一个典型的explain结果可能如下所示:
{
"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]"
]
}
}
}
]
},
各字段的意义如下所示:
如果在executionStats模式或者allPlansExecution模式下执行explain,其结果中还会包含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
}
}
},
其中重要字段含义如下:
stage作为explain结果非常重要的一个字段,指明目前处于哪个阶段,常见的stage有:COLLSCAN(全表扫描), IXSCAN(索引扫描), FETCH(根据索引去检索指定document), SHARD_MERGE(将各个分片返回数据进行merge), SORT(在内存中进行排序), LIMIT(使用limit限制返回数), SKIP(使用skip进行跳过), TEXT(全文索引进行查询), PROJECTION(限定返回字段), IDHACK(针对_id进行查询)等等
对于普通查询,我们最希望看到的组合有这些:
无论哪种模式,explain的结果中都会有winning plan的信息,这些winning plan是通过mongo查询分析器获得的,查询分析器会缓存winning plan的信息,所以queryplanner模式的explain执行速度很快。
下面这幅图说明了查询分析器的执行逻辑:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。