preface
Finally, the first Redis advanced. Slow query about Redis, yes, good brothers you read that right, is slow query. You don’t think that only databases like mysql have slow queries. You see this is not to learn it (ok, ok, don’t hit, know you strong male good brother will). Is not very looking forward to ah, anyway, I did not know before. So let’s go for it. One unit…
An overview of the
The so-called slow query log is that the system calculates the execution time of each command before and after the command is executed. When the value exceeds the preset threshold, the system records the information related to the command (such as the occurrence time, time, and command details). This function is so fragrant, I big Redis how can not have.
The life cycle
In Redis, executing a command can be broken down into four stepsCommand sending, command queuing, command execution, result responseNote that slow queries in Redis only count the third step (command execution). I paint you guess
Best practices
How can we set a time as Mysql does, when the time exceeds the time is recorded in the slow query log? What about these slow query logs? Beefcake I helped my brothers clean up.
1 Set thresholds
Redis provides lowlog-log-slower configuration to set a preset threshold in microseconds (1 second =1000 ms =1000000 microseconds), default is 10000 if a “slow” command (for example keys*) is executed If it takes more than 10,000 microseconds to execute, it is logged in the slow query log. Note that slowlog-log-slower than=0 records all commands, slowlog-log-slower than<0 does not record any commands.
2 Log Storage
Redis provides slowlog – Max – len command, see literally just shows the slow query log how storage, most did not say in where is it. Yes, the good brothers are right. Redis actually uses a list to store slow query logs, with slowlog-max-len being the maximum length of the list. A new command is inserted into the list when it meets the slow query condition. When the slow query log list reaches its maximum length, the earliest inserted command is removed from the list. For example, slowlog-max-len defaults to 128. The 129 slow query will enter the column.
3 the configuration
Redis provides two ways to configure, the first way to modify the startup configuration file, this way you need to find a good brother corresponding to the startup Redis configuration (do not make a mistake, otherwise try not to come out). For example, my configuration is at /home/redis-4.0.0, then execute the following command. Even the executive order, knock what all come out, sure don’t focus on praise it (has not clicked the praise plus concern are cheating and playing with women’s feelings of men, like white piao. All right, stop it. The other option is to use the config set command to dynamically modify the configuration file. In this way, it is best not to use the default configuration file, otherwise the persistent configuration will report an error (test).
The first kind of
Edit the configuration, of course you can use other Linux modification commands
vim redis.conf
## Search in English/when opened
/slowlog-log-slower-than
Press I to edit it and set it to 1 second
slowlog-log-slower-than 1000000
## Press ESC to exit editing, save, and restart Redis
:wq
Copy the code
The second,
Enter the Redis client
redis-cli
Dynamic configuration timeout is 1 second
config set slowlog-log-slower-than 1000000
The dynamic configuration column length is 500
config set slowlog-max-len 500
Write configuration files persistently
config rewrite
Copy the code
4 Obtaining Logs
Ah, good brother is not also found, just said the log is in the queue, that also did not tell me in which queue ah, I want to get the log. Hey, Antirez has that all figured out for us. To obtain the slow query logs, run the following command.
# # format. N indicates the number of items
slowlog get [n]
# # format. View how many logs there are
slowlog len
# # format. Reset the log
slowlog reset
Copy the code
5 Log Parsing
Executing the above command will return the following contents: slow query log id, execution timestamp, command time, execution command and parameter.
advice
- It is recommended to increase the slowlog-max-len parameter of the slow query list. Redis truncates long commands when recording slow query, which does not occupy large memory. Enlarging the list of slow queries reduces the likelihood that slow queries will be culled.
- Slowlog-log-slower than the default value of slowlog-slower is slower than 10 ms. Adjust this value based on the amount of Redis concurrency. Since Redis is single-threaded, it is recommended that this parameter be set low.
- Slow query only records the command execution time, excluding the command queuing time and network transmission time. Therefore, the command execution time on the client is longer than the actual command execution time.
- Slow query logs are in a first-in, first-out queue. In other words, some slow query commands may be lost if there are too many slow query commands. To prevent this from occurring, you can run the slow get command periodically to persist slow query logs to other storage (such as MySQL).
That’s the end of this issue. Welcome to leave your comments in the comments sectionAsk for attention, ask for likes
Redis Pipeline this is enough last article :Redis global key management parsing