Redis查询大key

Posted HappyTeemo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis查询大key相关的知识,希望对你有一定的参考价值。

原文

安装

wget "https://pypi.python.org/packages/68/44/5efe9e98ad83ef5b742ce62a15bea609ed5a0d1caf35b79257ddb324031a/redis-2.10.5.tar.gz#md5=3b26c2b9703b4b56b30a1ad508e31083"

tar -xvf redis-2.10.5.tar.gz
cd redis-2.10.5
sudo python setup.py install

python 代码

import sys
import redis
def check_big_key(r, k):
  bigKey = False
  length = 0 
  try:
    type = r.type(k)
    if type == "string":
      length = r.strlen(k)
    elif type == "hash":
      length = r.hlen(k)
    elif type == "list":
      length = r.llen(k)
    elif type == "set":
      length = r.scard(k)
    elif type == "zset":
      length = r.zcard(k)
  except:
    return
  if length > 10240:
    bigKey = True
  if bigKey :
    print db,k,type,length
def find_big_key_normal(db_host, db_port, db_password, db_num):
  r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, db=db_num)
  for k in r.scan_iter(count=1000):
    check_big_key(r, k)
def find_big_key_sharding(db_host, db_port, db_password, db_num, nodecount):
  r = redis.StrictRedis(host=db_host, port=db_port, password=db_password, db=db_num)
  cursor = 0
  for node in range(0, nodecount) :
    while True:
      iscan = r.execute_command("iscan",str(node), str(cursor), "count", "1000")
      for k in iscan[1]:
        check_big_key(r, k)
      cursor = iscan[0]
      print cursor, db, node, len(iscan[1])
      if cursor == "0":
        break;
if __name__ == \'__main__\':
  if len(sys.argv) != 4:
     print \'Usage: python \', sys.argv[0], \' host port password \'
     exit(1)
  db_host = sys.argv[1]
  db_port = sys.argv[2]
  db_password = sys.argv[3]
  r = redis.StrictRedis(host=db_host, port=int(db_port), password=db_password)
  nodecount = r.info()[\'nodecount\']
  keyspace_info = r.info("keyspace")
  for db in keyspace_info:
    print \'check \', db, \' \', keyspace_info[db]
    if nodecount > 1:
      find_big_key_sharding(db_host, db_port, db_password, db.replace("db",""), nodecount)
    else:
      find_big_key_normal(db_host, db_port, db_password, db.replace("db", ""))

参考以下命令,查找较大的Key。

python find_bigkey [$DB_Host] [$DB_Port] [$DB_Pass]

说明

  • [$DB_Host]:Redis数据库的主机名或IP地址。
  • [$DB_Port]:Redis数据库的端口号,默认为“6379”。
  • [$DB_Pass]:Redis数据库的密码。
  • 该命令支持查找Redis主从版本和Redis集群版本中的大Key。此处的大Key阈值为“10240”,比如:String类型的Value大于10240的是大Key,List长度大于10240认为是大Key,Hash Field的数目大于10240认为是大Key。
  • 该脚本默认每次搜索1000个Key,对业务的影响比较低,不过最好在业务低峰期进行操作,避免scan命令对业务造成影响。

以上是关于Redis查询大key的主要内容,如果未能解决你的问题,请参考以下文章

Redis第五篇——大key和热key问题

Redis中遍历大数据量的key:keys与scan命令

Redis中遍历大数据量的key:keys与scan命令

redis怎么查询key和删除缓存

redis key命名规范推荐

redishaskey和getkey效率