An example tutorial for Redis analyzing slow query operations

  • 2020-06-12 10:58:03
  • OfStack

What is a slow query

The role of slow query: through slow query analysis, find the command in question for optimization.

Like mysql's slow SQL log analysis, redis has a similar feature to help locate slow query operations.

Redis slowlog is the logging system that Redis USES to record query execution times.

Query execution time refers to the time it takes to execute just one query command, excluding IO operations such as client response (talking) and sending replies.

In addition, slow log is stored in memory and reads and writes very fast, so you can use it safely without worrying about damaging Redis's speed by turning on slow log.

4 attributes of slow query log:

1. The first field is the only id for each slow query.

2. Time stamp after processing the command

3, the implementation of the renaming required time, unit subtlety

4, the command parameter list, is an array type

ID for each slow query entity is unique and will not be reset, only reset after redis restarts.

Slow query parameters

Let's first focus on the two parameters for slow log analysis:

1. slowlog-log-slower-than: The default threshold, that is, the record of how much time is exceeded, is 10000 microseconds by default, that is, 10 milliseconds.

2. slowlog-max-len: The number of slow queries is recorded, which is 128 by default. When the number exceeds the set number, the earliest ones entering the queue will be removed. It is recommended to increase the value, such as 1000, to reduce the frequency of queue removal.


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"

Both parameters can be adjusted with config set or set in a configuration file.


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

Slow query command

Grammar: slowlog subcommand [argument]

For example, slow query, get the number of slow query records, reset the slow query log and other operations:


192.168.10.38:9001> slowlog get
(empty list or set)
192.168.10.38:9001> slowlog get 10
(empty list or set)
192.168.10.38:9001> slowlog len 
(integer) 0
192.168.10.38:9001> slowlog reset
OK

conclusion


Related articles: