<> What is slow query ?
Slow query , As the name suggests, it's a slow query , But how slow is it ? First , Let's get to know Redis The whole process of command execution ：
* dispatch orders
* Command queue
* Command execution
* Return result
In the definition of slow query , The slow time period of statistics refers to the command execution of this step . No slow query , Does not mean that the client has no timeout problem , It is possible that there is a delay in network transmission , It's also possible that there are more orders queued .
because Redis Queuing mechanism of command execution in , Slow queries can cause cascading blocking of other commands , So when the client's request times out , Need to check whether there is slow query at this time point , The command cascading block caused by slow query is analyzed .
<> What is slow query log ?
Slow query log is Redis The server calculates the execution time of each command before and after the command execution , When a threshold is exceeded, the log is recorded . The time of slow query is recorded in the log , And the execution time , Specific orders and other information , It can be used to help the development and operation and maintenance personnel locate the slow query in the system .
<> How to get slow query log ?
have access to slowlog get Command get slow query log , stay slowlog get You can add a number after that , Used to specify the number of slow query logs to get , such as , Obtain 3 Slow query log ：
> 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)
From the example above , You can see that every slow query log has 4 Attribute composition ：
* Unique identification ID
* Timestamp of command execution
* Command execution time
* Names and parameters of execution
<> How to get the length of slow query log ?
have access to slowlog len Command to get the length of slow query log , such as ：
> slowlog len (integer) 121
In the above example , current Redis There are 121 Slow query log .
<> How to clean up slow query logs ?
have access to slowlog reset Command cleanup slow query log , such as ：
> slowlog len (integer) 121 > slowlog reset OK > slowlog len (integer) 0
<> How to configure slow query parameters ?
As mentioned above , Slow query requires the following two configurations ：
* Specified threshold for command execution time .
* Number of slow query logs .
Redis Two parameters are provided ：slowlog-log-slower-than and slowlog-max-len, Let's go through these two parameters in detail .
slowlog-log-slower-than Is used to specify a threshold for the length of command execution , When the execution time exceeds this threshold, it will be recorded . It's in microseconds （1 second =
1000 Millisecond =
1000000 Microsecond ）, The default is 10000 Microsecond . If put slowlog-log-slower-than Set to 0, All commands will be logged . If put slowlog-log-slower-than Set less than 0, No command will be logged .
In the actual production environment , Need basis Redis Concurrent amount to adjust the configuration . because Redis Response command with single thread , If the command execution time is 1000 Microsecond , that Redis Up to support OPS Less than 1000, So for high concurrency scenarios Redis Recommended setting is
1000 Microsecond .
slowlog-max-len Specifies the maximum number of slow query logs stored . Actually ,Redis A list is used to store slow query logs ,slowlog-max-len Is the maximum length of this list . When a new command satisfies the slow query condition , Is inserted into this list . When the slow query log list has reached the maximum length , The first command inserted will be removed from the list . such as ,slowlog-max-len Set to 10, When there are 11 When commands are inserted , In the list, No 1 Orders are removed first , And then I put the 11 Commands in list .
Record slow query yes Redis Truncates long commands , Not a lot of memory . In the actual production environment , In order to slow down the possibility of slow queries being moved out and locate slow queries more easily , It is recommended to adjust the length of the slow query log to a larger size . For example, it can be set to
1000 Above .
<> How to configure
stay Redis There are two ways to modify a configuration in ：
* modify Redis configuration file . such as , hold slowlog-log-slower-than Set to 1000,slowlog-max-len Set to 1200：
slowlog-log-slower-than 1000 slowlog-max-len 1200
* Use config set
Command dynamic modification . such as , Still put slowlog-log-slower-than Set to 1000,slowlog-max-len Set to 1200： > config
set slowlog-log-slower-than 1000 OK > config set slowlog-max-len 1200 OK >
config rewrite OK
If you want Redis Persist configuration to local configuration file , Need execution config rewrite command .
Slow query refers to the query with long command execution time . adopt slowlog get Command get slow query log ; adopt slowlog len Command to get the length of slow query log ; adopt slowlog
reset Command cleanup slow query log . adopt slowlog-log-slower-than Configure threshold for command execution time ; adopt slowlog-max-len
Configure the maximum number of slow query logs to store .