MongoDB使用explain命令的步驟和代碼示例
使用 MongoDB 的 explain 命令可以深入了解查詢的執(zhí)行計劃,從而幫助你優(yōu)化和調試查詢性能。下面是詳細的步驟和代碼示例,展示如何使用 explain 命令深入分析查詢。
1. 基本使用
最基本的 explain 命令可以提供查詢的執(zhí)行計劃。
示例:基本explain使用
db.students.find({ studentId: 12345 }).explain();2. explain() 的模式
explain 命令有三種模式:
- queryPlanner: 返回查詢的邏輯計劃和索引使用情況。
- executionStats: 返回查詢的邏輯計劃、索引使用情況以及執(zhí)行統(tǒng)計信息。
- allPlansExecution: 返回查詢的邏輯計劃、所有備選計劃以及每個計劃的執(zhí)行統(tǒng)計信息。
示例:不同模式的explain
// queryPlanner 模式
db.students.find({ studentId: 12345 }).explain("queryPlanner");
// executionStats 模式
db.students.find({ studentId: 12345 }).explain("executionStats");
// allPlansExecution 模式
db.students.find({ studentId: 12345 }).explain("allPlansExecution");3. explain() 輸出解讀
示例輸出(executionStats 模式)
{
"queryPlanner": {
"plannerVersion": 1,
"namespace": "school.students",
"indexFilterSet": false,
"parsedQuery": { "studentId": { "$eq": 12345 } },
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": { "studentId": 1 },
"indexName": "studentId_1",
"direction": "forward",
"indexBounds": { "studentId": [ "[12345, 12345]" ] }
}
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 1,
"executionTimeMillis": 2,
"totalKeysExamined": 1,
"totalDocsExamined": 1,
"executionStages": {
"stage": "FETCH",
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 2,
"advanced": 1,
"needTime": 0,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"invalidates": 0,
"docsExamined": 1,
"alreadyHasObj": 0,
"inputStage": {
"stage": "IXSCAN",
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 2,
"advanced": 1,
"needTime": 0,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"invalidates": 0,
"keyPattern": { "studentId": 1 },
"indexName": "studentId_1",
"isMultiKey": false,
"multiKeyPaths": { "studentId": [] },
"indexBounds": { "studentId": [ "[12345, 12345]" ] },
"keysExamined": 1,
"seeks": 1,
"dupsTested": 0,
"dupsDropped": 0
}
}
},
"serverInfo": {
"host": "localhost",
"port": 27017,
"version": "4.4.6",
"gitVersion": "22c124145fa3bfdaeafb3f6d1b5f3d4e8391fe86"
}
}關鍵字段解讀
- queryPlanner:
namespace: 查詢的命名空間,即數(shù)據庫和集合的名稱。parsedQuery: MongoDB 解析后的查詢條件。winningPlan: 查詢的執(zhí)行計劃,包括使用的索引和操作步驟。rejectedPlans: 被拒絕的其他執(zhí)行計劃。
- executionStats:
executionSuccess: 查詢是否成功。nReturned: 查詢返回的文檔數(shù)量。executionTimeMillis: 查詢的執(zhí)行時間(毫秒)。totalKeysExamined: 掃描的索引鍵數(shù)量。totalDocsExamined: 掃描的文檔數(shù)量。數(shù)字越小越好。executionStages: 查詢的執(zhí)行步驟及其統(tǒng)計信息。
- executionStages:
stage: 執(zhí)行階段名稱,例如FETCH,IXSCAN。nReturned: 該階段返回的文檔數(shù)量。executionTimeMillisEstimate: 該階段的估計執(zhí)行時間。keysExamined: 該階段掃描的索引鍵數(shù)量。docsExamined: 該階段掃描的文檔數(shù)量。inputStage: 下一個輸入階段的信息。
4. 示例:復合索引和多條件查詢
假設有一個復合索引 { lastName: 1, firstName: 1 },并執(zhí)行多條件查詢。
創(chuàng)建復合索引
db.students.createIndex({ lastName: 1, firstName: 1 });查詢及執(zhí)行計劃分析
db.students.find({ lastName: "Smith", firstName: "John" }).explain("executionStats");
示例輸出及解讀
{
"queryPlanner": {
"plannerVersion": 1,
"namespace": "school.students",
"indexFilterSet": false,
"parsedQuery": { "lastName": { "$eq": "Smith" }, "firstName": { "$eq": "John" } },
"winningPlan": {
"stage": "FETCH",
"inputStage": {
"stage": "IXSCAN",
"keyPattern": { "lastName": 1, "firstName": 1 },
"indexName": "lastName_1_firstName_1",
"direction": "forward",
"indexBounds": {
"lastName": [ "[\"Smith\", \"Smith\"]" ],
"firstName": [ "[\"John\", \"John\"]" ]
}
}
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 1,
"executionTimeMillis": 1,
"totalKeysExamined": 1,
"totalDocsExamined": 1,
"executionStages": {
"stage": "FETCH",
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 2,
"advanced": 1,
"needTime": 0,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"invalidates": 0,
"docsExamined": 1,
"alreadyHasObj": 0,
"inputStage": {
"stage": "IXSCAN",
"nReturned": 1,
"executionTimeMillisEstimate": 0,
"works": 2,
"advanced": 1,
"needTime": 0,
"needYield": 0,
"saveState": 0,
"restoreState": 0,
"isEOF": 1,
"invalidates": 0,
"keyPattern": { "lastName": 1, "firstName": 1 },
"indexName": "lastName_1_firstName_1",
"isMultiKey": false,
"multiKeyPaths": { "lastName": [], "firstName": [] },
"indexBounds": {
"lastName": [ "[\"Smith\", \"Smith\"]" ],
"firstName": [ "[\"John\", \"John\"]" ]
},
"keysExamined": 1,
"seeks": 1,
"dupsTested": 0,
"dupsDropped": 0
}
}
}
}通過以上示例和解讀,您可以深入了解 MongoDB 查詢的執(zhí)行計劃,并根據執(zhí)行計劃中的信息優(yōu)化查詢和索引設計。合理使用 explain 命令,可以顯著提升查詢性能,確保數(shù)據庫的高效運行。
到此這篇關于MongoDB使用explain命令的文章就介紹到這了,更多相關MongoDB使用explain命令內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MongoDB數(shù)據庫forEach循環(huán)遍歷用法
這篇文章主要介紹了MongoDB數(shù)據庫forEach循環(huán)遍歷用法,需要的朋友可以參考下2014-07-07
MongoDB在Windows系統(tǒng)和Linux系統(tǒng)中實現(xiàn)自動定時備份的操作步驟
要在Windows系統(tǒng)中實現(xiàn)自動定時備份MongoDB數(shù)據庫,可以使用Windows任務計劃程序和MongoDB自帶的mongodump工具,這篇文章主要介紹了MongoDB在Windows系統(tǒng)和Linux系統(tǒng)中實現(xiàn)自動定時備份的操作步驟,需要的朋友可以參考下2023-12-12

