当前位置: 首页 > 科技观察

Redis如何分析慢查询操作?

时间:2023-03-16 17:55:01 科技观察

什么是慢查询?和mysql的慢SQL日志分析一样,redis也有类似的功能,可以帮助定位一些慢查询操作。Redisslowlog是Redis用来记录查询执行时间的日志系统。查询执行时间是指执行一条查询命令所花费的时间,不包括客户端响应(talking)和发送回复等IO操作。另外,慢日志存储在内存中,读写速度非常快,可以放心使用,不用担心开启慢日志会破坏Redis的速度。慢查询参数首先我们重点关注慢日志分析对应的两个参数:1.slowlog-log-slower-than:预设的阈值,即记录多长时间记录。默认为10000微秒,即10毫秒。2.slowlog-max-len:记录慢查询的个数,默认为128个,当超过设定个数时,最早进入队列的将被移除。网上建议加大这个值,比如:1000,这样可以减少出队频率。127.0.0.1:6379>configgetslowlog-log-slower-than1)"slowlog-log-slower-than"2)"10000"127.0.0.1:6379>configgetslowlog-max-len1)"slowlog-max-len"2)"128"这两个参数可以通过configset来调整,也可以在配置文件中设置。##################################慢日志####################################TheRedisSlowLogisasystemtologqueriesthatexceededaspecified#executiontime.TheexecutiontimedoesnotincludetheI/Ooperations#liketalkingwiththeclient,sendingthereplyandsoforth,#butjustthetimeneededtoactuallyexecutethecommand(thisistheonly#stageofcommandexecutionwherethethreadisblockedandcannotserve#otherrequestsinthemeantime).##Youcanconfiguretheslowlogwithtwoparameters:onetellsRedis#whatistheexecutiontime,inmicroseconds,toexceedinorderforthe#commandtogetlogged,andtheotherparameteristhelengthofthe#slowlog.Whenanewcommandisloggedtheoldestoneisremovedfromthe#queueofloggedcommands.#Thefollowingtimeisexpressedinmicroseconds,so1000000isequivalent#toonesecond.Notethatanegativenumberdisablestheslowlog,while#avalueofzeroforcestheloggingofeverycommand.slowlog-log-slower-than10000#Thereisnolimittothislength.Justbeawarethatitwillconsumememory.#YoucanreclaimmemoryusedbytheslowlogwithSLOWLOGRESET.slowlog-max-len128慢查询命令语法:slowlogsubcommand[argument]例如查询慢查询,获取慢查询记录数,重置慢查询日志等操作:192.168.10.38:9001>slowlogget(emptylistorset)192.168.10.38:9001>slowlogget10(emptylistorset)192.168.10.38:9001>slowloglen(整数)0192.168.10.38:9001>slowlogresetOK