Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does redis slow query refer to?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what redis slow query refers to. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Redis slow query refers to the query for which the command takes a long time to execute. The slow query log means that the Redis server calculates the execution time of each command before and after the command execution. When a certain threshold is exceeded, the log records the time when the slow query occurs, as well as the execution time, specific command and other information.

What is a slow query?

Slow query, as the name implies, is a relatively slow query, but where is slow? First, let's take a look at the whole process of Redis command execution:

Send command queue command to execute the returned result

In the definition of a slow query, the time period in which statistics are slow refers to the execution of this step by the command. There is no slow query, it does not mean that the client does not have a timeout problem, there may be delays in network transmission, or there may be more commands queued.

Because of the queuing mechanism of command execution in Redis, slow query will lead to cascade blocking of other commands, so when the client has a request timeout, it is necessary to check whether there is a slow query at this point in time, so as to analyze the command cascade blocking caused by slow query.

What is a slow query log?

The slow query log is the log that the Redis server calculates the execution time of each command before and after the command execution, and records it when it exceeds a certain threshold. The log records the time when the slow query occurs, as well as the execution time, specific commands and other information, which can be used to help developers and operators locate slow queries in the system.

How to get the slow query log?

You can use the slowlog get command to obtain slow logs, and you can add a number after slowlog get to specify the number of entries to obtain slow logs, for example, get 3 slow logs:

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

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

How do the naming and parameters that uniquely identify the timestamp of the execution of the ID command get the length of the slow query log?

You can use the slowlog len command to get the length of the slow query log, such as:

> slowlog len (integer) 121

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

How to clean up the slow query log?

You can use the slowlog reset command to clean up the slow query log, such as:

> slowlog len (integer) 121 > slowlog resetOK > slowlog len (integer) 0 how to configure the parameters of slow query?

As mentioned above, slow queries require the following two configurations:

The specified threshold for the length of time the command is executed. The number of entries that store slow query logs.

Redis provides two parameters: slowlog-log-slower-than and slowlog-max-len, which we will describe in more detail next.

Slowlog-log-slower-than

The role of slowlog-log-slower-than is to specify a threshold for how long a command will be executed, beyond which it will be recorded. It is measured in 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 to the log. If slowlog-log-slower-than is set to less than 0, no commands will be logged.

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

Slowlog-max-len

The purpose of slowlog-max-len is to specify the maximum number of slow log entries to be stored. 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 satisfies the slow query condition, it is inserted into the list. When the slow log list has reached its maximum length, the earliest inserted command will be removed from the list. For example, slowlog-max-len is set to 10, and when the 11th command is inserted, the 1st command in the list is removed first, and then the 11th command is put into the list.

Recording slow queries means that Redis truncates long commands and does not take up a lot of memory. In the actual production environment, in order to slow down the possibility of slow query being moved out and make it easier to locate slow query, it is suggested that the length of slow query log should be adjusted larger. For example, it can be set to more than 1000.

How to configure

There are two ways to modify the configuration in Redis:

Modify the Redis configuration file. For example, set slowlog-log-slower-than to 1000 and 1200:slowlog-log-slower-than 1000slowlog-max-len 1200 to modify dynamically using the config set command. For example, still set the slowlog-log-slower-than to 1000 and set it to 1200: > config set slowlog-log-slower-than 1000OK > config set slowlog-max-len 1200OK > config rewriteOK.

If you want Redis to persist the configuration to the local configuration file, you need to execute the config rewrite command.

Summary

A slow query refers to a query in which the command takes a long time to execute. The slow query log is obtained by the slowlog get command; the length of the slow query log is obtained by the slowlog len command; and the slow query log is cleaned by the slowlog reset command. Configure the threshold of command execution time through slowlog-log-slower-than; configure the maximum number of slow query logs stored through slowlog-max-len

On the redis slow query refers to what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report