阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Redis的慢查询日志

112次阅读
没有评论

共计 3379 个字符,预计需要花费 9 分钟才能阅读完成。

慢查询日志

记录最新的 N 条执行时间超过 M 毫秒的命令。慢查询日志保存在内存中,而不是文件中,这保证了慢查询日志的效率。

慢查询日志的条目定义
/* This structure defines an entry inside the slow log list */
/*
 * 慢查询日志
 */
typedef struct slowlogEntry {
 
    // 命令与命令参数
    robj **argv;
 
    // 命令与命令参数的数量
    int argc;
 
    // 唯一标识符
    long long id;       /* Unique entry identifier. */
 
    // 执行命令消耗的时间,以微秒为单位
    // 注释里说的 nanoseconds 是错误的
    long long duration; /* Time spent by the query, in nanoseconds. */
 
    // 命令执行时的时间,格式为 UNIX 时间戳
    time_t time;        /* Unix time at which the query was executed. */
 
} slowlogEntry;
 
服务器和慢查询有关的定义
    /* slowlog */
 
    // 保存了所有慢查询日志的链表
    list *slowlog;                  /* SLOWLOG list of commands */
 
    // 下一条慢查询日志的 ID
    long long slowlog_entry_id;     /* SLOWLOG current entry ID */
 
    // 服务器配置 slowlog-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 */
 
服务器的慢查询存储在一个 list 中,list 中的每一项都是一条慢查询日志,较新的日志总是保存在队首。慢查询日志中保存命令的执行参数和执行时间,如果超出系统限制,参数和日志可能被截断。
 
慢查询支持的客户端操作
GET:获取某条或者全部慢查询日志
RESET:清空慢查询日志
LEN:慢查询日志的数量
 
 
慢查询日志的应用
Redis 每执行一条命令,就会记录命令的开始时间和结束时间,由此计算命令的执行时间。并发命令以及命令的执行时间传递给 slowlogPushEntryIfNeeded,由 slowlogPushEntryIfNeeded 决定是否生成慢查询日志。
/* Call() is the core of Redis execution of a command */
// 调用命令的实现函数,执行命令
void call(redisClient *c, int flags)
{
    // 获取命令的执行时间
    /* Log the command into the Slow log if needed, and populate the
     * per-command statistics that we show in INFO commandstats. */
    // 如果有需要,将命令放到 SLOWLOG 里面
    if (flags & REDIS_CALL_SLOWLOG && c->cmd->proc != execCommand)
        slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
}
 
 
slowlogPushEntryIfNeeded 的实现
判断系统标志位,并把慢查询日志加入到服务器的慢查询链表中
/* Push a new entry into the slow log.
 *
 * 如果参数 duration 超过服务器设置的上限时间,
 * 那么将一个新条目以 FIFO 顺序推入到慢查询日志中。
 *
 * This function will make sure to trim the slow log accordingly to the
 * configured max length. 
 *
 * 根据服务器设置的最大日志长度,可能会对日志进行截断(trim)
 */
void slowlogPushEntryIfNeeded(robj **argv, int argc, long long duration) {
 
    // 慢查询功能未开启,直接返回
    if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */
 
    // 如果执行时间超过服务器设置的上限,那么将命令添加到慢查询日志
    if (duration >= server.slowlog_log_slower_than)
        // 新日志添加到链表表头
        listAddNodeHead(server.slowlog,slowlogCreateEntry(argv,argc,duration));
 
    /* Remove old entries if needed. */
    // 如果日志数量过多,那么进行删除
    while (listLength(server.slowlog) > server.slowlog_max_len)
        listDelNode(server.slowlog,listLast(server.slowlog));
}

下面关于 Redis 的文章您也可能喜欢,不妨参考下:

Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm

Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm

Redis 集群搭建与简单使用  http://www.linuxidc.com/Linux/2017-03/142210.htm

CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm

Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Redis 单机 & 集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm

CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm

Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm

Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm

Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm

Redis 热迁移实战总结  http://www.linuxidc.com/Linux/2017-02/141083.htm

Redis3.0 配置文件详解  http://www.linuxidc.com/Linux/2017-03/141369.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-12/149441.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-22发表,共计3379字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中