MongoDB整理笔记の性能监控

    方法一:Mongostat

    此工具可以快速查看某组运行中的mongodb实例的统计信息,用法如下:

[root@localhost bin]# ./mongostat
insert query update delete ...... locked % idx miss % qr|qw ar|aw conn time
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:15
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:16
*0 *0 *0 *0 ...... 0 0 0|0 1|0 4 01:19:17

    它每秒钟刷新一次状态值,提供良好的可读性,通过这些参数可以观察到一个整体的性能情况。

    字段说明:

    insert: 每秒插入量
    query: 每秒查询量
    update: 每秒更新量
    delete: 每秒删除量
    locked: 锁定量
    qr | qw: 客户端查询排队长度(读|写)
    ar | aw: 活跃客户端量(读|写)
    conn: 连接数
    time: 当前时间

    方法二:db.serverStatus

    这个命令最常用

> db.serverStatus()
{
"host" : "localhost.localdomain",
"version" : "1.8.1", --服务器版本
"process" : "mongod",
"uptime" : 3184, --启动时间(秒)
"uptimeEstimate" : 3174,
"localTime" : ISODate("2012-05-28T11:20:22.819Z"),
"globalLock" : {
"totalTime" : 3183918151,
"lockTime" : 10979,
"ratio" : 0.000003448267034299149,
"currentQueue" : {
"total" : 0, --当前全部队列量
"readers" : 0, --读请求队列量
"writers" : 0 --写请求队列量
},
"activeClients" : {
"total" : 0, --当前全部客户端连接量
"readers" : 0, --客户端读请求量
"writers" : 0 --客户端写请求量
}
},
"mem" : {
"bits" : 32, --32 位系统
"resident" : 20, --占用物量内存量
"virtual" : 126, --虚拟内存量
"supported" : true, --是否支持扩展内存
"mapped" : 32
},
"connections" : {
"current" : 1, --当前活动连接量
"available" : 818 --剩余空闲连接量
},
……
"indexCounters" : {
"btree" : {
"accesses" : 0, --索引被访问量
"hits" : 0, --索引命中量
"misses" : 0, --索引偏差量
"resets" : 0,
"missRatio" : 0 --索引偏差率(未命中率)
}
},
……
"network" : {
"bytesIn" : 1953, --发给此服务器的数据量(单位:byte)
"bytesOut" : 25744, --此服务器发出的数据量(单位:byte)
"numRequests" : 30 --发给此服务器的请求量
},
"opcounters" : {
"insert" : 0, --插入操作的量
"query" : 1, --查询操作的量
"update" : 0, --更新操作的量
"delete" : 0, --删除操作的量
"getmore" : 0,
"command" : 31 --其它操作的量
},
……
"ok" : 1
}
>
View Code

    方法三:db.stats 查看数据库状态信息   

> db.stats()
{
"db" : "test",
"collections" : 7, --collection 数量
"objects" : 28, --对象数量
"avgObjSize" : 50.57142857142857, --对象平均大小
"dataSize" : 1416, --数据大小
"storageSize" : 31744, --数据大小(含预分配空间)
"numExtents" : 7, --事件数量
"indexes" : 7, --索引数量
"indexSize" : 57344, --索引大小
"fileSize" : 50331648, --文件大小
"ok" : 1 --本次取stats 是否正常
}
>
View Code

  方法四:profile

    mongodb可以通过profile来监控数据,进行优化。查看当前是否开启profile功能用命令db.getProfilingLevel()  返回level等级,值为0|1|2,分别代表意思:0代表关闭,1代表记录慢命令,2代表全部开始profile功能为db.setProfilingLevel(level);  #level等级,值同上level为1的时候,慢命令默认值为100ms,更改为db.setProfilingLevel(level,slowms)如db.setProfilingLevel(1,50)这样就更改为50毫秒通过db.system.profile.find() 查看当前的监控日志。 

> db.system.profile.find({millis:{$gt:500}})
{ "ts" : ISODate("2011-07-23T02:50:13.941Z"), "info" : "query order.order reslen:11022 nscanned:672230  
query: { status: 1.0 } nreturned:101 bytes:11006 640ms", "millis" : 640 }
{ "ts" : ISODate("2011-07-23T02:51:00.096Z"), "info" : "query order.order reslen:11146 nscanned:672302  
query: { status: 1.0, user.uid: { $gt: 1663199.0 } }  nreturned:101 bytes:11130 647ms", "millis" : 647 }

    这里值的含义是

    ts:命令执行时间

    info:命令的内容

    query:代表查询

    order.order: 代表查询的库与集合

    reslen:返回的结果集大小,byte数

    nscanned:扫描记录数量

    nquery:后面是查询条件

    nreturned:返回记录数及用时

    millis:所花时间

    如果发现时间比较长,那么就需要作优化。

    比如nscanned数很大,或者接近记录总数,那么可能没有用到索引查询。

    reslen很大,有可能返回没必要的字段。

    nreturned很大,那么有可能查询的时候没有加限制。

原文地址:https://www.cnblogs.com/tomcatx/p/4245633.html