MongoDB 慢查询优化

一、MongoDB 慢查询(Profiling)级别说明

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

1、开启和设置慢查询
① 修改配置文件(mongo.conf)开启Profiling

#开启慢查询,200毫秒的记录
profile = 1
slowms = 200

② mongoshell来进行临时性打开启Profiling

##登录数据库
mongo --host 127.0.0.1:27017 --username 户名 --password 密码 --authenticationDatabase admin

##跳转到要开启慢查询监控的数据库
use crm

##设置Profiling
PRIMARY> db.getProfilingStatus()          //查看状态:级别和时间
{ "was" : 1, "slowms" : 100 }

PRIMARY> db.getProfilingLevel()          //查看级别
1

PRIMARY> db.setProfilingLevel(2)         //设置级别
{ "was" : 1, "slowms" : 100, "ok" : 1 }

PRIMARY> db.setProfilingLevel(1,200)     //设置级别和时间
{ "was" : 2, "slowms" : 100, "ok" : 1 }

2、修改慢查询的大小

##关闭Profiling
PRIMARY> db.setProfilingLevel(0)
{ "was" : 0, "slowms" : 200, "ok" : 1 }

##删除system.profile集合
PRIMARY> db.system.profile.drop()
true

##创建一个新的system.profile集合
PRIMARY> db.createCollection( "system.profile", { capped: true, size:4000000 } )
{ "ok" : 1 }

##重新开启Profiling
PRIMARY> db.setProfilingLevel(1)
{ "was" : 0, "slowms" : 200, "ok" : 1 }

备注:要改变Secondarysystem.profile的大小,你必须停止Secondary,运行它作为一个独立的,然后再执行上述步骤。完成后,重新启动加入副本集。

二、慢查询(system.profile)说明

PRIMARY> db.system.profile.find().pretty()
{
	"op" : "query",               #操作类型,有insert、query、update、remove、getmore、command
	"ns" : "crm.customer",        #操作的集合
	"command" : {                 #操作的集合
		"find" : "customer",
		"filter" : {
			"stores" : 909,
			"companyId" : 999
		},
		"sort" : {
			"_id" : -1
		},
		"limit" : 100000,
	},
	"cursorid" : NumberLong("8994141675609999540"),
	"keysExamined" : 24221,
	"docsExamined" : 24221,
	"hasSortStage" : true,
	"nreturned" : 101,      #返回的记录数。例如,profile命令将返回一个文档(一个结果文件),因此ntoreturn值将为1。limit(5)命令将返回五个文件,因此ntoreturn值是5。如果ntoreturn值为0,则该命令没有指定一些文件返回,因为会是这样一个简单的find()命令没有指定的限制。
    "ntoskip" : 0,     #skip()方法指定的跳跃数
    "nscanned" : 304,  #扫描数量
    "keyUpdates" : 0,  #索引更新的数量,改变一个索引键带有一个小的性能开销,因为数据库必须删除旧的key,并插入一个新的key到B-树索引
    "numYield" : 0,    #该查询为其他查询让出锁的次数
    "lockStats" : {    #锁信息,R:全局读锁;W:全局写锁;r:特定数据库的读锁;w:特定数据库的写锁
        "timeLockedMicros" : {     #锁
            "r" : NumberLong(19467),
            "w" : NumberLong(0)
        },
        "timeAcquiringMicros" : {  #锁等待
            "r" : NumberLong(7),
            "w" : NumberLong(9)
        }
    },
	"ts" : ISODate("2020-11-18T02:40:53.138Z"),    #语句执行的时间
	"client" : "192.168.0.140",                    #链接ip或则主机
	"allUsers" : [],
	"user" : ""                                    #用户
}

三、日常使用的查询

##返回最近的10条记录
db.system.profile.find().limit(10).sort({ ts : -1 }).pretty()

##返回所有的操作,除command类型的
db.system.profile.find( { op: { $ne : 'command' } } ).pretty()

##返回特定集合
db.system.profile.find( { ns : 'mydb.test' } ).pretty()

##返回大于5毫秒慢的操作
db.system.profile.find( { millis : { $gt : 5 } } ).pretty()

##从一个特定的时间范围内返回信息
db.system.profile.find({
  ts : {
    $gt : new ISODate("2020-11-17T09:00:00Z") ,
    $lt : new ISODate("2020-11-17T09:40:00Z")
  }}).pretty()

#特定时间,限制用户,按照消耗时间排序
db.system.profile.find({
  ts : {
    $gt : new ISODate("2020-11-17T09:00:00Z") ,
    $lt : new ISODate("2020-11-17T09:40:00Z")
  }},
  { user : 0 }
  ).sort( { millis : -1 } )

猜你喜欢

转载自blog.csdn.net/m0_37886429/article/details/109774472