Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • ⛸️ Redis Implementation
    • Part 1 - 数据结构与对象

      • Chapter 2 - 简单动态字符串
      • Chapter 3 - 链表
      • Chapter 4 - 字典
      • Chapter 5 - 跳跃表
      • Chapter 6 - 整数集合
      • Chapter 7 - 压缩列表
      • Chapter 8 - 对象
    • Part 2 - 单机数据库的实现

      • Chapter 9 - 数据库
      • Chapter 10 - RDB 持久化
      • Chapter 11 - AOF 持久化
      • Chapter 12 - 事件
      • Chapter 13 - 客户端
      • Chapter 14 - 服务器
    • Part 3 - 多机数据库的实现

      • Chapter 15 - 复制
      • Chapter 16 - Sentinel
      • Chapter 17 - 集群
    • Part 4 - 独立功能的实现

      • Chapter 18 - 发布与订阅
      • Chapter 19 - 事务
      • Chapter 21 - 排序
      • Chapter 22 - 二进制位数组
      • Chapter 23 - 慢查询日志

Chapter 23 - 慢查询日志

Created by : Mr Dk.

2020 / 06 / 13 14:08

Nanjing, Jiangsu, China


Redis 的慢查询日志功能用于记录执行时间超过给定时长的命令请求。用户可以通过慢查询日志来监视和优化查询速度。

  • slowlog-log-slower-than 指定执行时间超过该阈值的命令会被记录
  • slowlog-max-len 指定服务器最多保存多少条慢查询日志

与该功能相关的数据结构:

struct redisServer {
    // ...
    long long slowlog_entry_id; // 下一条慢查询日志 id
    list *slowlog; // 保存了所有慢查询日志的链表

    long long slowlog_log_slower_than;
    unsigned long slowlog_max_len;
    // ...
}

其中,第一条慢查询日志的 id 为 0,之后每创建一条日志 id + 1。慢查询日志的链表结构定义:

typedef struct slowlogEntry {
    long long id;
    time_t time; // 命令执行时的时间
    long long duration; // 执行命令消耗的时间
    robj **argv; // 命令与命令参数
    int argc; // 命令与命令参数的数量
} slowlogEntry;

每次执行命令前后,程序都会记录微秒格式的 UNIX 时间戳,两个时间戳的差就是服务器执行命令所耗费的时长。如果超出了设定阈值,就产生一条新的慢查询日志加入链表;如果链表长度超出阈值,则删除最早的慢查询日志。

Edit this page on GitHub
Prev
Chapter 22 - 二进制位数组