Redis 慢查詢?nèi)罩?/h1>

2018-08-02 11:55 更新

慢查詢?nèi)罩?/h2>

慢查詢?nèi)罩臼?Redis 提供的一個(gè)用于觀察系統(tǒng)性能的功能,這個(gè)功能的實(shí)現(xiàn)非常簡(jiǎn)單,這里我們也簡(jiǎn)單地講解一下。

本章先介紹和慢查詢功能相關(guān)的數(shù)據(jù)結(jié)構(gòu)和變量,然后介紹 Redis 是如何記錄命令的執(zhí)行時(shí)間,以及如何為執(zhí)行超過(guò)限制事件的命令記錄慢查詢?nèi)罩镜摹?/p>

相關(guān)數(shù)據(jù)結(jié)構(gòu)

每條慢查詢?nèi)罩径家砸粋€(gè) slowlog.h/slowlogEntry 結(jié)構(gòu)定義:

typedef struct slowlogEntry {

    // 命令參數(shù)
    robj **argv;

    // 命令參數(shù)數(shù)量
    int argc;

    // 唯一標(biāo)識(shí)符
    long long id;       /* Unique entry identifier. */

    // 執(zhí)行命令消耗的時(shí)間,以納秒(1 / 1,000,000,000 秒)為單位
    long long duration; /* Time spent by the query, in nanoseconds. */

    // 命令執(zhí)行時(shí)的時(shí)間
    time_t time;        /* Unix time at which the query was executed. */

} slowlogEntry;

記錄服務(wù)器狀態(tài)的 redis.h/redisServer 結(jié)構(gòu)里保存了幾個(gè)和慢查詢有關(guān)的屬性:

struct redisServer {

    // ... other fields

    // 保存慢查詢?nèi)罩镜逆湵?    list *slowlog;                  /* SLOWLOG list of commands */

    // 慢查詢?nèi)罩镜漠?dāng)前 id 值
    long long slowlog_entry_id;     /* SLOWLOG current entry ID */

    // 慢查詢時(shí)間限制
    long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */

    // 慢查詢?nèi)罩镜淖畲髼l目數(shù)量
    unsigned long slowlog_max_len;     /* SLOWLOG max number of items logged */

    // ... other fields
};

slowlog 屬性是一個(gè)鏈表,鏈表里的每個(gè)節(jié)點(diǎn)保存了一個(gè)慢查詢?nèi)罩窘Y(jié)構(gòu),所有日志按添加時(shí)間從新到舊排序,新的日志在鏈表的左端,舊的日志在鏈表的右端。

slowlog_entry_id 在創(chuàng)建每條新的慢查詢?nèi)罩緯r(shí)增一,用于產(chǎn)生慢查詢?nèi)罩镜?ID (這個(gè) ID 在執(zhí)行 SLOWLOG RESET 之后會(huì)被重置)。

slowlog_log_slower_than 是用戶指定的命令執(zhí)行時(shí)間上限,執(zhí)行時(shí)間大于等于這個(gè)值的命令會(huì)被慢查詢?nèi)罩居涗洝?/p>

slowlog_max_len 慢查詢?nèi)罩镜淖畲髷?shù)量,當(dāng)日志數(shù)量等于這個(gè)值時(shí),添加一條新日志會(huì)造成最舊的一條日志被刪除。

下圖展示了一個(gè) slowlog 屬性的實(shí)例:

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)