详解:Mongodb慢查询(Mongodb slow query log)
来源:互联网 发布:java解压zip文件 编辑:程序博客网 时间:2024/04/30 01:46
转自:http://www.jquerycn.cn/a_7309
本节主要内容:
Mongodb慢查询(Mongodb slow query log)
开始今天的学习吧。
Profiling Levels:
1 resume
0 - the profiler is off, does not collect any data.
1 - collects profiling data for slow operations only. By default slow operations are those slower than 100 milliseconds.
You can modify the threshold for “slow” operations with the slowms runtime option or the setParameter command. See the Specify the Threshold for Slow Operations section for more information.
2 - collects profiling data for all database operations.
2 able
2.1
db.setProfilingLevel(2,500);
PRIMARY> db.setProfilingLevel(1,500);
{ "was" : 0, "slowms" : 500, "ok" : 1 }
PRIMARY>
PRIMARY>
PRIMARY>
PRIMARY> db.setProfilingLevel(1,500);
{ "was" : 1, "slowms" : 500, "ok" : 1 }
PRIMARY>
PRIMARY>
PRIMARY>
为什么需要set2次才能生效?
解释:
0代表关闭,1代表只记录slowlog,2代表记录所有操作,这里设置成了500,即500ms。
2.2 check the status
{ "was" : 2, "slowms" : 500 }
PRIMARY> db.getProfilingLevel();
PRIMARY>
2.3 see the last log info
PRIMARY> db.system.profile.find().sort({$natural:-1})
{ "ts" : ISODate("2013-05-14T08:13:37.098Z"), "op" : "insert", "ns" : "test.tickets", "millis" : 0, "client" : "127.0.0.1", "user" : "" }
{ "ts" : ISODate("2013-05-14T08:13:37.098Z"), "op" : "insert", "ns" : "test.tickets", "millis" : 0, "client" : "127.0.0.1", "user" : "" }
{ "ts" : ISODate("2013-05-14T08:13:37.098Z"), "op" : "insert", "ns" : "test.tickets", "millis" : 0, "client" : "127.0.0.1", "user" : "" }
PRIMARY>
ts:时间戳
op: 操作类型
ns:执行操作的对象集合
millis:操作所花时间,毫秒
client: 执行操作的客户端
user: 执行操作的mongodb连接用户
2.4 可以在mongodb启动之初设置slow生效
直接加在启动命令中:mongod --profile=1 --slowms=15
或在配置文件中写好 用-f强制加载配置文件启动mongodb
slowms=1000
[] 在replicaset中,必须把一个个instance都生效才行。每一个都需要生效一次。
3 see the log
db.system.profile.find().limit(10).sort( { ts : 1 } ).pretty();
4 Disable Profiling
db.setProfilingLevel(0)
5 当profile表过小时,调整表大小为4MB
db.system.profile.drop() -- 删除
db.createCollection( "system.profile", { capped: true, size:4000000 } ) -- 重建
db.setProfilingLevel(1) -- profile生效
6 查看出来的慢mongo命令
6.1 显示最新的5条操作记录
show profile;
6.2 显示结果分析,查询大于5毫秒的slow command。
db.system.profile.find( { millis : { $gt : 5 } } ).pretty();
{
"ts" : ISODate("2013-01-16T18:26:18.041Z"),
"op" : "query", -- 执行类型
"ns" : "ssodatabase.exchangetickets", -- 执行collection名称
"query" : {
"xid" : "X15e1481688254bc9a94701b3aa9e7abc627971358360754783"
}, -- 执行的内容
"ntoreturn" : 1,
"nscanned" : 382793,
"nreturned" : 1,
"responseLength" : 605,
"millis" : 5841, -- 执行的时间
"client" : "10.100.10.161", -- 执行的客户端
"user" : "" -- 执行的mongo用户
}
观察结果中的"query"字段。 没有直接db.test.insert({xxxxxxx.....})这样显示的,需要你自己根据query字段去拼接取值.
7 返回特定时间的慢查询记录
7.1普通的时间短查询
{
ts : {
$gt : new ISODate("2013-05-09T03:00:00Z") ,
$lt : new ISODate("2013-05-17T03:40:00Z")
}
}
).pretty();
run the command, follows:
PRIMARY> db.system.profile.find(
... {
... ts : {
... $gt : new ISODate("2013-05-09T03:00:00Z") ,
... $lt : new ISODate("2013-05-17T03:40:00Z")
... }
... }
...).pretty();
{
"ts" : ISODate("2013-05-14T08:36:58.691Z"),
"op" : "query",
"ns" : "ssodatabase.digitalriverorderdetails",
"query" : {
"invoiceId" : "15539232823"
},
"ntoreturn" : 1,
"nscanned" : 1,
"nreturned" : 1,
"responseLength" : 1213,
"millis" : 663,
"client" : "10.100.10.162",
"user" : "admin"
}
{
"ts" : ISODate("2013-05-14T09:17:58.911Z"),
"op" : "insert",
"ns" : "ssodatabase.tickets",
"millis" : 527,
"client" : "10.100.10.154",
"user" : "admin"
}
{
"ts" : ISODate("2013-05-14T09:20:58.648Z"),
"op" : "insert",
"ns" : "ssodatabase.tickets",
"millis" : 529,
"client" : "10.100.10.153",
"user" : "admin"
}
7.2 带执行时间倒序排序,并且只输出用户信息
{
ts : {
$gt : new ISODate("2013-05-09T03:00:00Z") ,
$lt : new ISODate("2013-05-17T09:40:00Z")
}
},
{ user : 1 } -- 只输出用户信息
).sort( { millis : -1 } ) -- 倒序排序
PRIMARY> db.system.profile.find(
... {
... ts : {
... $gt : new ISODate("2013-05-09T03:00:00Z") ,
... $lt : new ISODate("2013-05-17T09:40:00Z")
... }
... },
... { user : 1 }
...).sort( { millis : -1 } )
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
{ "user" : "admin" }
7.3 带执行时间倒序排序,并且只输出用户信息
{
ts : {
$gt : new ISODate("2013-05-09T03:00:00Z") ,
$lt : new ISODate("2013-05-17T09:40:00Z")
}
}
).sort( { millis : -1 } ) -- 倒序排序
- 详解:Mongodb慢查询(Mongodb slow query log)
- Mongodb慢查询笔记 (Mongodb slow query log)
- slow query log 查询慢查询语句
- MySQL 慢查询日志(Slow Query Log)
- MySQL 慢查询日志(Slow Query Log)
- MySQL 慢查询日志(Slow Query Log)
- mongodb slow query analyzer
- mongodb slow query
- mysql5.6.10关于慢查询的设置slow-query-log
- mysql5.6.10关于慢查询的设置slow-query-log
- MySQL 日志之--慢查询日志(slow-query-log)
- MySQL中的慢查询日志(Slow Query Log)
- mongodb--Profiling慢查询详解
- 使用pt-query-digest分析mysql慢查询日志(mysql slow query log )
- 【MongoDB-query查询条件】
- mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用
- MySQL错误日志(error log),二进制日志(binlog),慢查询日志(slow query log),查询日志(general log),重写日志(redo log)
- mongodb 的慢查询
- VS项目重命名工具,不再为重命名项目,命名空间,文件夹,相互引用关系而烦恼
- 贪心算法 - 删数问题
- Oracle创建定时任务
- 走进shell 2
- 杭电 2553 N皇后问题
- 详解:Mongodb慢查询(Mongodb slow query log)
- 基于devstack自动化部署OpenStack kilo liberty mitaka版本
- 左值引用"&"、右值引用"&&"以及它们之间的转换
- Linux Ulimit 命令
- 近日发现一个有趣的数据分析项目 (解析滴滴算法大赛---数据分析过程)
- 从猪和鸡的故事看Scrum Master职责
- Hadoop大数据系列---Hadoop安装与环境配置
- jvm调优
- Nginx中的正则表达式