Redis——慢查询日志

    Redis的慢查询日志功能用于记录执行时间超过给定时长的命令请求,用户能够经过这个功能产生的日志来监视和优化查询速度。
    服务器配置有两个和慢查询日志相关的选项:
    * slowlog-log-slower-than 选项指定执行时间超过多少微妙(1秒等于1 000 000微妙)的命令请求 会被记录到日志上。
    * showlog-max-len 选项指定服务器最多保存多少条慢查询日志。
    
    服务器使用先进先出的方式保存多条慢查询日志,当服务器存储的慢查询日志数量等于
    slowlog-max-len 选线的值时,服务器在添加一条新的慢查询日志以前,会先将最旧的一条慢查询日志删除。redis

################################## SLOW LOG    ###################################

    # The Redis Slow Log is a system to log queries that exceeded a specified
    # execution time. The execution time does not include the I/O operations
    # like talking with the client, sending the reply and so forth,
    # but just the time needed to actually execute the command (this is the only
    # stage of command execution where the thread is blocked and can not serve
    # other requests in the meantime).
    #
    # You can configure the slow log with two parameters: one tells Redis
    # what is the execution time, in microseconds, to exceed in order for the
    # command to get logged, and the other parameter is the length of the
    # slow log. When a new command is logged the oldest one is removed from the
    # queue of logged commands.

    # The following time is expressed in microseconds, so 1000000 is equivalent
    # to one second. Note that a negative number disables the slow log, while
    # a value of zero forces the logging of every command.
    # slowlog-log-slower-than 10000
    slowlog-log-slower-than 0

    # There is no limit to this length. Just be aware that it will consume memory.
    # You can reclaim memory used by the slow log with SLOWLOG RESET.
    #slowlog-max-len 128
    slowlog-max-len 2

上面的慢查询日志的配置,执行下面的例子的结果:
[root@localhost redis-3.2.0]# ./src/redis-server ./redis.conf     
[root@localhost redis-3.2.0]# redis-cli -p 7000
127.0.0.1:7000> set "nao" "name"
OK
127.0.0.1:7000> set "age" "19"
OK
127.0.0.1:7000> slowlog get
1) 1) (integer) 1
   2) (integer) 1464262866
   3) (integer) 8
   4) 1) "set"
      2) "age"
      3) "19"
2) 1) (integer) 0
   2) (integer) 1464262851
   3) (integer) 7
   4) 1) "set"
      2) "nao"
      3) "name"
127.0.0.1:7000> set "number" "3"
OK
127.0.0.1:7000> slowlog get
1) 1) (integer) 3
   2) (integer) 1464262899
   3) (integer) 7
   4) 1) "set"
      2) "number"
      3) "3"
2) 1) (integer) 2
   2) (integer) 1464262869
   3) (integer) 29
   4) 1) "slowlog"
      2) "get"express


    慢查询日志记录保存:bash

//保存了全部慢查询日志的链表
list *slowlog;                  /* SLOWLOG list of commands */
//下一条慢查询日志的id
long long slowlog_entry_id;     /* SLOWLOG current entry ID */
//服务器配置slow_log_slower_than的值
long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */
//服务器配置slowlog_max_len的值
unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */

slowlog_entry_id属性的初始值为0,每当建立一条新的慢查询日志时,这个属性的值就会用做新日志的id值,以后程序会对这个属性的值增一。服务器


slowlog链表保存了服务器中的全部慢查询日志,链表中的每一个结点都保存了一个slowEntry结构,每一个slowlogEntry结构表明一条慢查询日志:ide

/* This structure defines an entry inside the slow log list */
typedef struct slowlogEntry {
	//命令与命令参数
    robj **argv;
	//命令与命令参数的数量
    int argc;
	//惟一标识符
    long long id;       /* Unique entry identifier. */
	//执行命令消耗的时间,以微妙为单位
    long long duration; /* Time spent by the query, in nanoseconds. */
	//命令执行时的时间,格式为UNIX时间戳
    time_t time;        /* Unix time at which the query was executed. */
} slowlogEntry;

总结:
    Redis的慢查询日志功能用于记录执行时间超过指定时长的命令。
    Redis服务器将全部的慢查询日志保存在服务器状态的slowlog链表中,每一个链表结点都包含一个slowlogEntry结构,每一个slowlogEntry结构表明一个慢查询日志。
    打印和删除慢查询日志能够经过遍历slowlog链表来完成。
    slowlog链表的长度就是服务器所保存慢查询日志的数量。
    新的慢查询日志会被添加到slowlog链表的表头,若是日志的数量超过slowlog-max-len选项的值,那么多出来的日志会被删除。优化

相关文章
相关标签/搜索