Redis:慢日志slowlog

本文介绍了Redis慢查询日志的配置方法,包括如何设置慢日志的执行耗时阈值及保存数量阈值,并提供了相关操作命令示例。

redis.conf 慢日志配置

################################## SLOW LOG ###################################

# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I/O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (this is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
#
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order for the
# command to get logged, and the other parameter is the length of the
# slow log. When a new command is logged the oldest one is removed from the
# queue of logged commands.


# The following time is expressed in microseconds, so 1000000 is equivalent
# to one second. Note that a negative number disables the slow log, while
# a value of zero forces the logging of every command.
# 设置记录超过指定时间的命令,单位微秒
slowlog-log-slower-than 10000


# There is no limit to this length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
# 最大保留条数
slowlog-max-len 128
 
 

常用指令:

1、设置slowlog的执行耗时阈值

127.0.0.1:6379> config set slowlog-log-slower-than 1000
OK
2、查看slowlog执行耗时的阈值
127.0.0.1:6379> config get slowlog-log-slower-than
1) "slowlog-log-slower-than"
2) "10000"

3、设置slowlog保存数量的阈值

127.0.0.1:6379> config set slowlog-max-len 1024
OK

4、查看slowlog保存数量的阈值

 
127.0.0.1:6379> config get slowlog-max-len
1) "slowlog-max-len"
2) "1024"

5、查看slowlog的数量

127.0.0.1:6379> slowlog len
(integer) 1024

6、查询slowlog

 
7、清除slowlog记录
127.0.0.1:6379> slowlog reset
OK

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
### 配置和使用 Redis 慢查询日志 #### 1. 启用慢查询日志 Redis 的慢查询日志功能可以帮助开发者识别那些执行时间过长的命令,从而优化数据库性能。启用该功能需要调整两个主要参数:`slowlog-log-slower-than` 和 `slowlog-max-len`。 - **`slowlog-log-slower-than` 参数** 此参数决定了哪些命令会被记录到慢查询日志中。它的单位是微秒(μs),默认值为 10,000 微秒(即 10 毫秒)。如果某个命令的执行时间超过了这个设定值,则会被写入慢查询日志。当将此参数设为 `-1` 时,可以完全禁用慢查询日志;而将其设置为 `0` 则会记录所有被执行过的命令[^1]。 - **`slowlog-max-len` 参数** 这一参数用来限制慢查询日志列表的最大长度。一旦达到上限,最早的一条记录将会被移除以腾出空间给新的记录。例如,如果你希望只保留最新的 500 条慢查询日志,可将此参数设置为 `500`[^4]。 #### 2. 修改配置的方法 有两种途径去改变这些参数——静态修改或者动态修改: - **静态修改 (通过配置文件)** 编辑 Redis 配置文件 (`redis.conf`) 中的相关字段: ```conf slowlog-log-slower-than 10000 slowlog-max-len 128 ``` 完成编辑之后记得重启 Redis 服务以便应用改动。 - **动态修改 (不需停机)** 使用 `CONFIG SET` 命令即时调整这两个选项而不需要停止服务: ```bash CONFIG SET slowlog-log-slower-than 5000 CONFIG SET slowlog-max-len 500 ``` #### 3. 查看慢查询日志 为了获取当前存储在内存里的慢查询日志内容,可以调用以下命令: ```bash SLOWLOG GET [count] ``` 其中 `[count]` 是一个可选参数,指定了返回的日志数量。如果不提供它,默认会显示全部已有的慢查询日志条目。 另外还可以借助其他辅助性的 SLOWLOG 子命令来管理和监控慢查询日志的行为状态,比如: - 获取慢查询日志总次数及最大保存数目等元数据信息: ```bash SLOWLOG LEN ``` - 清空现有的慢查询日志缓存区: ```bash SLOWLOG RESET ``` #### 示例代码片段展示如何操作慢查询日志 下面是一组完整的例子演示怎样先期配置再读取 Redis 的慢查询日志: ```python import redis r = redis.Redis(host='localhost', port=6379) # 动态设置慢查询阈值为 5ms print(r.config_set('slowlog-log-slower-than', '5000')) # 设定最多保持 500 条慢查询日志 print(r.config_set('slowlog-max-len', '500')) # 展示最后十条慢查询日志详情 for entry in r.slowlog_get(10): print(entry) ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值