mongodb的explain


explain()是MongoDB的一個重要的查詢論斷工具,這個函數能夠提供大量的與查詢相關的信息,該函數會返回查詢計划、執行狀態、服務器信息,根據這些信息可以有針對性的對性能進行優化。

 

  1. explain()函數
  2. explain()返回信息
  3. explain()使用示例

1. explain()函數

 

cursor.explain(verbosity)

 

查看db.collection.find()的執行查詢計划信息時,explain()的使用方法如下:

 

db.collection.find().explain()

 

explain()方法的參數如下:

  • verbose:{String},可選參數。指定冗長模式的解釋輸出,方式指定后會影響explain()的行為及輸出信息。

    可選值有:"queryPlanner""executionStats""allPlansExecution",默認為"queryPlanner"

 

2. explain()返回信息

explain()的返回值有:

queryPlanner(查詢計划):查詢優化選擇的計划細節和被拒絕的計划。其可能包括以下值:
queryPlanner.namespace-一個字符串,運行查詢的指定命名空間
queryPlanner.indexFilterSet-一個布爾什,表示MongoDB在查詢中是否使用索引過濾
queryPlanner.winningPlan-由查詢優化選擇的計划文檔
winningPlan.stage-表示查詢階段的字符串
winningPlan.inputStage-表示子過程的文檔
winningPlan.inputStages-表示子過程的文檔數組
queryPlanner.rejectedPlans-被查詢優化備選並被拒絕的計划數組
executionStats,(執行狀態):被選中執行計划和被拒絕執行計划的詳細說明:
queryPlanner.nReturned-匹配查詢條件的文檔數
queryPlanner.executionTimeMillis-計划選擇和查詢執行所需的總時間(毫秒數)
queryPlanner.totalKeysExamined-掃描的索引總數
queryPlanner.totalDocsExamined-掃描的文檔總數
queryPlanner.totalDocsExamined-掃描的文檔總數
queryPlanner.executionStages-顯示執行成功細節的查詢階段樹
executionStages.works-指定查詢執行階段執行的“工作單元”的數量
executionStages.advanced-返回的中間結果數
executionStages.needTime-未將中間結果推進到其父級的工作周期數
executionStages.needYield-存儲層要求查詢系統產生的鎖的次數
executionStages.isEOF-指定執行階段是否已到達流結束
queryPlanner.allPlansExecution-包含在計划選擇階段期間捕獲的部分執行信息,包括選擇計划和拒絕計划
serverInfo,(服務器信息):MongoDB實例的相關信息:
serverInfo.winningPlan-使用的執行計划
winningPlan.shards-包括每個訪問片的queryPlanner和serverInfo的文檔數組

 

 

3. explain()使用示例

有一個users集合,現查詢其'status'值為'1'的數據,並查看執行情況:

> db.users.find({status:1}).explain()
{
  "cursor" : "BasicCursor",
  "isMultiKey" : false,
  "n" : 1,
  "nscannedObjects" : 1,
  "nscanned" : 1,
  "nscannedObjectsAllPlans" : 1,
  "nscannedAllPlans" : 1,
  "scanAndOrder" : false,
  "indexOnly" : false,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "millis" : 9,
  "server" : "localhost:27017",
  "filterSet" : false
}

 

 

 

glc-test:PRIMARY> db.galleryimg.find().explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "gallery.galleryimg",
                "indexFilterSet" : false,
                "parsedQuery" : {

                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 7598,
                "executionTimeMillis" : 195,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 7598,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "nReturned" : 7598,
                        "executionTimeMillisEstimate" : 58,
                        "works" : 7600,
                        "advanced" : 7598,
                        "needTime" : 1,
                        "needYield" : 0,
                        "saveState" : 59,
                        "restoreState" : 59,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 7598
                }
        },
        "serverInfo" : {
                "host" : "xxx",
                "port" : 28042,
                "version" : "4.0.17-10",
                "gitVersion" : "c7a95fa71cb1a23bc9b73401b178dfb7cff8cc7d"
        },
        "ok" : 1,
        "operationTime" : Timestamp(1604636678, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1604636678, 1),
                "signature" : {
                        "hash" : BinData(0,"++H4QYguEzP2QqAKEycUwT7izww="),
                        "keyId" : NumberLong("6856584343653974019")
                }
        }
}

 

 

 

 

 

 

 

 

 

 

###################################################################


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM