这篇文章将为大家详细讲解有关Redis中慢查询操作的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

什么是慢查询

慢查询的作用:通过慢查询分析,找到有问题的命令进行优化。

和mysql的慢SQL日志分析一样,redis也有类似的功能,来帮助定位一些慢查询操作。

Redis slowlog是Redis用来记录查询执行时间的日志系统。

查询执行时间指的是不包括像客户端响应(talking)、发送回复等IO操作,而单单是执行一个查询命令所耗费的时间。

另外,slow log保存在内存里面,读写速度非常快,因此你可以放心地使用它,不必担心因为开启slow log而损害Redis的速度。

慢查询日志四个属性:

1、第一个字段是每个慢查询唯一标识。

2、处理完命令后的时间戳

3、执行改名了所需要的时间,单位微妙

4、命令的参数列表,是个数组类型

每个慢查询实体的ID都是唯一的,而且不会被重新设置,只会在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"

可以用config set对这两个参数进行调整,或者在配置文件中设置。

##################################SLOWLOG####################################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

慢查询命令

语法:slowlog subcommand [argument]

如,进行查询慢查询、获取慢查询记录的数量、重置慢查询日志等操作:

192.168.10.38:9001>slowlogget(emptylistorset)192.168.10.38:9001>slowlogget10(emptylistorset)192.168.10.38:9001>slowloglen(integer)0192.168.10.38:9001>slowlogresetOK

关于“Redis中慢查询操作的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。