1 為什么要執行explain,什么時候執行
explain的目的是將mongo的黑盒操作白盒化。
比如查詢很慢的時候想知道原因。
2 explain的三種模式
2.1 queryPlanner
不會真正的執行查詢,只是分析查詢,選出winning plan。
2.2 executionStats
返回winning plan的關鍵數據。
executionTimeMillis該query查詢的總體時間。
2.3 allPlansExecution
執行所有的plans。
通過explain("executionStats")來選擇模式,默認是第一種模式。
3 queryPlanner分析
3.1 namespace
本次所查詢的表。
3.2 indexFilterSet
是否使用partial index,比如只對某個表中的部分文檔進行index。
3.3 parsedQuery
本次執行的查詢
3.4 winning plan
3.4.1 stage
COLLSCAN:全表掃描
IXSCAN:索引掃描
FETCH:根據索引去檢索指定document
SHARD_MERGE:將各個分片返回數據進行merge
SORT:表明在內存中進行了排序
LIMIT:使用limit限制返回數
SKIP:使用skip進行跳過
IDHACK:針對_id進行查詢
winning plan的stage是一個樹狀結構,最外面的stage是根節點,然后inputStage或者inputStages里面的stage是它的子stage。
The explain results present the query plans as a tree of stages. Each stage passes its results (i.e. documents or index keys) to the parent node. The leaf nodes access the collection or the indices. The internal nodes manipulate the documents or the index keys that result from the child nodes. The root node is the final stage from which MongoDB derives the result set.
explain.queryPlanner.winningPlan.stage A string that denotes the name of the stage.
Each stage consists of information specific to the stage. For instance, an IXSCAN stage will include the index bounds along with other data specific to the index scan. If a stage has a child stage or multiple child stages, the stage will have an inputStage or inputStages.
explain.queryPlanner.winningPlan.inputStage A document that describes the child stage, which provides the documents or index keys to its parent. The field is present if the parent stage has only one child.
3.4.2 filter
對子stage返回的結果進行過濾,filter給本stage指明了fetch的條件。
3.4.3 inputStage
用於容納一個子stage。
3.4.3.1 stage
子stage
3.4.3.2 indexName
所使用的索引的名字。
3.4.3.3 indexBounds
索引查找時使用的范圍。
4 db.getCollection("AndroidDataReport").getIndexes()解析
每個json是一個索引,如果key有多個就是復合索引,復合索引的順序很重要。