Home > Database > Redis > body text

What is redis slow query

藏色散人
Release: 2020-11-04 17:12:14
Original
3583 people have browsed it

Redis slow query refers to a query with a relatively long command execution time. The slow query log is the Redis server calculating the execution time of each command before and after the command is executed. When it exceeds a certain threshold, the log is recorded. , the log records the time when the slow query occurred, as well as the execution time, specific command and other information.

What is redis slow query

Recommendation: "redis tutorial"

What is slow query?

Slow query, as the name suggests, is a relatively slow query, but where is it slow? First, let’s understand the entire process of Redis command execution:

  1. Send command
  2. Command queuing
  3. Command execution
  4. Return results

In the definition of slow query, the time period when statistics are relatively slow refers to the step of command execution. The absence of slow queries does not mean that the client does not have timeout problems. There may be delays in network transmission, or there may be more commands queued.

Because of the queuing mechanism for command execution in Redis, slow queries will cause cascading blocking of other commands. Therefore, when a request timeout occurs on the client, it is necessary to check whether there is a slow query at that time point, so as to analyze the reason why Command cascade blocking caused by slow query.

What is a slow query log?

The slow query log is a log that the Redis server calculates the execution time of each command before and after the command is executed. When it exceeds a certain threshold, it is recorded. The log records the time when the slow query occurred, as well as the execution time, specific commands and other information. It can be used to help development and operation and maintenance personnel locate slow queries that exist in the system.

How to obtain the slow query log?

You can use the slowlog get command to obtain the slow query log. You can also add a number after slowlog get to specify the number of slow query logs to obtain, such as , get 3 slow query logs:

> slowlog get 3
1) 1) (integer) 6107
   2) (integer) 1616398930
   3) (integer) 3109
   4) 1) "config"
      2) "rewrite"
2) 1) (integer) 6106
   2) (integer) 1613701788
   3) (integer) 36004
   4) 1) "flushall"
3) 1) (integer) 6105
   2) (integer) 1608722338
   3) (integer) 20449
   4) 1) "scan"
      2) "0"
      3) "MATCH"
      4) "*comment*"
      5) "COUNT"
      6) "10000"
Copy after login

From the above example, you can see that each slow query log consists of 4 attributes:

  1. Unique identification ID
  2. Time stamp of command execution
  3. Command execution duration
  4. Name and parameters of execution

How to obtain the length of slow query log?

You can use the slowlog len command to obtain the length of the slow query log, for example:

> slowlog len
(integer) 121
Copy after login

In the above example, there are currently 121 slow query logs in Redis.

How to clear slow query logs?

You can use the slowlog reset command to clear the slow query log, for example:

> slowlog len
(integer) 121
> slowlog reset
OK
> slowlog len
(integer) 0
Copy after login

How to configure the slow query parameters?

As mentioned above, slow query requires the following two configurations:

  1. The specified threshold for the command execution time.
  2. The number of slow query logs stored.

Redis provides two parameters: slowlog-log-slower-than and slowlog-max-len. Next, we will introduce these two parameters in detail.

slowlog-log-slower-than

The function of slowlog-log-slower-than is to specify the threshold of the command execution time. When the execution time of the command exceeds this threshold, it will be recorded. Its unit is microseconds (1 second = 1000 milliseconds = 1000000 microseconds), and the default is 10000 microseconds. If slowlog-log-slower-than is set to 0, all commands will be logged. If slowlog-log-slower-than is set to less than 0, no commands will be recorded in the log.

In the actual production environment, this configuration needs to be adjusted according to the Redis concurrency. Because Redis uses a single thread to respond to commands, if the command execution time is more than 1000 microseconds, Redis can support up to less than 1000 OPS. Therefore, it is recommended to set Redis to 1000 microseconds for high concurrency scenarios.

slowlog-max-len

slowlog-max-len is used to specify the maximum number of slow query logs to store. In fact, Redis uses a list to store slow query logs, and slowlog-max-len is the maximum length of this list. When a new command meets the slow query conditions, it is inserted into this list. When the slow query log list reaches the maximum length, the earliest inserted command will be removed from the list. For example, slowlog-max-len is set to 10. When the 11th command is inserted, the first command in the list is removed first, and then the 11th command is added to the list.

Recording slow queries means that Redis will truncate long commands and will not occupy a large amount of memory. In an actual production environment, in order to reduce the possibility of slow queries being removed and to locate slow queries more conveniently, it is recommended to adjust the length of the slow query log to be larger. For example, it can be set to 1000 or above.

How to configure

There are two ways to modify the configuration in Redis:

  1. 修改Redis配置文件。比如,把slowlog-log-slower-than设置为1000,slowlog-max-len设置为1200:
slowlog-log-slower-than 1000
slowlog-max-len 1200
Copy after login
  1. 使用config set命令动态修改。比如,还是把slowlog-log-slower-than设置为1000,slowlog-max-len设置为1200:
> config set slowlog-log-slower-than 1000
OK
> config set slowlog-max-len 1200
OK
> config rewrite
OK
Copy after login

如果要Redis把配置持久化到本地配置文件,需要执行config rewrite命令。

总结

慢查询指的是命令执行时长比较长的查询。通过slowlog get命令获取慢查询日志;通过slowlog len命令获取慢查询日志的长度;通过slowlog reset命令清理慢查询日志。通过slowlog-log-slower-than配置命令执行时长的阈值;通过slowlog-max-len配置慢查询日志最多存储的条数


The above is the detailed content of What is redis slow query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template