Home > Database > Mysql Tutorial > body text

【MongoDB】MongoDB之优化器Profiler

WBOY
Release: 2016-06-07 16:12:07
Original
1197 people have browsed it

在mysql数据库中,慢查询日志经常作为优化数据库的依据, mongodb中依然有类似的功能。Mongodb自带的profiler,可以方便地记录所有耗时的操作,以便于调优; 一、开始profiler功能 开启profier功能有两种: 第一种就是直接在启动参数里面进行设置,就在茄冬m

在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

<pre name="code" class="html">> db.students.find({&#39;comment.aihao&#39;:&#39;reading&#39;}).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" }
Copy after login
Copy after login

 

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

1: ts 命令在何时执行

2: info 命令的详细信息

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

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

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

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

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!