您好,登錄后才能下訂單哦!
Explain and Index
explain有三種模式,分別是:queryPlanner、executionStats、allPlansExecution。現實開發中,常用executionStats模式,本例分析這種模式。
> db.createCollection("person")
{ "ok" : 1 }
> for(var i=0;i<2000000;i++) {
... db.person.insert({"name":"ryan"+i,"age":i})
... }
WriteResult({ "nInserted" : 1 })
> db.person.find({"age":{"$lte":2000}}).explain("executionStats")
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.person", "indexFilterSet" : false, "parsedQuery" : { "age" : { "$lte" : 2000 } }, "winningPlan" : { "stage" : "COLLSCAN", "COLLSCAN"表示全表掃描 "filter" : { "age" : { "$lte" : 2000 } }, "direction" : "forward" }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 2001, 表示查詢返回的條目數 "executionTimeMillis" : 741, 表示執行語句的執行時間 "totalKeysExamined" : 0, "totalDocsExamined" : 2000000, 表示 文檔掃描的條目數 "executionStages" : { "stage" : "COLLSCAN", "filter" : { "age" : { "$lte" : 2000 } }, "nReturned" : 2001, "executionTimeMillisEstimate" : 530, 表示整體的查詢時間 "works" : 2000002, "advanced" : 2001, "needTime" : 1998000, "needYield" : 0, "saveState" : 15625, "restoreState" : 15625, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 2000000 } }, "serverInfo" : { "host" : "meteor.yeecall.com", "port" : 27027, "version" : "3.2.8", "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0" }, "ok" : 1 }
> db.person.ensureIndex({age:1}) 創建索引,“1”:表示按照age進行升序,“-1”:表示按照age進行降序
創建索引時ensureIndex({field:1},{name:"indexName"}) 可以指定索引的名稱
也可以后臺創建索引: db.collection.ensureIndex({name:-1},{background:true})
{ "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
> db.person.find({"age":{"$lte":2001}}).explain("executionStats")
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "test.person", 返回所查詢的表 "indexFilterSet" : false, 針對該query是否有indexfilter "parsedQuery" : { "age" : { "$lte" : 2001 } }, "winningPlan" : { 查詢優化器針對該query所返回的最優執行計劃的詳細內容 "stage" : "FETCH", "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 1, "direction" : "forward", "indexBounds" : { "age" : [ "[-inf.0, 2001.0]" ] } } }, "rejectedPlans" : [ ] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 2002, 表示本次查詢返回的條目數 "executionTimeMillis" : 28, 表示本次query整體查詢的時間 "totalKeysExamined" : 2002, 表示索引掃描的條目數 "totalDocsExamined" : 2002, 表示文檔掃描的條目數 "executionStages" : { "stage" : "FETCH", "nReturned" : 2002, "executionTimeMillisEstimate" : 10, 表示本次查詢根據index檢索document獲得結果數據的時間 "works" : 2003, "advanced" : 2002, "needTime" : 0, "needYield" : 0, "saveState" : 15, "restoreState" : 15, "isEOF" : 1, "invalidates" : 0, "docsExamined" : 2002, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 2002, "executionTimeMillisEstimate" : 0, 表示該查詢掃描2002行index所用的時間 "works" : 2003, "advanced" : 2002, "needTime" : 0, "needYield" : 0, "saveState" : 15, "restoreState" : 15, "isEOF" : 1, "invalidates" : 0, "keyPattern" : { "age" : 1 }, "indexName" : "age_1", "isMultiKey" : false, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 1, "direction" : "forward", "indexBounds" : { "age" : [ "[-inf.0, 2001.0]" ] }, "keysExamined" : 2002, "dupsTested" : 0, "dupsDropped" : 0, "seenInvalidated" : 0 } } }, "serverInfo" : { "host" : "meteor.yeecall.com", "port" : 27027, "version" : "3.2.8", "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0" }, "ok" : 1 }
>
影響 totalKeysExamined 和 totalDocsExamined 的是 stage 類型。
類型列舉如下:
COLLSCAN:全表掃描
IXSCAN:索引掃描
FETCH:根據索引去檢索指定document
SHARD_MERGE:將各個分片返回數據進行merge
SORT:表明在內存中進行了排序
LIMIT:使用limit限制返回數
SKIP:使用skip進行跳過
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的返回
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。