Redis(2)常用命令

数据类型和操作

string

set

hash

list

sorted set

geospatial index

bitmaps

hyperloglog

stream

pub/sub

redis服务管理命令

慢查询

慢查询时间阈值的配置在/etc/redis.conf文件内可以看到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$ cat /etc/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
...

slowlog-log-slower-than用来控制慢查询的超时时间,单位是微秒,默认配置是10毫秒。

slowlog-max-len配置保存的慢查询的数量,默认是128个。

也可以通过redis-cli来获取这两个配置参数。

1
2
3
4
5
6
127.0.0.1:6379> config get slowlog-log-slower-than
1) "slowlog-log-slower-than"
2) "10000"
127.0.0.1:6379> config get slowlog-max-len
1) "slowlog-max-len"
2) "128"

也可以动态的设置slowlog参数。

1
2
3
4
127.0.0.1:6379> CONFIG SET slowlog-log-slower-than 1
OK
127.0.0.1:6379> CONFIG SET slowlog-max-len 1000
OK

查看slowlog日志。

1
2
3
4
5
6
127.0.0.1:6379> SLOWLOG GET 1
1) 1) (integer) 26 // slowlog唯一编号id
2) (integer) 1440057815 // 查询的时间戳
3) (integer) 47 // 查询的耗时(微妙),如表示本条命令查询耗时47微秒
4) 1) "SLOWLOG" // 查询命令,完整命令为 SLOWLOG GET,slowlog最多保存前面的31个key和128字符
2) "GET"

查看slowlog的总条数。

1
2
127.0.0.1:6379> slowlog len
(integer) 18

清空所有的slowlog。

1
2
127.0.0.1:6379> slowlog reset
OK

Redis 用一个链表以 FIFO 的顺序保存着所有慢查询日志。

参考

Redis data types

Redis 慢查询日志