找出Redis数据库中键的最大字符串值大小

时间:2014-11-11 17:01:52

标签: python redis

我需要查看我们的Redis缓存,看看我们最大存储值的大小。我对Python很有经验,或者可以直接使用redis-cli。有没有办法迭代数据库中的所有键,以便我可以检查每个值的大小?

看起来SCAN是迭代键的方式,但我仍在研究如何使用它来获取值的大小并存储最大值。

3 个答案:

答案 0 :(得分:7)

由于你提到redis-cli作为一个选项,它有一个构建它的功能,几乎可以满足你的要求(以及更多)。

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).

以下是夏季输出的示例:

Sampled 343799 keys in the keyspace!
Total key length in bytes is 9556361 (avg len 27.80)

Biggest string found '530f8dc7c7b3b:39:string:87929' has 500 bytes
Biggest list found '530f8d5a17b26:9:list:11211' has 500 items
Biggest set found '530f8da856e1e:75:set:65939' has 500 members
Biggest hash found '530f8d619a0af:86:hash:16911' has 500 fields
Biggest zset found '530f8d4a9ce31:45:zset:315' has 500 members

68559 strings with 17136672 bytes (19.94% of keys, avg size 249.96)
68986 lists with 17326343 items (20.07% of keys, avg size 251.16)
68803 sets with 17236635 members (20.01% of keys, avg size 250.52)
68622 hashs with 17272144 fields (19.96% of keys, avg size 251.70)
68829 zsets with 17241902 members (20.02% of keys, avg size 250.50)

您可以查看完整的输出示例here

答案 1 :(得分:3)

以下是使用redis's built-in scripting capabilities的解决方案:

local longest
local cursor = "0"

repeat
  local ret = redis.call("scan", cursor)
  cursor = ret[1]
  for _, key in ipairs(ret[2]) do
    local length = redis.pcall("strlen", key)
    if type(length) == "number" then
      if longest == nil or length > longest then
        longest = length
      end
    end
  end
until cursor == "0"

return longest

这应该比你提供的Python代码Ben Roberts运行得更快,特别是因为Lua脚本使用STRLEN而不是GET + Python len

答案 2 :(得分:0)

我想我已经使用redis-py library

找出了一个基本想法
import redis
r= redis.StrictRedis(...)
max_len = 0
for k in r.scan_iter():
  try:
    val_len = r.strlen(k)
  except:
    continue
  if val_len > max_len:
    max_len = val_len
print max_len