Using mongoDB's Profiler analyze the performance of database operations

慢查询在数据库分析中是一个非常重要的参考。
我记得在PostgreSQL Conference 2009 中曾经就有人专门讲解了Hit the mole.(90%的数据库性能问题往往出现在10%的SQL上面.)
在MySQL,PostgreSQL数据库中,用日志来记录慢查询。(比如为数据库设置一下阀值100MS,运行时间超过100MS的SQL将记录到日志中.)
在mongoDB中也比较类似,使用profile 来配置慢查询的阀值.已经是否开启记录慢查询的功能。mongoDB的慢查询被记录到system.profile collection中。
在mongoDB  1.7.x 以下版本中使用getProfilingLevel()查看当前配置的数据库profile.1.7.x以后改为getProfilingStatus().
配置profile可以通过参数文件,或在mongo shell中执行命令来配置.
1. 参数文件
  --profile arg         0=off 1=slow, 2=all  (0表示关闭profile,1表示只记录执行时间超过slowms配置的值的执行内容,2表示记录所有执行内容)
  --slowms arg (=100)   value of slow for profile and console log (如果profile配置为1并且没有配置slowms的话默认是100毫秒)
2. mongo shell
  db.setProfilingLevel( level , slowms ) 
3. 查看profile
> db.getProfilingLevel()
1

配置举例:
# 配置slowms=1,profile=1
> db.setProfilingLevel(1,1) 
{ "was" : 0, "slowms" : 100, "ok" : 1 }

# 注意这里返回的slowms : 100,实际上已经配置为1了,估计是个BUG。在1.7.x里面可以通过以下命令查看:

> db.getProfilingStatus()
{ "was" : 1, "slowms" : 1 }

# 插入测试数据
> for (var i=0;i<10000;i++) {
... db.userinfo.insert({"firstname" : "zhou","lastname" : "digoal" + i,"age" : i})
... }

# 在1.6.5的环境下,没有办法通过db.getProfilingLevel()查看slowms的具体配置,不过通过以下测试可以知道,前面配置的slowms=1已经生效.
> db.userinfo.find({"firstname" : "zhou"}).explain()
{
"cursor" : "BasicCursor",
"nscanned" : 10000,
"nscannedObjects" : 10000,
"n" : 10000,
"millis" : 5,
"indexBounds" : {

}
}
> db.system.profile.find()                          
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }

# 使用以下方式查看效果也是一样的
> db.system.profile.find( function() { return this.info.indexOf('$cmd')<0; } )
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }

# 也可以使用规则表达式过滤出你关心的collection,如下.
> db.system.profile.find({"info" : /userinfo/})  
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }

# 或者按millis排个序
> db.system.profile.find().sort({"millis" : -1})
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }

# 由于system.profile是capped collection,使用$natural  可以按照记录插入的插入顺序查看结果.
> db.system.profile.find().sort({"$natural" : -1})  
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }

# 使用show profile 查看最近5%的超过1ms执行时间的事件
> show profile

5ms Sun Jan 09 2011 09:07:21
query admin.userinfo reslen:202 nscanned:10000  
query: { query: { firstname: "zhou" }, $explain: true }  nreturned:1 5ms

1ms Sun Jan 09 2011 09:02:56
insert admin.userinfo

输出格式:
1. ts : 记录下捕获的时间戳,(SQL执行结束时间,如下)
> Date()
Sun Jan 09 2011 09:42:31 GMT+0800 (CST)
> db.userinfo.ensureIndex({"lastname" : 1,"detail.city" : 1})
> db.system.profile.find().sort({"$natural" : -1}).limit(1)
{ "ts" : "Sun Jan 09 2011 09:42:46 GMT+0800 (CST)", "info" : "insert admin.system.indexes 14374ms", "millis" : 14374 }
# 从时间上看是执行结束时间.
2. millis : 执行时间,注意不包含lock请求时间和network传输时间,只包含了server处理这条请求的时间.
3. info : 执行详细信息,包含(query,update,insert,getmore)
        query : 数据库查询操作,包含以下信息:
                ntoreturn : Number of objects the client requested for return from a query. For example, <code>findOne()</code> sets ntoreturn to 1.<code>limit()</code> sets the appropriate limit. Zero indicates no limit.
                query : Details of the query spec.
                nscanned : Number of objects scanned in executing the operation.
                reslen : Query result length in bytes.
                nreturned : Number of objects returned from query.
         update : A database update operation. <code>save()</code> calls generate either an update or insert operation.
                  fastmod : Indicates a fast modify operation. See Updates. These operations are normally quite fast.
                  fastmodinsert : indicates a fast modify operation that performed an upsert.
                  upsert : Indicates on upsert performed.
                  moved : Indicates the update moved the object on disk (not updated in place). This is slower than an in place update, and normally occurs when an object grows.
          insert : A database insert.
          getmore : For large queries, the database initially returns partial information. getmore indicates a call to retrieve further information.
# 例:
> db.userinfo.find().sort({"age" : -1}).limit(1)     
{ "_id" : ObjectId("4d2910734f7371f2497bd6ad"), "firstname" : "zhou", "lastname" : "digoal999998", "age" : 999998, "detail" : { "city" : "HangZhou", "corp" : "sky-mobi" } }
> db.system.profile.find().sort({"$natural" : -1}).limit(1)
{ "ts" : "Sun Jan 09 2011 09:53:42 GMT+0800 (CST)", "info" : "query admin.userinfo ntoreturn:1 scanAndOrder  reslen:169 nscanned:2009998  \nquery: { query: {}, orderby: { age: -1.0 } }  nreturned:1 3140ms", "millis" : 3140 }
# 性能优化相关
从上面的profile结果看出ntoreturn = 1,nscanned = 2009998,也就是说返回结果是1条,但是扫描了200多W条记录。如果这种查询非常多的话建议在"age"上建立索引。对于update操作同样适用.(当然,还要考虑索引对写入带来的overhead)
如果reslen这个值比较大,表示返回结果非常多,建议调整find()限制输出结果.

调整profileSIZE:
1. 关闭profile
> db.setProfilingLevel(0)
{ "was" : 1, "slowms" : 1, "ok" : 1 }
2. 调整profile size
> db.system.profile.renameCollection("system.oldprofile")
{ "ok" : 1 }
> show collections                                       
system.indexes
system.oldprofile
system.users
userinfo
version
# 调整为100MB
> db.createCollection("system.profile",{"capped" : true, "size" : 102400000})
{ "ok" : 1 }
> db.system.profile.stats()                                                  
{
        "ns" : "admin.system.profile",
        "count" : 0,
        "size" : 0,
        "avgObjSize" : NaN,
        "storageSize" : 102400256,
        "numExtents" : 1,
        "nindexes" : 0,
        "lastExtentSize" : 102400256,
        "paddingFactor" : 1,
        "flags" : 0,
        "totalIndexSize" : 0,
        "indexSizes" : {

        },
        "capped" : 1,
        "max" : 2147483647,
        "ok" : 1
}
3. 重新开启profile
> db.setProfilingLevel(1,1)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
4. oldprofile中的数据无法插入到profile
测试:
> for (var c = db.system.oldprofile.find(); c.hasNext();) {
... b = c.next();
... db.system.profile.insert({"ts" : c.ts,"info" : c.info,"millis" : c.millis}); 
... }
> db.system.profile.count()
0
# 没有插进去
# 非常抱歉,插入到test是OK的.system.profile 不可以手工插入记录
> for (var c = db.system.oldprofile.find(); c.hasNext();) {              
... b = c.next();
... db.test.insert({"ts" : c.ts,"info" : c.info,"millis" : c.millis});          
... }
> db.test.count()
6

5. 删除oldprofile
> db.system.oldprofile.drop()
Sun Jan  9 10:38:57 uncaught exception: drop failed: {
        "ns" : "admin.system.oldprofile",
        "errmsg" : "exception: can't drop system ns",
        "code" : 12502,
        "ok" : 0
}
# system下面的collection不允许删除,所以要先rename一下.
> db.system.oldprofile.renameCollection("oldprofile")
{ "ok" : 1 }
> db.oldprofile.drop()
true

注意事项:
1. system.profile 是capped collection,默认配置比较小,所以如果需要放更多的内容需要调整size和max (具体的话可以参考我以前写过的capped collection topic)
如:
> db.system.profile.stats() 
{
        "ns" : "admin.system.profile",
        "count" : 1,
        "size" : 68,
        "avgObjSize" : 68,
        "storageSize" : 131328,
        "numExtents" : 1,
        "nindexes" : 0,
        "lastExtentSize" : 131328,
        "paddingFactor" : 1,
        "flags" : 0,
        "totalIndexSize" : 0,
        "indexSizes" : {

        },
        "capped" : 1,
        "max" : 2147483647,
        "ok" : 1
}
> db.system.profile.totalSize()
131328

2. 开启profile对性能略有影响.

3. 通过命令行开启profile只对当前数据库生效,并且重启数据库后失效.
# 例如在admin库中使用mongo shell开启了profile,转到test库后是没有开启的.
> use test
switched to db test
> show collections
system.indexes
system.users
userinfo
> db.getProfilingLevel()
0
> db.setProfilingLevel(1,1)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
> db.getProfilingLevel()   
1
# 使用mongo shell开启的profile重启后失效,如下
# 重启mongoDB
[root@db5 ~]# /opt/mongodb/bin/mongo 127.0.0.1:5281/admin
MongoDB shell version: 1.6.5
connecting to: 127.0.0.1:5281/admin
> db.auth("digoal","DIGOAL")
1
> db.getProfilingLevel()
0
# 显然system.profile是还在的.
> show collections
system.indexes
system.profile
system.users
userinfo
version
> db.system.profile.find()
{ "ts" : "Sun Jan 09 2011 09:02:56 GMT+0800 (CST)", "info" : "insert admin.userinfo", "millis" : 1 }
{ "ts" : "Sun Jan 09 2011 09:07:21 GMT+0800 (CST)", "info" : "query admin.userinfo reslen:202 nscanned:10000  \nquery: { query: { firstname: \"zhou\" }, $explain: true }  nreturned:1 5ms", "millis" : 5 }

4. system.profile 不可以手工插入记录

时间: 2024-09-20 22:32:44

Using mongoDB's Profiler analyze the performance of database operations的相关文章

TokuMX, MongoDB and InnoDB versus the insert benchmark with disks

I used the insert benchmark on servers that use disks in my quest to learn more about MongoDB internals. The insert benchmark is interesting for a few reasons. First while inserting a lot of data isn't something I do all of the time it is something f

query performance of the access dababase

 source: http://support.microsoft.com/kb/209126 Information about query performance in an Access database View products that this article applies to. Article ID : 209126 Last Review : November 28, 2007 Revision : 2.4 This article was previously publi

Linux下快速安装MongoDB

    Mongo DB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐.Mongo DB很好的实现了面向对象的思想(OO思想),在Mongo DB中 每一条记录都是一个Document对象.Mongo DB最大的优势在于所有的数据持久操作都无需开发人员手动编写SQL语句,直接调用方法就可以轻松的实现CRUD操作.本文介绍了如何快速安装mongodb供大家参考. 一.安装配置mongodbStep 1: 设置系统环境及确保缺省端口271

MapReduce with MongoDB and Python[ZT]

MapReduce with MongoDB and Python 从 Artificial Intelligence in Motion 作者:Marcel Pinheiro Caraciolo (由于Artificial Intelligence in Motion发布的图在墙外,所以将图换到cnblogs) Hi all,  In this post, I'll present a demonstration of a map-reduce example with MongoDB and

What is the Actual Performance of HANA?

What is the Actual Performance of HANA?   http://www.linkedin.com/pulse/what-actual-hana-performance-shaun-snapp?trk=hp-feed-article-title-publish   Introduction I covered the topic of the actual performance of HANA versus competitive databases in th

Use mongodb 1.8.1&#039;s replicaSet with auth,journal,keyFile feature

MongoDB replicSet 1.8.1 产品部署推荐: 1. 文件系统加载时使用参数noatime 2. no VM PAGEs 3. 推荐使用逻辑卷,文件系统推荐ext4或xfs 4. 3个full nodes 或 2个full nodes+1个arbiter node (最好是奇数个物理服务器,否则仲裁会有问题,例如两台物理机,两个mongod进程,相互网络不通的话,任何一台都无法达到majority,因此都无法成为primary.那就是只读了.因此本例的物理服务器只有2台是不合理的

php操作MongoDB类实例

 本文实例讲述了php操作MongoDB类的方法.分享给大家供大家参考.具体如下: 1. MyMongo.php文件: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

php实现的mongodb操作类实例

 本文实例讲述了php实现的mongodb操作类.分享给大家供大家参考.具体如下:   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

springboot(十一):Spring boot中mongodb的使用

mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置漏洞被攻击,数据被删,引起了人们的注意,感兴趣的可以看看这篇文章:场屠戮MongoDB的盛宴反思:超33000个数据库遭遇入侵勒索,同时也说明了很多公司生产中大量使用mongodb. mongodb简介 MongoDB(来自于英文单词"Humongous",中文含义为"庞大&qu