利用mongo profile 分析mongo慢查询


原文html

  http://itopm.com/archives/2012/07/21/mongodb-profile.shtmlmysql

Mongodb Profiling 是Mongodb提供的相似Mysql的 慢查询的功能,能够记录执行时间超过多少的语句,Mongodb Profiling记录是记录在syste的profile里面。能够经过db.system.profile.find()来进行查询。sql

Mongodb Profiling帮助mongodb

>db.commandHelp("profile") //see how to run from dirversshell

profile 命令只针对某个数据库,不是全局的,mysql开启慢查询以后是针对全部数据库的。好比要对itopm数据库启用profile,须要use itopm数据库

启用Mongodb的profileide

方法1:ui

To enable profiling, from the mongo shell invoke:

> db.setProfilingLevel(2);
{"was" : 0 , "slowms" : 100, "ok" : 1} // "was" is the old setting
> db.getProfilingLevel()
2
Profiling levels are:

0 - off
1 - write slow operations (by default, >100ms is considered slow) to the system.profile collection
2 - write all operations to the system.profile collectionhtm

In addition to the default levels you can also specify a slowms option:ci

> db.setProfilingLevel(1,20) // log slow operations, slow threshold=20ms

查看目前的profiling的状态

> db.getProfilingStatus() // new shell helper method as of v1.7+
{ "was" : 1, "slowms" : 20 }

Note: the profiling level controls which operations get written to the system.profile collection. However, even if the profiler is off, queries slower than the "slowms" level will get written to the logs.

启动Mongodb profile 方法2:

在启动mongodb的时候加入相关参数

Through the command-line/config-file

You can also enable profiling on the command line; for example:

$ mongod --profile=1 --slowms=15

还有须要注意的是,若是你的mongodb采用了sharding,须要在每一个sharding上面开启profile

查询profile的数据

db.system.profile.find()

固然啦,你能够经过加入条件来过滤相关数据来为你所用了,好比

查询运行时间大于500毫秒的语句

db.system.profile.find( { millis : { $gt : 500 } } )


查询最近的语句

db.system.profile.find().sort({$natural:-1})


查询一段时间内的语句

> db.system.profile.find(
...{ts:{$gt:new ISODate("2012-07-19T03:00:00Z"),
...     $lt:new ISODate("2012-07-19T03:40:00Z")}
...})

也能够用show profile 来查看相关的信息

PRIMARY> show profilecommand itopm.$cmd 155ms Fri Jul 20 2012 13:58:15command:{        "count" : "itodoc",        "query" : {                "pt" : {                        "$gte" : NumberLong(1342713600)                },                "tag" : {                        "$in" : [                                "r887"                        ]                }        }} ntoreturn:1 responseLength:48 client:192.168.100.4 user: command itopm.$cmd 195ms Fri Jul 20 2012 13:58:15command:{        "count" : "itodoc",        "query" : {                "pt" : {                        "$gte" : NumberLong(1342713600)                },                "tag" : {                        "$in" : [                                "r51"                        ]                }        }} ntoreturn:1 responseLength:48 client:192.168.100.4 user: command itopm.$cmd 465ms Fri Jul 20 2012 13:58:14command:{        "count" : "itodoc",        "query" : {                "pt" : {                        "$gte" : NumberLong(1342713600)                },                "tag" : {                        "$in" : [                                "r884"                        ]                }        }} ntoreturn:1 responseLength:48 client:192.168.100.4 user: command itopm.$cmd 490ms Fri Jul 20 2012 13:58:14command:{        "count" : "itodoc",        "query" : {                "pt" : {                        "$gte" : NumberLong(1342713600)                },                "tag" : {                        "$in" : [                                "r884"                        ]                }        }} ntoreturn:1 responseLength:48 client:192.168.100.4 user: command itopm.$cmd 288ms Fri Jul 20 2012 13:58:14command:{        "count" : "itodoc",        "query" : {                "pt" : {                        "$gte" : NumberLong(1342713600)                },                "tag" : {                        "$in" : [                                "r876"                        ]                }        }} ntoreturn:1 responseLength:48 client:192.168.100.4 user:

相关文章
相关标签/搜索