使用 Redis 的 slowlog get [n] 慢查询日志彻底解决生产问题!
或多或少,你们可能听过 MySQL 的慢查询日志。其实 Redis 也有,而且 Redis 的慢查询日志对性能的影响很小,因为它就保存在内存中。
slowlog 是 Redis 用来记录查询执行时间的日志系统。注意,这个查询执行时间指的是不包括像客户端响应(talking)、发送回复等 IO 操作,而单单是执行一个查询命令所耗费的时间。
slowlog 保存在内存里面,读写速度非常快,因此我们可以放心地使用它,不必担心因为开启 slowlog 而损害 Redis 的速度。
slowlog 有两个重要的配置,我们先通过 CONFIG GET slowlog-* 命令来查看现有的配置。
1
2
3
4
5
127.0.0.1:6379> CONFIG GET slowlog-*
1) "slowlog-log-slower-than"
2) "100"
3) "slowlog-max-len"
4) "1024"
slowlog-log-slower-than 代表慢查询的阈值,单位为:微秒。当执行查询命令消耗时间大于配置的阈值时,会将该条命令记录到慢查询日志。当 slowlog-log-slower-than=0 时,记录所有命令。slowlog-log-slower-than<0 时,不记录任何命令。slowlog-log-slower-than 的默认值为 10000 (10毫秒,1秒 = 1,000毫秒 = 1,000,000微秒)。
slowlog-max-len 代表慢查询日志最大条数。它是一个队列形式的存储结构,先进先出的队列,即当慢查询日志达到最大条数后,会销毁最早记录的日志条目。slowlog-max-len 的默认值为 128,保存在内存内,所以重启 redis 会清空慢查询日志。
配置 slowlog-log-slower-than 和 slowlog-max-len 的命令非常简单,如下:
1
2
CONFIG SET slowlog-log-slower-than 100
CONFIG SET slowlog-max-len 1024
使用 SLOWLOG LEN 命令,查询当前的慢查询日志记录数。
1
2
127.0.0.1:6379> SLOWLOG LEN
(integer) 2019
当我们只需要查询前几个慢查询记录时,可以使用 SLOWLOG GET [n] 命令。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
127.0.0.1:6379> SLOWLOG GET 3
1) 1) (integer) 14 # 唯一性(unique)的日志标识符
2) (integer) 1522808219 # 被记录命令的执行时间点,以 UNIX 时间戳格式表示
3) (integer) 16 # 查询执行时间,以微秒为单位
4) 1) "keys" # 执行的命令,以数组的形式排列
2) "*" # 这里完整的命令是 "keys *"
2) 1) (integer) 13
2) (integer) 1522808215
3) (integer) 7
4) 1) "set"
2) "name"
3) "baicai"
3) 1) (integer) 12
2) (integer) 1522808198
3) (integer) 101
4) 1) "set"
2) "age"
3) "25"
SLOWLOG GET [n] 若不加 n ,则获取全部慢查询记录。
清空慢查询日志使用 SLOWLOG RESET。注意,slowlog-log-slower-than 不要设置过大,设置过大有可能一条记录也不会记录。
参考地址 https://www.xttblog.com/?p=3708
————————————————
版权声明:本文为CSDN博主「那些年的代码」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44018338/article/details/99460667