cursor.explain(“executionStats”)和 db.collection.explain(“executionStats”) 方法提供關于查詢性能的相關信息。這些信息可用于衡量查詢是否使用了索引以及如何使用索引。
db.collection.explain() 還提供有關其他操作的執行信息。例如 db.collection.update()。 有關詳情信息,請參見 db.collection.explain() 。
評價查詢性能
考慮采用以下的 inventory 集合文檔:
db.inventory.insert([{ "_id" : 1, "item" : "f1", type: "food", quantity: 500 },{ "_id" : 2, "item" : "f2", type: "food", quantity: 100 },{ "_id" : 3, "item" : "p1", type: "paper", quantity: 200 },{ "_id" : 4, "item" : "p2", type: "paper", quantity: 150 },{ "_id" : 5, "item" : "f3", type: "food", quantity: 300 },{ "_id" : 6, "item" : "t1", type: "toys", quantity: 500 },{ "_id" : 7, "item" : "a1", type: "apparel", quantity: 250 },{ "_id" : 8, "item" : "a2", type: "apparel", quantity: 400 },{ "_id" : 9, "item" : "t2", type: "toys", quantity: 50 },{ "_id" : 10, "item" : "f4", type: "food", quantity: 75 }
]);
不使用索引查詢
以下查詢返回 quantity 值在 100 到 200 之間(含)的文檔:
db.inventory.find( { quantity: { $gte: 100, $lte: 200 } } )
將cursor.explain(“executionStats”)游標方法拼接到find 命令的結尾,顯示查詢選擇的計劃:
db.inventory.find({ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
explain() 方法返回如下結果:
{"queryPlanner" : {"plannerVersion" : 1,..."winningPlan" : {"stage" : "COLLSCAN",...}},"executionStats" : {"executionSuccess" : true,"nReturned" : 3,"executionTimeMillis" : 0,"totalKeysExamined" : 0,"totalDocsExamined" : 10,"executionStages" : {"stage" : "COLLSCAN",...},...},...
}
-
queryPlanner.winningPlan.stage 顯示 COLLSCAN 表示集合掃描。
集合掃描表示mongod必須按照文檔掃描整個文檔集合來匹配結果。這通常是昂貴的操作,可能導致查詢速度慢。
-
executionStats.nReturned 顯示3表示查詢匹配到并返回3個文檔。
-
executionStats.totalKeysExamined 顯示0表示這個查詢沒有使用索引。
-
executionStats.totalDocsExamined 顯示10表示MongoDB掃描了10個文檔,從中查詢匹配到3個文檔。
匹配文檔的數量與檢查文檔的數量之間的差異可能意味著,查詢可以使用索引提高的查詢效率。
基于索引查詢
為了查詢支持 quantity 字段,在 quantity 字段上新增索引:
db.inventory.createIndex( { quantity: 1 } )
使用 explain(“executionStats”) 方法,顯示查詢計劃信息:
db.inventory.find({ quantity: { $gte: 100, $lte: 200 } }
).explain("executionStats")
這個 explain() 方法返回如下結果信息:
{"queryPlanner" : {"plannerVersion" : 1,..."winningPlan" : {"stage" : "FETCH","inputStage" : {"stage" : "IXSCAN","keyPattern" : {"quantity" : 1},...}},"rejectedPlans" : [ ]},"executionStats" : {"executionSuccess" : true,"nReturned" : 3,"executionTimeMillis" : 0,"totalKeysExamined" : 3,"totalDocsExamined" : 3,"executionStages" : {...},...},...
}
- queryPlanner.winningPlan.inputStage.stage 顯示
IXSCAN
表示使用了索引。 - executionStats.nReturned 顯示3表示查詢匹配到并返回3個文檔。
- executionStats.totalKeysExamined 顯示3表示MongoDB 掃描了3個索引數據。 檢查的鍵數與返回的文檔數相匹配,這意味著mongod只需檢查索引鍵即可返回結果。mongod不必掃描所有文檔,只有三個匹配的文檔被拉入內存。 這個查詢結果是非常高效的。
- executionStats.totalDocsExamined 顯示3表示MongoDB掃描了3個文檔。
沒有使用索引時查詢將掃描整個集合中的10個文檔返回匹配到的3個文檔。查詢時會將它們拉入內存并掃描每個文檔的整體。這個結果非常耗性能并且潛在的會導致查詢變慢。
當使用索引運行時,查詢掃描3個索引條目然后3個文檔中返回匹配到的3個文檔,這個查詢結果非常高效。
比較索引的性能
查詢時不止一個索引時手動的比較索引性能,可以使用 hint() 方法再結合 explain() 方法。
考慮下面的查詢:
db.inventory.find( {quantity: {$gte: 100, $lte: 300},type: "food"
} )
查詢結果如下:
{ "_id" : 2, "item" : "f2", "type" : "food", "quantity" : 100 }
{ "_id" : 5, "item" : "f3", "type" : "food", "quantity" : 300 }
為了支持這個查詢,添加復合索引。復合索引中字段的順序很重要。
例如,添加如下的2個復合索引。第一個索引先使用 quantity ,再使用 type 字段創建索引。第二個索引先使用 type ,再使用 quantity 字段創建索引。
db.inventory.createIndex( { quantity: 1, type: 1 } )
db.inventory.createIndex( { type: 1, quantity: 1 } )
查詢使用第一個索引來評估性能:
db.inventory.find({ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ quantity: 1, type: 1 }).explain("executionStats")
這個 explain() 方法返回如下輸出信息:
{"queryPlanner" : {..."winningPlan" : {"stage" : "FETCH","inputStage" : {"stage" : "IXSCAN","keyPattern" : {"quantity" : 1,"type" : 1},...}}},"rejectedPlans" : [ ]},"executionStats" : {"executionSuccess" : true,"nReturned" : 2,"executionTimeMillis" : 0,"totalKeysExamined" : 6,"totalDocsExamined" : 2,"executionStages" : {...}},...
}
MongoDB 掃描了6條索引鍵 (executionStats.totalKeysExamined) 并返回了2條匹配到的文檔(executionStats.nReturned)。
查詢使用第二個索引來評估性能:
db.inventory.find({ quantity: { $gte: 100, $lte: 300 }, type: "food" }
).hint({ type: 1, quantity: 1 }).explain("executionStats")
這個 explain() 方法返回如下輸出信息:
{"queryPlanner" : {..."winningPlan" : {"stage" : "FETCH","inputStage" : {"stage" : "IXSCAN","keyPattern" : {"type" : 1,"quantity" : 1},...}},"rejectedPlans" : [ ]},"executionStats" : {"executionSuccess" : true,"nReturned" : 2,"executionTimeMillis" : 0,"totalKeysExamined" : 2,"totalDocsExamined" : 2,"executionStages" : {...}},...
}
MongoDB 掃描了2條索引鍵 (executionStats.totalKeysExamined) 并返回了2條匹配到的文檔(executionStats.nReturned)。
這個查詢例子中,復合索引 {type:1,quantity:1} 比復合索引 {quantity:1,type:1} 更高效。
個人博客: 學習園