cursor.explain()
- 可以在db.collection.find()加入explain()獲取計劃資訊
- explain()獲取資訊模式有三種
- queryPlanner: 獲取查詢資訊,預設模式
- executionStats: 除了queryPlanner資訊外,額外提供winning plan訊息
- allPlansExecution: 提供queryPlanner和executionStats,額外提供候選計畫
獲得資訊可由STAGE得知索引使用狀況
- COLLSCAN: 集合掃描,沒有使用到索引查詢
- IXSCAN: 有用到索引查詢
- FETCH: 檢索文件
queryPlanner相關資訊
- explain.queryPlanner.namespace: 查詢命名空間
- explain.queryPlanner.indexFilterSet: 是否應用索引過濾器(可忽略)
- explain.queryPlanner.queryHash: mongodb用來查詢的16進制字串(不會異動)
- explain.queryPlanner.planCacheKey: 當前可用索引含數(會異動)
- explain.queryPlanner.winningPlan: 為有效查詢樹狀結構,子結構存在於inputStage,有多個子結構則會存在於inputStages集合
- explain.queryPlanner.winningPlan.stage: 該階段查詢索引使用狀況
- explain.queryPlanner.winningPlan.inputStage: 子結構索引使用狀況
- explain.queryPlanner.winningPlan.inputStages: 子結構集合索引使用狀況
executionStats相關資訊
- explain.executionStats.nReturned: 符合查詢條件文件數量
- explain.executionStats.executionTimeMillis: 查詢耗費時間
- explain.executionStats.totalKeysExamined: 掃描索引數量
- explain.executionStats.totalDocsExamined: 審查的文件總數,非返回文件的數量(索引精準度)
- explain.executionStats.executionStages.works: 查詢所應用到的工作單位
- explain.executionStats.executionStages.inputStage.keysExamined: 非索引查詢使用的數量
- explain.executionStats.executionStages.inputStage.docsExamined: 非索引查詢文檔數
參考來源(explain)
參考來源(explain結果)