Home > Database > Redis > body text

Redis operation and maintenance slow query log

齐天大圣
Release: 2020-05-14 11:55:51
Original
1763 people have browsed it

To use Redis well, you must not only know how to use the API, but also know how to prevent Redis from blocking and check and analyze the blocking. Today, let’s talk to you about how to discover the blocking of Redis—finding blocked commands through slow queries.

Like Mysql, Redis also has slow query records. When the command execution time exceeds the set value, the command will be recorded in the slow query list. With slow queries, we can improve our programs. Prevent redis from blocking.

Configuration

There are two configuration parameters for slow query:

  • slowlog-log- slower-than

  • slowlog-max-len

slowlog-log-slower-than is used to set a threshold in microseconds. The default value is 10000, which is 10 milliseconds.

  • When the value is set to less than 0, no commands will be recorded;

  • When the value is equal to 0, all commands will be recorded .

slowlog-max-len indicates the maximum number of records, the default is 128, for example, set it to 10, when the 11th slow query is inserted, then the data at the head of the queue will be out of line.

These two configurations also support dynamic configuration. When the project has just started and the number of visits is not very large, the value of slow-log-slower-than can be set larger. As the number of visits increases, you may need to change its value to a smaller value. But we don't want to stop the redis service, so we can dynamically modify the configuration.

127.0.0.1:6379> config set slowlog-log-slower-than 1000
OK  
# 在线修改配置
127.0.0.1:6379> config rewrite
OK
# 将修改的配置持久化到配置文件中
Copy after login

Operation and maintenance suggestions: When the number of visits is large, we generally recommend setting slowlog-log-slower-than to 1000, or less. When this value is 1000, it means that redis can support up to 1000 concurrent .

Slow query view

The slow query view of redis is different from that of mysql. There are special commands for redis view.

Get slow query log

slowlog get [n], n represents the number of entries, the default is 10

127.0.0.1:6379> slowlog get
1) 1) (integer) 18004
   2) (integer) 1589424642
   3) (integer) 50
   4) 1) "slowlog"
      2) "get"
   5) "127.0.0.1:58364"
   6) ""
2) 1) (integer) 18003
   2) (integer) 1589423805
   3) (integer) 47
   4) 1) "slowlog"
      2) "get"
   5) "127.0.0.1:58364"
   6) ""
……
Copy after login
  • The first parameter is the log ID number

  • The second parameter is the occurrence timestamp

  • The third parameter is the command Execution time (microseconds)

  • The last one is the command and parameters.

Get the length of slow query

slowlog len

127.0.0.1:6379> slowlog len
(integer) 128
Copy after login

Clear the slow query list

127.0.0.1:6379> slowlog reset
OK
Copy after login

Persist the slow query log to mysql

Because the length of redis is limited , so when there are many slow queries, there will be loss. We can regularly go to redis to take out the slow query record list and persist it into mysql to prevent this from happening.

The pseudo code is as follows:

while (true) {
    $slowlen = $redis->slowlog('len');
    $slowlogs = $redis->slowlog('get',$slowlen);
    $insSql  = "INSERT INTO 
        slowlog(exec_time,run_time,command) VALUES";
     
    if (is_array($slowlogs) && count($slowlogs)) {
     
        foreach ($slowlogs as $slowlog) {
            $execTime = $slowlog[2];
            $runTime  = $slowlog[1];
            $command  = implode(' ', $slowlog[3]);
     
            $insSql .= "('$execTime', '$runTime','$command'),";
        }
     
        $flag = mysqli_query($mysqli, substr($insSql, 0, -1));
     
        if ($flag) {
            $redis->slowlog('reset');
        }
    }
    
    unsleep(10000);
}
Copy after login

The above code can realize the persistence of slow queries to MYSQL.

The above is the detailed content of Redis operation and maintenance slow query log. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template