MongoDB分析查詢性能的步驟和代碼示例
分析MongoDB查詢性能是確保應(yīng)用程序高效運(yùn)行的關(guān)鍵步驟。MongoDB提供了許多工具和命令,用于詳細(xì)分析查詢的性能表現(xiàn)。以下是詳細(xì)的步驟和代碼示例,展示如何分析MongoDB的查詢性能。
1. 使用explain命令
explain 命令可以詳細(xì)描述查詢的執(zhí)行計(jì)劃,包括使用的索引、掃描的文檔數(shù)量等。
示例:基本explain使用
db.students.find({ studentId: 12345 }).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"
}
}關(guān)鍵指標(biāo)說明:
totalKeysExamined: 掃描的索引鍵數(shù)量。totalDocsExamined: 掃描的文檔數(shù)量。數(shù)字越小越好。executionTimeMillis: 查詢執(zhí)行時(shí)間,單位是毫秒。winningPlan: 使用的執(zhí)行計(jì)劃,包括使用的索引。
2. 使用 MongoDB Profiler
MongoDB Profiler 可以記錄數(shù)據(jù)庫操作和慢查詢的詳細(xì)信息。
啟用 Profiler
db.setProfilingLevel(2);
查詢 Profiler 數(shù)據(jù)
db.system.profile.find().sort({ ts: -1 }).limit(1).pretty();
關(guān)閉 Profiler
db.setProfilingLevel(0);
示例輸出
{
"op": "query",
"ns": "school.students",
"command": {
"find": "students",
"filter": { "studentId": 12345 },
"projection": {},
"sort": {}
},
"keysExamined": 1,
"docsExamined": 1,
"cursorExhausted": true,
"numYield": 0,
"locks": {
"Global": { "acquireCount": { "r": 1 } },
"Database": { "acquireCount": { "r": 1 } },
"Collection": { "acquireCount": { "r": 1 } }
},
"nreturned": 1,
"responseLength": 466,
"millis": 2,
"ts": ISODate("2021-07-20T14:45:14.467Z"),
"client": "127.0.0.1",
"appName": "MongoDB Shell",
"allUsers": [],
"user": ""
}關(guān)鍵指標(biāo)說明:
keysExamined,docsExamined: 這些指標(biāo)與explain命令的輸出類似。millis: 查詢執(zhí)行時(shí)間,單位是毫秒。nreturned: 返回的文檔數(shù)量。
3. 使用db.currentOp()命令
db.currentOp() 可以查看當(dāng)前正在運(yùn)行的操作。
示例:查看當(dāng)前操作
db.currentOp();
示例輸出
{
"inprog": [
{
"opid": 12345,
"active": true,
"secs_running": 2,
"microsecs_running": NumberLong(2000000),
"op": "query",
"ns": "school.students",
"query": { "studentId": 12345 },
"client": "127.0.0.1:50731",
"desc": "conn123",
"threadId": "0x7fd4e7bfb700",
"connectionId": 123,
"waitingForLock": false,
"lockStats": { "Global": { "acquireCount": { "r": NumberLong(1) } } }
}
]
}關(guān)鍵指標(biāo)說明:
secs_running: 查詢已運(yùn)行的時(shí)間,單位是秒。op: 當(dāng)前正在執(zhí)行的操作類型。query: 正在執(zhí)行的查詢。
4. 使用 Index Usage Statistics
查看索引的使用情況,識(shí)別未使用的索引。
示例:查看索引使用情況
db.students.aggregate([
{ $indexStats: {} }
]);
示例輸出
[
{
"name": "studentId_1",
"key": { "studentId": 1 },
"host": "localhost:27017",
"accesses": {
"ops": 123,
"since": ISODate("2021-07-01T00:00:00Z")
}
}
]關(guān)鍵指標(biāo)說明:
name: 索引名稱。accesses.ops: 索引的訪問次數(shù)。accesses.since: 統(tǒng)計(jì)開始時(shí)間。
總結(jié)
通過上述方法,您可以詳細(xì)分析MongoDB查詢性能,并識(shí)別潛在的瓶頸和優(yōu)化機(jī)會(huì)。關(guān)鍵工具和命令包括:
explain命令,詳細(xì)描述查詢執(zhí)行計(jì)劃。- MongoDB Profiler,記錄數(shù)據(jù)庫操作和慢查詢。
db.currentOp()命令,查看當(dāng)前正在運(yùn)行的操作。- 索引使用統(tǒng)計(jì),識(shí)別未使用的索引。
通過合理使用這些工具和方法,可以有效提高M(jìn)ongoDB查詢的性能,從而確保數(shù)據(jù)庫應(yīng)用程序的高效運(yùn)行。
到此這篇關(guān)于MongoDB分析查詢性能的文章就介紹到這了,更多相關(guān)MongoDB分析查詢性能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MongoDB數(shù)據(jù)庫安裝配置、基本操作實(shí)例詳解
這篇文章主要介紹了MongoDB數(shù)據(jù)庫安裝配置、基本操作,結(jié)合實(shí)例形式詳細(xì)分析了MongoDB數(shù)據(jù)庫安裝配置具體步驟、相關(guān)命令與基本操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2020-01-01
MongoDB中javascript腳本編程簡介和入門實(shí)例
作為一個(gè)數(shù)據(jù)庫,MongoDB有一個(gè)很大的優(yōu)勢(shì)——它使用js管理數(shù)據(jù)庫,所以也能夠使用js腳本進(jìn)行復(fù)雜的管理——這種方法非常靈活2014-04-04
mongodb命令行連接及基礎(chǔ)命令總結(jié)大全
大家可能平時(shí)在開發(fā)過程中都使用客戶端工具來連接和查詢mongodb,但是一般生產(chǎn)當(dāng)中的數(shù)據(jù)庫是不允許本地客戶端連接的,下面這篇文章主要給大家介紹了關(guān)于mongodb命令行連接及基礎(chǔ)命令總結(jié)的相關(guān)資料,需要的朋友可以參考下2024-04-04
Centos 7下Mongodb開機(jī)無法自啟動(dòng)的解決方法
這篇文章主要介紹了Centos 7下Mongodb開機(jī)無法自啟動(dòng)的解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的實(shí)例代碼
這篇文章主要介紹了使用Mongodb實(shí)現(xiàn)打卡簽到系統(tǒng)的示例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
MongoDB 正則表達(dá)式查詢之如何在 MongoDB 中實(shí)現(xiàn)模糊搜索與索引優(yōu)化陷阱
本文詳細(xì)介紹了MongoDB正則表達(dá)式的基礎(chǔ)、索引行為、性能優(yōu)化、安全風(fēng)險(xiǎn)、替代方案以及最佳實(shí)踐,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2026-02-02

