-- ===========================
-- mongodb slow query log
-- ===========================

Reference: http://docs.mongodb.org/manual/tutorial/manage-the-database-profiler/

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(1,500);
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
PRIMARY> db.getProfilingStatus();
{ "was" : 2, "slowms" : 500 }
PRIMARY> db.getProfilingLevel();
PRIMARY>

2.3 see the last log info
db.system.profile.find().sort({$natural:-1}) 
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
profile=1
slowms=1000
[] 在replicaset中,必须把一个个instance都生效才行。每一个都需要生效一次。

3 see the log
db.system.profile.find().limit(10).sort( { ts : -1 } ).pretty();
db.system.profile.find().limit(10).sort( { ts : 1 } ).pretty();

4 Disable Profiling
To disable profiling, use the following helper in the mongo shell: set the default value 0;
db.setProfilingLevel(0)

5 当profile表过小时,调整表大小为4MB
db.setProfilingLevel(0)   -- profile失效
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().sort({millis:-1}).limit(10);
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普通的时间短查询
db.system.profile.find(
                       {
                        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 带执行时间倒序排序,并且只输出用户信息
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 } ) -- 倒序排序
 
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 带执行时间倒序排序,并且只输出用户信息
db.system.profile.find(
                       {
                         ts : {
                               $gt : new ISODate("2013-05-09T03:00:00Z")  ,
                               $lt : new ISODate("2013-05-17T09:40:00Z")
                              }
                       }
                      ).sort( { millis : -1 } )  -- 倒序排序

url:http://blog.csdn.net/mchdba/article/details/8950632

Mongodb慢查询笔记 (Mongodb slow query log)的更多相关文章

  1. MySQL:动态开启慢查询日志(Slow Query Log)

    前言 在开发中,高效能的程序 也包括 高效能的查询,所以优化SQL也是程序员必要技能之一.要优化就必须要有慢日志记录才可以知道哪些查询慢,然后反向去修改 慢日志设置方式 写入文件 写入数据库 实践操作 ...

  2. mysql中slow query log慢日志查询分析

    在mysql中slow query log是一个非常重要的功能,我们可以开启mysql的slow query log功能,这样就可以分析每条sql执行的状态与性能从而进行优化了. 一.慢查询日志 配置 ...

  3. mysql慢查询Slow Query Log和未使用索引(Not Using Indexes)查询配置和使用

    mysql的“慢查询”指的是超过了允许的最大查询时间(long_query_time)的sql语句,而“未使用索引”查询顾名思义就是查询语句没有使用到索引的sql语句. 慢查询配置和使用 在msyql ...

  4. MySQL 慢查询日志(Slow Query Log)

    同大多数关系型数据库一样.日志文件是MySQL数据库的重要组成部分.MySQL有几种不同的日志文件.通常包含错误日志文件,二进制日志,通用日志.慢查询日志.等等.这些日志能够帮助我们定位mysqld内 ...

  5. Mysql slow query log

    一.概念部分:  顾名思义,慢查询日志中记录的是执行时间较长的query,也就是我们常说的slow query,通过设--log-slow-queries[=file_name]来打开该功能并设置记录 ...

  6. MySQL专题 2 数据库优化 Slow Query log

    MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录 mysqld 的 ...

  7. MySQL四种类型日志:Error Log、General Query Log、Binary Log、Slow Query Log

    MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录mysqld的一些 ...

  8. mongodb慢查询记录

    在 MySQL中,慢查询日志是经常作为我们优化数据库的依据,那在MongoDB中是否有类似的功能呢?答案是肯定的,那就是Mongo Database Profiler.不仅有,而且还有一些比MySQL ...

  9. mongoDB 高级查询语法

    http://www.cnblogs.com/ITAres/articles/2084794.html本文参考自官方的手册:http://www.mongodb.org/display/DOCS/Ad ...

随机推荐

  1. bzoj 4806: 炮【dp】

    同1801 注意到一行只能放012个炮,我们只需要知道列的状态,不用状压行 所以设f[i][j][k]表示前i行有j列有1个炮,有k列有2个炮的方案数 然后分情况讨论转移就行了 #include< ...

  2. 清北考前刷题day1下午好

    水题(water) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK出了道水题. 这个水题是这样的:有两副牌,每副牌都有n张. 对于第一副牌的每张牌长和宽 ...

  3. codevs1669(dfs)子集和目标值

    1692 子集和的目标值  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 给定n个整数in和目标值T,求某一非空子集 ...

  4. 洛谷 P1582 倒水

    题目描述 一天,CC买了N个容量可以认为是无限大的瓶子,开始时每个瓶子里有1升水.接着~~CC发现瓶子实在太多了,于是他决定保留不超过K个瓶子.每次他选择两个当前含水量相同的瓶子,把一个瓶子的水全部倒 ...

  5. Lightoj 1010 - Knights in Chessboard (胡搞)

    题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1010 题目描述: 有一个n*m的棋盘,根据象棋中马走日字的规则,问此棋盘最多 ...

  6. C#基础 函数部分

    函数:能够独立完成某项功能的模块. 函数四要素:输入.输出.函数体.函数名 函数定义: (static/public) 返回类型 函数名(参数类型 参数名,参数类型 参数名){ 函数体} 函数的调用: ...

  7. asp.net ajax get post 中文乱码解决办法

    前台: var username = $("#UserName").val(); var tel = $("#tel").val(); var yzm = $( ...

  8. P1044 栈

    题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即pop(从栈顶弹出一个元素)和push(将一个元素进栈). 栈的重要性不言自明,任何 ...

  9. P3373 【模板】线段树 2 区间求和 区间乘 区间加

    题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.将某区间每一个数乘上x 3.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含三个整数N.M.P,分别 ...

  10. MySQL与MongoDB的操作对比

    MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoDB则是非关系型数据库,也叫文档型数据库,是一种NoSQL的数据库.它们各有各的优点,关键是看用在什么地方 ...