当前位置:网站首页>Redis introduction complete tutorial: slow query analysis

Redis introduction complete tutorial: slow query analysis

2022-07-04 22:52:00 Gu Ge academic

Many storage systems ( for example MySQL) Provide slow query logs to help developers and operation and maintenance personnel set
Slow operation of bit system . The so-called slow query log is that the system calculates each life before and after the command execution
Execution time of order , When the default threshold is exceeded , This command will be the relevant information ( for example : occurs
between , Time consuming , Command details ) recorded ,Redis It also provides a similar function .
Pictured 3-1 Shown ,Redis A command executed by the client is divided into the following 4 Parts of :

1) dispatch orders
2) Order to line up
3) Command execution
4) Return results
We need to pay attention to , Slow query only counts steps 3) Time for , So no slow query doesn't mean customers
There is no timeout problem on the client . 

3.1.1  Two configuration parameters of slow query
For slow query function , Two things need to be clear :
· How to set the preset threshold ?
· Slow query where records are stored ?
Redis Provides slowlog-log-slower-than and slowlog-max-len Configuration to solve these two problems
problem . It can be seen from the literal meaning that ,slowlog-log-slower-than That's the preset threshold ,
It's in microseconds (1 second =1000 millisecond =1000000 Microsecond ), The default value is 10000, If hold
OK, one “ Very slowly ” The order of ( for example keys*), If its execution time exceeds 10000 tiny
second , Then it will be recorded in the slow query log .
Operation and maintenance tips
If slowlog-log-slower-than=0 All orders will be recorded ,slowlog-log-slower-
than<0 No command is recorded .
Literally ,slowlog-max-len It just shows how much slow query logs can be stored at most
strip , It doesn't say where it's stored ? actually Redis A list is used to store slow query days
Records ,slowlog-max-len Is the maximum length of the list . When a new command satisfies the slow query condition
Inserted into this list , When the slow query log list is at its maximum length , First inserted
A command will be removed from the list , for example slowlog-max-len Set to 5, When there is a second 6 Slow query
Insert the words , Then the first data at the head of the team will be listed , The first 6 A slow query will be listed .
stay Redis There are two ways to modify the configuration , One is to modify the configuration file , The other is to make
use config set Command dynamic modification . For example, use config set The order will slowlog-log-slower-
than Set to 20000 Microsecond ,slowlog-max-len Set to 1000:
config set slowlog-log-slower-than 20000
config set slowlog-max-len 1000
config rewrite
If you want to Redis Persistent configuration to local configuration file , You need to perform config rewrite life
Make , Pictured 3-2 Shown .

  Although the slow query log is stored in Redis In the memory list , however Redis Did not expose this
Key of a list , But through a set of commands to achieve the slow query log access and management . Below
Shao these orders .
(1) Get slow query log
slowlog get [n]
The following operation returns the current Redis The slow query , Parameters n You can specify the number :
127.0.0.1:6379> slowlog get
1) 1) (integer) 666
2) (integer) 1456786500
3) (integer) 11615
4) 1) "BGREWRITEAOF"
2) 1) (integer) 665
2) (integer) 1456718400
3) (integer) 12006
4) 1) "SETEX"
2) "video_info_200"
3) "300"
4) "2"
...
You can see that each slow query log has 4 Attributes make up , They are the identification of the slow query log
id、 Occurrence timestamp 、 The command takes time 、 Execute commands and parameters , The slow query list is shown in the figure 3-3 Shown .

 (2) Get the current length of the slow query log list
slowlog len
for example , At present Redis There is 45 Slow query :
127.0.0.1:6379> slowlog len
(integer) 45
(3) Slow query log reset

slowlog reset
It's actually cleaning up the list , for example :
127.0.0.1:6379> slowlog len
(integer) 45
127.0.0.1:6379> slowlog reset
OK
127.0.0.1:6379> slowlog len
(integer) 0

3.1.2  Best practices
The slow query function can effectively help us find Redis Possible bottlenecks , But in reality
Pay attention to the following points during use :
·slowlog-max-len Configuration suggestions : It is suggested to enlarge the slow query list online , When recording slow queries
Redis Will truncate long commands , It doesn't take up a lot of memory . Increasing the slow query list can
Slow down the possibility of slow queries being eliminated , For example, online can be set to 1000 above .
·slowlog-log-slower-than Configuration suggestions : Default value exceeds 10 MS is judged as slow query ,
Need basis Redis The amount of concurrency adjusts the value . because Redis Using single thread response command , For high flow
The scene of quantity , If the command is executed at 1 Milliseconds or more , that Redis Support at most OPS Less than
1000. So for high OPS Of the scene Redis Recommended setting is 1 millisecond .
· Slow queries only record command execution time , Command queuing and network transmission time are not included . because
The execution time of this client will be longer than the actual execution time of the command . Because the command execution queue machine
system , Slow queries can cause other command cascades to block , So when the client has a request timeout , It needs to be checked
Check whether there is a corresponding slow query at this time point , So we can analyze whether it is the command cascade caused by slow query
Blocking .
· Because slow query log is a FIFO queue , In other words, if there are many slow queries
Under the circumstances , Some slow query commands may be lost , To prevent this from happening , On a regular basis
perform slow get Command to persist slow query logs to other stores ( for example MySQL), then
You can make a visual interface to query , The first 13 This chapter introduces Redis Private cloud CacheCloud Provide
This function , Good tools can get twice the result with half the effort .

原网站

版权声明
本文为[Gu Ge academic]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207042229263813.html