首發於 https://segmentfault.com/a/11...git
注意如下全部試驗基於redis 5.0.3版本github
redis-cli 提供一個bigkeys參數,能夠掃描redis中的大keyredis
--bigkeys Sample Redis keys looking for big keys.
執行以後輸出以下所示:sql
bogon:sqlite didi$ redis-cli --bigkeys # Scanning the entire keyspace to find biggest keys as well as # average sizes per key type. You can use -i 0.1 to sleep 0.1 sec # per 100 SCAN commands (not usually needed). [00.00%] Biggest zset found so far 'testzset' with 129 members [00.00%] Biggest hash found so far 'h2' with 513 fields [00.00%] Biggest set found so far 'si1' with 5 members [00.00%] Biggest hash found so far 'h4' with 514 fields [00.00%] Biggest string found so far 'key' with 9 bytes -------- summary ------- Sampled 9 keys in the keyspace! Total key length in bytes is 27 (avg len 3.00) Biggest string found 'key' has 9 bytes Biggest set found 'si1' has 5 members Biggest hash found 'h4' has 514 fields Biggest zset found 'testzset' has 129 members 1 strings with 9 bytes (11.11% of keys, avg size 9.00) 0 lists with 0 items (00.00% of keys, avg size 0.00) 2 sets with 8 members (22.22% of keys, avg size 4.00) 4 hashs with 1541 fields (44.44% of keys, avg size 385.25) 2 zsets with 132 members (22.22% of keys, avg size 66.00) 0 streams with 0 entries (00.00% of keys, avg size 0.00)
原理比較簡單,使用scan命令去遍歷全部的鍵,對每一個鍵根據其類型執行"STRLEN","LLEN","SCARD","HLEN","ZCARD"這些命令獲取其長度或者元素個數。segmentfault
該方法有兩個缺點:spa
1.線上使用:雖然scan命令經過遊標遍歷建空間而且在生產上能夠經過對從服務執行該命令,但畢竟是一個線上操做debug
2.set,zset,list以及hash類型只能獲取有多少個元素。但其實元素多的不必定佔用空間大code
因此有沒有一種方法對線上沒有影響,而且能直接以topn的形式輸出每一個鍵佔用的空間大小呢?sqlite
咱們能夠經過讀取rdb文件的方式來試驗一下,首先看看rdb文件的格式圖片
rdb是一種二進制文件格式,咱們首先看看rdb文件的總體結構
首先是一個魔數,REDIS0009(redis5.0.3版本)。而後是一些附加屬性字段,接着是db_num(0-15),而後是db和expire的字典大小(db和過時時間在Redis中是兩個獨立的hash table),接着是一個個key-value pairs,而後是一個EOF結束標誌(0xFF),最後是8字節的checksum
Redis中定義了一些opcode(1字節),去標記opcode以後保存的是什麼類型的數據。以下圖所示
opcode 252標記一個過時時間,248和249分別表示lru或者lfu,接着是value_type,標記值的類型,接着就是一個個key和vlaue.咱們看下value_type和redis中數據類型的對應關係
value_type就是值類型這一列,括號中的數字就是保存到rdb文件中時的實際使用數字
知道了rdb的保存格式,咱們能夠寫代碼解析rdb文件,經過value_type去獲取每一個value的大小
代碼地址以下:
https://github.com/erpeng/god...
下載以後在將rdb文件拷貝到項目根目錄,按以下方式執行
bogon:godis-cli-bigkey didi$ go run godis-cli-bigkey.go -h -debug open debug mode //debug模式,輸出詳細key/value信息 -topn int output topn keys (default 100)//默認輸出top100的大key -totallen get total len (key and meta) or only value len (default true)//若是該選項設置爲false,只輸出rdb文件中value實際佔用的大小 //默認爲true,輸出key、value和全部該key,value保存時使用的元數據總和 exit status 2
咱們具體執行一下,輸出以下:
bogon:godis-cli-bigkey didi$ go run godis-cli-bigkey.go Rdb Version:0009 key:k1,valueSize:9,valueType:0,expireTime:1549533396795,lfu:0,lru:0 key:key,valueSize:9,valueType:0,expireTime:0,lfu:0,lru:0 key:ss1,valueSize:14,valueType:2,expireTime:0,lfu:0,lru:0 key:si1,valueSize:23,valueType:11,expireTime:0,lfu:0,lru:0 key:l1,valueSize:28,valueType:14,expireTime:1549537004535,lfu:0,lru:0 key:h1,valueSize:33,valueType:13,expireTime:0,lfu:0,lru:0 key:z1,valueSize:67,valueType:12,expireTime:0,lfu:0,lru:0 key:testzset,valueSize:1303,valueType:5,expireTime:0,lfu:0,lru:0 key:h3,valueSize:8845,valueType:13,expireTime:0,lfu:0,lru:0 key:h2,valueSize:11680,valueType:4,expireTime:0,lfu:0,lru:0 key:h4,valueSize:11703,valueType:4,expireTime:0,lfu:0,lru:0