原文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: