簡介
MongoDB的 explain() 函數可以幫助我們查看查詢相關的信息,這有助於快速定位查詢瓶頸。
基本用法
基本命令
db.dev.find({name:/^zhang/, age:{$gt:15}}).explain()
直接跟在 find() 函數后面,表示查看 find() 函數的執行計划,結果如下:

{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "xwj_db.dev", "indexFilterSet" : false, "parsedQuery" : { "$and" : [ { "age" : { "$gt" : 15.0 } }, { "name" : { "$regex" : "^zhang" } } ] }, "queryHash" : "ADEBB940", "planCacheKey" : "5F8D7E6C", "winningPlan" : { "stage" : "FETCH", "filter" : { "age" : { "$gt" : 15.0 } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "name" : 1.0 }, "indexName" : "idx_name", "isMultiKey" : false, "multiKeyPaths" : { "name" : [] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "name" : [ "[\"zhang\", \"zhanh\")", "[/^zhang/, /^zhang/]" ] } } }, "rejectedPlans" : [] }, "serverInfo" : { "host" : "mongo", "port" : 27017, "version" : "4.2.11", "gitVersion" : "ea38428f0c6742c7c2c7f677e73d79e17a2aab96" }, "ok" : 1.0 }
返回結果包含兩大塊信息:一個是 queryPlanner,即查詢計划,還有一個是serverInfo,即MongoDB服務的一些信息。
查詢計划 queryPlanner 中,參數比較多,挑幾個比較重要的參數說明下:
參數 | 含義 |
plannerVersion | 查詢計划版本 |
namespace | 要查詢的集合 |
indexFilterSet | 是否應用了index filter |
parsedQuery | 查詢條件 |
winningPlan | 最佳執行計划 |
winningPlan.stage | 查詢方式,常見的有: COLLSCAN/全表掃描、IXSCAN/索引掃描、FETCH/根據索引去檢索文檔、 SHARD_MERGE/合並分片結果、IDHACK/針對_id進行查詢、SORT/在內存中進行排序 |
winningPlan.filter | 過濾條件 |
winningPlan.inputStage.keyPattern | 索引規則。這里是name正序 |
winningPlan.inputStage.indexName | 索引名稱 |
winningPlan.inputStage.isMultiKey | 本次查詢是否使用了多鍵、復合索引 |
winningPlan.inputStage.direction | 查詢順序:正序是forward,倒序是backward |
winningPlan.inputStage.direction | 所掃描的索引范圍 |
參數
explain() 函數也可以接收不同參數,通過設置不同參數可以查看更詳細的查詢計划。
參數包括:queryPlanner(缺省)、executionStats、allPlansExecution
執行explain("executionStats"),會發現執行計划中多了一些統計信息(其它信息跟上面一樣,這里省略),如下:

"executionStats" : { "executionSuccess" : true, "nReturned" : 1, "executionTimeMillis" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 3, "executionStages" : { "stage" : "FETCH", "filter" : { "age" : { "$gt" : 15.0 } }, "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 1, "needTime" : 2, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 3, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 3, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 3, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "name" : 1.0 }, "indexName" : "idx_name", "isMultiKey" : false, "multiKeyPaths" : { "name" : [] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "name" : [ "[\"zhang\", \"zhanh\")", "[/^zhang/, /^zhang/]" ] }, "keysExamined" : 3, "seeks" : 1, "dupsTested" : 0, "dupsDropped" : 0 } } }
executionStats參數含義:
參數 | 含義 |
totalKeysExamined | 索引掃描次數 |
totalDocsExamined | 文檔掃描次數 |
nReturned | 返回的結果數 |
executionTimeMillis | 執行耗時 |
executionSuccess | 是否執行成功 |