重庆思庄Oracle、Redhat认证学习论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 444|回复: 0
打印 上一主题 下一主题

mongodb慢查询设置

[复制链接]
跳转到指定楼层
楼主
发表于 2023-10-6 17:28:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1.显示当前数据库设置
  登录需要开启慢查询的数据库
  use ordb

   查看慢查询状态
  > db.getProfilingStatus()
  { "was" : 1, "slowms" : 10, "sampleRate" : 1 }
  >  

2.设置慢查询级别
   Profiling 级别

  0:关闭,不收集任何数据。
  1:收集慢查询数据,默认是100毫秒。
  2:收集所有数据

  进入数据库
    use ordb

  db.setProfilingLevel(1)

    如果不需要收集所有慢日志,只需要收集超过 200ms 的慢日志可以使用如下命令
  db.setProfilingLevel(1,200)

   注意:

  以上操作要是在 test 集合下面的话,只对该集合里的操作有效,要是需要对整个实例有效,则需要在所有的集合下设置或在开启的时候开启参数。
  每次设置之后返回给你的结果是修改之前的状态(包括级别、时间参数)
> db.getProfilingLevel()
0
> db.setProfilingLevel(1)
{ "was" : 0, "slowms" : 200, "sampleRate" : 1, "ok" : 1 }


3.全局设置
  在 mongoDB 启动的时候加入如下参数
  mongod --profile=1  --slowms=200

   或在配置文件里添加 2 行:

  operationProfiling:
     slowOpThresholdMs: 200
     mode: slowOp

4.关闭 Profiling
  使用如下命令可以关闭慢日志
  db.setProfilingLevel(0)


5.慢查询(system.profile)分析:

   a.显示所有
         db.system.profile.find()

   b.显示最近的
         db.system.profile.find({"op":"query"}).sort({$natural:-1});

   c.查询大于指定时间如500毫秒:

   db.system.profile.find({"millis":{$gte:500}})

   d.查询指定时间段慢查询:

   db.system.profile.find(
                       {
                        ts : {
                              $gt : new ISODate("2023-10-06T07:00:00.000Z"),
                              $lt : new ISODate("2023-10-06T08:00:00.000Z")
                             }
                       }
                      )


   e.只显示指定的字段

> db.system.profile.find({}, {ns:1,op:1,millis:1})
{ "op" : "query", "ns" : "ordb.employees", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "command", "ns" : "ordb", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.employees", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }
{ "op" : "command", "ns" : "ordb.imooc_2", "millis" : 0 }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0 }


   f.按执行时间倒序显示
> db.system.profile.find({}, {ns:1,op:1,millis:1,query:1,ts:1}).sort({ts:-1});
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:29:13.773Z") }
{ "op" : "command", "ns" : "ordb.imooc_2", "millis" : 0, "ts" : ISODate("2023-10-06T07:28:37.004Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:17:05.185Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:01:27.864Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:58:14.475Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:57:37.763Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:54:14.803Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:52:11.602Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:49:54.021Z") }
{ "op" : "query", "ns" : "ordb.employees", "millis" : 0, "ts" : ISODate("2023-10-06T06:49:46.893Z") }
{ "op" : "command", "ns" : "ordb", "millis" : 0, "ts" : ISODate("2023-10-06T06:49:43.707Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T04:55:15.841Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T04:54:46.468Z") }
{ "op" : "query", "ns" : "ordb.employees", "millis" : 0, "ts" : ISODate("2023-10-06T04:54:20.117Z") }
>
  g.只显示TOP5
>
> db.system.profile.find({}, {ns:1,op:1,millis:1,query:1,ts:1}).sort({ts:-1}).limit(5);
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:29:13.773Z") }
{ "op" : "command", "ns" : "ordb.imooc_2", "millis" : 0, "ts" : ISODate("2023-10-06T07:28:37.004Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:17:05.185Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T07:01:27.864Z") }
{ "op" : "query", "ns" : "ordb.system.profile", "millis" : 0, "ts" : ISODate("2023-10-06T06:58:14.475Z") }
>
>

6.清空慢查询日志
  当我们分析完成以后通过
db.system.profile.drop()
  来清空这些捕捉到的数据,清空之前需要先关闭profiling。

  > db.system.profile.drop()
2023-10-06T17:16:23.075+0800 E  QUERY    [js] uncaught exception: Error: drop failed: {
        "ok" : 0,
        "errmsg" : "turn off profiling before dropping system.profile collection",
        "code" : 20,
        "codeName" : "IllegalOperation"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.drop@src/mongo/shell/collection.js:708:15
@(shell):1:1


--关闭记录  
  db.setProfilingLevel(0)
--确认
> db.getProfilingLevel()
  0
  --重新去删除
> db.system.profile.drop()
true
> db.system.profile.find();



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|重庆思庄Oracle、Redhat认证学习论坛 ( 渝ICP备12004239号-4 )

GMT+8, 2024-4-27 19:15 , Processed in 0.096702 second(s), 19 queries .

重庆思庄学习中心论坛-重庆思庄科技有限公司论坛

© 2001-2020

快速回复 返回顶部 返回列表