一、慢日志Profiling功能介绍
1.1 Profiling是什么?
官网介绍:https://www.mongodb.com/docs/v4.2/tutorial/manage-the-database-profiler/index.html
MongoDB Profiling可以收集运行中的数据库执行命令的详细信息。其中包含,CRUD操作以及配置和管理命令等。分析器会将收集的所有数据写入一个system.profile集合中。Profiling默认是关闭的,可以根据业务需求去设置Profiling级别。当然启用后,分析过程会对数据库性能和磁盘IO产生一定影响。不建议业务高峰期开启或长期开启。
1.2 Profiling级别有哪些?
Level | Description |
---|---|
0 | The profiler is off and does not collect any data. This is the default profiler level. |
1 | The profiler collects data for operations that take longer than the value of slowms . |
2 | The profiler collects data for all operations. |
注:上述即为官网对Profiling级别的描述。0是Profiling默认级别,即关闭Profiling功能,不进行任何数据收集;1代表收集操作时长超过慢日志阀值(slowms的值)的所有数据;2代表收集所有操作数据。(2非常消耗性能,不建议设置)
二、Profiling配置方法
2.1 配置文件修改Profiling参数
systemLog : destination : file logAppend : true path : /opt/udb/instance/mongodb-3.6/xxxxxxxx/log/mongodb.log # 日志记录,含慢日志 operationProfiling: slowOpThresholdMs: 100 # 查询超过100ms即认为是慢查询 mode: slowOp
2.2 命令设置Profiling参数
(1)查询Profiling参数
#(1)查看Profiling默认参数设置 udb-4qg23pzn:PRIMARY> db.getProfilingStatus() { "was" : 0, # 级别0 "slowms" : 100, # 查询超过100ms记录为慢查询 "sampleRate" : 1, "operationTime" : Timestamp(1653719217, 2), "$clusterTime" : { "clusterTime" : Timestamp(1653719217, 2), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } #(2)查询Profiling当前级别 udb-4qg23pzn:PRIMARY> db.getProfilingLevel() 0
(2)修改Profiling参数
#(1)设置Profiling级别为1 udb-4qg23pzn:PRIMARY> db.setProfilingLevel(1) { "was" : 0, "slowms" : 100, "sampleRate" : 1, "ok" : 1, "operationTime" : Timestamp(1653719517, 2), "$clusterTime" : { "clusterTime" : Timestamp(1653719517, 2), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } } #(2)设置级别1及慢查询时间0ms udb-4qg23pzn:PRIMARY> db.setProfilingLevel(1,0) { "was" : 1, "slowms" : 100, "sampleRate" : 1, "ok" : 1, "operationTime" : Timestamp(1653719617, 1), "$clusterTime" : { "clusterTime" : Timestamp(1653719617, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } } }
注:为了方便观察现象,我这里把慢查询设置为0ms,即任何一个查询操作都会被记录成慢查询!!!!
(3)效果验证
# 执行一条查询 udb-4qg23pzn:PRIMARY> use admin udb-4qg23pzn:PRIMARY> db.system.users.find() # 查看慢日志记录 udb-4qg23pzn:PRIMARY> db.system.profile.find().pretty().limit(1) { "op" : "query", # 操作类型有insert、query、update、remove、getmore、command "ns" : "admin.system.users", # 操作集合 "command" : { "find" : "system.users", "filter" : { }, "lsid" : { "id" : UUID("781d4c24-98ed-4f56-a8e7-095c295d70e9") }, "$clusterTime" : { "clusterTime" : Timestamp(1653720747, 1), "signature" : { "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="), "keyId" : NumberLong(0) } }, "$db" : "admin" }, "keysExamined" : 0, "docsExamined" : 3, "cursorExhausted" : true, "numYield" : 0, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(2) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1) } }, "Collection" : { "acquireCount" : { "r" : NumberLong(1) } } }, "nreturned" : 3, # 返回的记录数 "responseLength" : 1139, "protocol" : "op_msg", "millis" : 0, # 查询耗时 "planSummary" : "COLLSCAN", "execStats" : { "stage" : "COLLSCAN", "nReturned" : 3, "executionTimeMillisEstimate" : 0, "works" : 5, "advanced" : 3, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 3 }, "ts" : ISODate("2022-05-28T06:52:44.876Z"), # 查询发生时间 "client" : "10.25.135.106", "appName" : "MongoDB Shell", "allUsers" : [ { "user" : "root", "db" : "admin" } ], "user" : "root@admin" }
2.3 常见其它操作命令
#(1)查询最近的1条慢查询记录 db.system.profile.find().limit(1).sort({ ts : -1 }).pretty() #(2)查询特定集合的慢查询记录 db.system.profile.find( { ns : 'admin.system.users' } ).pretty() #(3)查询大于200ms的慢查询记录 db.system.profile.find( { millis : { $gt : 200 } } ).pretty() #(4)查询当前数据库的时间 new Date() ISODate("2022-05-28T07:11:51.042Z") #(5)查询某个时间范围内的慢查询记录 db.system.profile.find({ ts : { $gt : new ISODate("2022-05-28T06:52:00Z") , $lt : new ISODate("2022-05-28T06:53:00Z") }}).pretty() #(6)查询指定时间内,某个user的慢查询信息,并按照耗时排序 db.system.profile.find({ ts : { $gt : new ISODate("2022-05-28T06:52:00Z") , $lt : new ISODate("2022-05-28T06:53:00Z") }}, { user : 0 } ).sort( { millis : -1 } )
注:即使不开启Profiling参数,即Profiling设置为0,MongoDB照样会按照slowms的值大小,记录慢日志到错误日志文件中,即mongodb.log。所有建议可以短期开启Profiling参数用于分析和定位数据库异常,不建议长期开启,否则会严重拖慢数据库性能,并且占用额外磁盘空间。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/128118.html
摘要:分析数据库的慢请求云数据库默认开启了慢请求,系统自动地将请求时间超过的执行情况记录到对应数据库下的集合里。通过命令进入指定数据库。分析慢请求日志,查找引起使用率升高的原因。当一个操作请求如查询更新删除等需要全表扫描时,将非常占用资源。分析MongoDB数据库的慢请求云数据库MongoDB默认开启了慢请求Profiling ,系统自动地将请求时间超过100ms的执行情况记录到对应数据库下的sy...
摘要:实际应用中,查询的返回结果会有条记录。修改保存文件后,重启服务。通常优化至少到级别,最好能优化到指出使用哪个索引在该表找到行记录。如果该值为,说明没有使用索引,可以建立索引提高性能显示实际使用的索引。 ...
摘要:串行最高的隔离级别,完全服从的隔离级别。但是这将严重影响程序的性能。列显示使用了哪个索引。具体指运行时间超过值的,则会被记录到慢查询日志中。当然,如果不是调优需要的话,一般不建议启动该参数,因为开启慢查询日志会或多或少带来一定的性能影响。 一、先谈谈事务 1. ACID特性 1.1 原子性: 事务是最小的执行单位,不允许分割。事务的原子性确保动作要么全部完成,要么完全不起作用;1.2 ...
摘要:不能盲目的做优化,要先找准问题所在,是内存,还是峰值,语句一优化套路二相关命令观察服务器状态或观察连接的工作状态状态说明把查询结果放在磁盘取得数据太多创建临时表索引需要优化把内存临时表复制到磁盘索引需要优化,索引字段被其他查询锁住事务记录慢 不能盲目的做优化,要先找准问题所在,是cpu,内存,io?还是峰值,语句? 一、mysql优化套路 showImg(https://segment...
摘要:命令行方式获取显示当前的用户操作。类似于的可以监控所有慢的以及不慢的查询。以及其他第三方链接性能调优场景现实的首页我们希望现实最近发布的条。自动删除旧文档为了给新文档创建空间,在集合中自动删除老旧的文档,不需要执行额外的脚本和操作。 转载请注明出处 http://www.paraller.com 原文排版地址 http://www.paraller.com/2016/10/22/m...
阅读 1281·2024-02-01 10:43
阅读 368·2024-01-31 14:58
阅读 432·2024-01-31 14:54
阅读 814·2024-01-29 17:11
阅读 2201·2024-01-25 14:55
阅读 1481·2023-06-02 13:36
阅读 2080·2023-05-23 10:26
阅读 469·2023-05-23 10:25