MySQL There are slow queries in ,Redis There are also slow queries in ,Redis My slow query is If the command execution exceeds the set threshold, it is a slow query . Let's sort it out .
The slow query
Redis It will record the command whose execution time exceeds the set threshold time , Slow query here means slow command execution , It's not I/O slow .
In general , We all connect through the client Redis The server , Then send the command to Redis The server ,Redis The server will cache the commands from each client into a queue , And then execute one by one , Finally, return the result to the client . And here we are Slow query means “ Carry out orders ” That part. . Not the Internet I/O perhaps The problem of command queuing .
About slow query configuration
There are two configurations for slow queries , They are as follows :
slowlog-log-slower-than: Slow query threshold , Command execution exceeds the value set by the configuration parameter , It is considered slow query ;
slowlog-max-len: The maximum number of slow query log records , That is to say Redis How many slow query records can be recorded at most . Slow query record is a list type , If the maximum allowable record 100 strip , So in record number 101 Stripe time , The first 1 Records will be removed , The number of records that always keep slow queries is 100 strip .
Slow query related commands
Here are a few commands , These commands are related to slow queries , A screenshot of the command and configuration is shown below :
config Commands are commands for configuration , Not just for slow queries , Because we will use these two commands , Therefore, I would also like to mention .config Commands can be obtained and set dynamically Redis Some parameters of the server , Usually we view and set the parameters to enter redis.conf In this file , But at run time, you can use config Command to quickly get and set parameters .
To use config get obtain slowlog-log-slower-than and slowlog-max-len Two parameters , The order is as follows .
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"
From the return result of the run, we can see that ,slowlog-log-slower-than The value of is 10000, This value is in microseconds , that 10000 Microseconds are 10 millisecond .slowlog-max-len The value of is 128, namely Redis Save only 128 Slow query records .
slowlog-log-slower-than If set to 0, Record all commands to the slow query queue , If slowlog-log-slower-than Set to Less than 0 Value , It means that no command is recorded in the slow query queue .
stay redis-cli Next , Use config The command has no syntax prompt !
The above two configurations are about slow query configuration , About slow query command Redis Provides slowlog The order of , This command can provide some parameters , Introduce the following .
slowlog get [n]: obtain [ Specify the number ] Slow query list of ;
slowlog len: Get the number of slow query records ;
slowlog reset: Clear the slow query list .
The above is about the configuration and command of slow query .
Command demonstration
There are not many configurations and commands about slow query , The command here demonstrates , Mainly to see slowlog get Return result of command .
View the configuration parameters of slow query
Look at slowlog-log-slower-than and slowlog-max-len The values of the two configuration parameters .
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-log-slower-than The default value of is 10000 Microsecond , That is to say 10 millisecond , This has been described before .slowlog-max-len The default value of is 128, That is to say, the slow query command queue can be saved 128 Slow query records .
Set slow query configuration parameters
Set the values of the two parameters to the values suitable for our learning .
127.0.0.1:6379> config set slowlog-log-slower-than 0 OK 127.0.0.1:6379> config set slowlog-max-len 5 OK
slowlog-log-slower-than Is set to 0, So you can record all the orders , And then slowlog-max-len Is set to 5, That is to say, only save 5 Slow query records .
Just test a few commands
Just test a few commands , It is convenient for us to check the content of slow query later .
127.0.0.1:6379> keys * 1) "k1" 2) "k2" 127.0.0.1:6379> get k1 "v1" 127.0.0.1:6379> hgetall k2 1) "k21" 2) "v1" 3) "k22" 4) "v2"
see Redis The number of slow queries recorded
adopt slowlog len Command to see how many slow query commands are recorded .
127.0.0.1:6379> slowlog len (integer) 5
Check slow query
adopt slowlog get Command to view the list of slow queries .
127.0.0.1:6379> slowlog get 1) 1) (integer) 6 2) (integer) 1610156168 3) (integer) 2 4) 1) "slowlog" 2) "len" 5) "127.0.0.1:58406" 6) "" 2) 1) (integer) 5 2) (integer) 1610156086 3) (integer) 10 4) 1) "hgetall" 2) "k2" 5) "127.0.0.1:58406" 6) "" 3) 1) (integer) 4 2) (integer) 1610156073 3) (integer) 9 4) 1) "get" 2) "k1" 5) "127.0.0.1:58406" 6) "" 4) 1) (integer) 3 2) (integer) 1610156064 3) (integer) 6 4) 1) "keys" 2) "*" 5) "127.0.0.1:58406" 6) "" 5) 1) (integer) 2 2) (integer) 1610155960 3) (integer) 5 4) 1) "config" 2) "set" 3) "slowlog-max-len" 4) "5" 5) "127.0.0.1:58406" 6) ""
You can see that the above list is all the slow query commands , Because we only keep 5 strip . For convenience, we only look at one , The order is as follows .
127.0.0.1:6379> slowlog get 1 1) 1) (integer) 7 2) (integer) 1610156232 3) (integer) 24 4) 1) "slowlog" 2) "get" 5) "127.0.0.1:58406" 6) ""
In the output above , There are six lines , Explain separately :
1. Slow query label of record , Inverted display
2. Record the timestamp of the command
3. The time it takes to execute a command , In microseconds
4. Specific orders to execute
5. Execute the client's IP Address and port number
With the above information, we can easily locate which client executed which command to cause slow query .
summary
Redis Slow down may be due to slow operation on some data structures , It may also be that improper data structure is used . Yes, of course , Lead to Redis There's a lot of slowing down , It's not just the execution part that leads to , But slow queries can only help us record slow commands , As for the cause Redis The reason why it's so slow , It's a multi-faceted search .
This article is from WeChat official account. - Code the agriculture UP2U(gh_3c91b47a82e0) , author : Code the agriculture UP2U
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .
Original publication time : 2021-01-11
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .