分析mongodb查询性能是确保应用程序高效运行的关键步骤。mongodb提供了许多工具和命令,用于详细分析查询的性能表现。以下是详细的步骤和代码示例,展示如何分析mongodb的查询性能。
1. 使用explain命令
explain 命令可以详细描述查询的执行计划,包括使用的索引、扫描的文档数量等。
示例:基本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"
}
}关键指标说明:
totalkeysexamined: 扫描的索引键数量。totaldocsexamined: 扫描的文档数量。数字越小越好。executiontimemillis: 查询执行时间,单位是毫秒。winningplan: 使用的执行计划,包括使用的索引。
2. 使用 mongodb profiler
mongodb profiler 可以记录数据库操作和慢查询的详细信息。
启用 profiler
db.setprofilinglevel(2);
查询 profiler 数据
db.system.profile.find().sort({ ts: -1 }).limit(1).pretty();
关闭 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": ""
}关键指标说明:
keysexamined,docsexamined: 这些指标与explain命令的输出类似。millis: 查询执行时间,单位是毫秒。nreturned: 返回的文档数量。
3. 使用db.currentop()命令
db.currentop() 可以查看当前正在运行的操作。
示例:查看当前操作
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) } } }
}
]
}关键指标说明:
secs_running: 查询已运行的时间,单位是秒。op: 当前正在执行的操作类型。query: 正在执行的查询。
4. 使用 index usage statistics
查看索引的使用情况,识别未使用的索引。
示例:查看索引使用情况
db.students.aggregate([
{ $indexstats: {} }
]);
示例输出
[
{
"name": "studentid_1",
"key": { "studentid": 1 },
"host": "localhost:27017",
"accesses": {
"ops": 123,
"since": isodate("2021-07-01t00:00:00z")
}
}
]关键指标说明:
name: 索引名称。accesses.ops: 索引的访问次数。accesses.since: 统计开始时间。
总结
通过上述方法,您可以详细分析mongodb查询性能,并识别潜在的瓶颈和优化机会。关键工具和命令包括:
explain命令,详细描述查询执行计划。- mongodb profiler,记录数据库操作和慢查询。
db.currentop()命令,查看当前正在运行的操作。- 索引使用统计,识别未使用的索引。
通过合理使用这些工具和方法,可以有效提高mongodb查询的性能,从而确保数据库应用程序的高效运行。
到此这篇关于mongodb分析查询性能的文章就介绍到这了,更多相关mongodb分析查询性能内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论