博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【MongoDB】MongoDB之优化器Profiler
阅读量:4170 次
发布时间:2019-05-26

本文共 1876 字,大约阅读时间需要 6 分钟。

在mysql数据库中,慢查询日志经常作为优化数据库的依据, mongodb中依然有类似的功能。Mongodb自带的profiler,可以方便地记录所有耗时的操作,以便于调优;

一、开始profiler功能

开启profier功能有两种:

第一种就是直接在启动参数里面进行设置,就在茄冬mongodb时候添加-profile=级别

第二种就是在客户端执行“db.setProfilingLevel(级别)”命令

profiler信息保存在system.profile表中,可以通过执行“db.getProfilingLevel()”命令来获取当前profiler级别来:

在上图可以看到,level总共有三个参数,0是关闭,1是慢查询,2是所有的。如果设置为2表示所有的语句都会记录到log中。慢查询的默认时间是100ms,当然也可以通过参数slowsms进行设置。

二、查询profiler记录

mysql慢查询日志是存储在磁盘上,而mongodb profiler记录直接存在系统的db中。记录到system.profile中。profile就是前面讲过的capped collection集合。所以只要查询这个collection的记录就可以获取profiler记录的日志,可以使用db.system.profile.find()的命令直接查找执行时间大于某一限度的(如5ms)的profiler日志;

开启设置为100ms

> db.students.find({'comment.aihao':'reading'}).limit(1){ "_id" : ObjectId("5485c80bdf41c6670aa8d51c"), "name" : "albert", "age" : 12, "comment" : { "aihao" : [ "basket", "reading" ], "relatives" : [ "parent", "brother" ] } }> db.system.profile.find().sort({$natural:-1}).limit(1){ "op" : "query", "ns" : "test.system.indexes", "query" : { "expireAfterSeconds" : { "$exists" : true } }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 7, "nscannedObjects" : 7, "keyUpdates" : 0, "numYield" : 0, "lockStats" : { "timeLockedMicros" : { "r" : NumberLong(159), "w" : NumberLong(0) }, "timeAcquiringMicros" : { "r" : NumberLong(4), "w" : NumberLong(6) } }, "nreturned" : 0, "responseLength" : 20, "millis" : 0, "execStats" : { "type" : "COLLSCAN", "works" : 9, "yields" : 0, "unyields" : 0, "invalidates" : 0, "advanced" : 0, "needTime" : 8, "needFetch" : 0, "isEOF" : 1, "docsTested" : 7, "children" : [ ] }, "ts" : ISODate("2014-12-08T15:54:47.377Z"), "client" : "0.0.0.0", "allUsers" : [ { "user" : "__system", "db" : "local" } ], "user" : "__system@local" }

 

通过执行上面语句,可以看出在system.profile中记录了详细的查询信息。主要字段说明:

1: ts 命令在何时执行

2: info 命令的详细信息

3:reslen: 返回结果集的大小

4: nscanned:本次查询扫描的记录数

5: nreturned: 本次查询实际返回的结果集

6: mills:该命令的执行耗时(单位:毫秒)

可以利用命令查询特定条件的值:例如:查询执行时间大于4ms的查询语句:

你可能感兴趣的文章
如何导入pycharm无法导入的包
查看>>
2018.4.37
查看>>
2018.4.38
查看>>
2018.4.39
查看>>
2018.4.40
查看>>
2018.5.27
查看>>
2018.5.51
查看>>
2018.5.52
查看>>
《python基础教程》答案(第四章)
查看>>
2018.5.53
查看>>
2018.5.54
查看>>
2018.5.55
查看>>
2018.5.58
查看>>
2018.12.5
查看>>
2018.12.6
查看>>
人智导(四):约束满足问题
查看>>
2018.12.7
查看>>
2018.12.8
查看>>
2018.12.9
查看>>
2018.12.29
查看>>