python2.7
pip install redis
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.ar**) != 4:
# print 'usage: python ', sys.ar**[0], ' host port password '
# exit(1)
db_host = 填寫redis位址'
db_port =埠
db_password = '密碼'
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", ""))
說明該命令支援查詢redis主從版本和集群版本的大key ,預設大key的閾值為10240。string型別的value大於10240的是大key,list長度大於10240認為是大key,hash field的數目大於10240認為是大key。另外預設該指令碼每次搜尋1000個key,對業務的影響比較低,不過最好在業務低峰期進行操作,避免scan命令對業務的影響。
批量刪除Redis資料庫中的Key
批量刪除key redis 中有刪除單個 key 的指令 del,但好像沒有批量刪除 key 的指令,不過我們可以借助 linux 的 xargs 指令來完成這個動作 1 2 3 redis cli keys xargs redis cli del 如果redis cli沒有設定成系統變數,需要指定...
批量刪除Redis資料庫中的Key
linux 的 xargs 指令 redis cli keys xargs redis cli del 如果redis cli沒有設定成系統變數,需要指定redis cli的完整路徑 如 opt redis redis cli keys xargs opt redis redis cli del 如...
批量刪除Redis資料庫中的Key
linux 的 xargs 指令 redis cli keys xargs redis cli del 如果redis cli沒有設定成系統變數,需要指定redis cli的完整路徑 如 opt redis redis cli keys xargs opt redis redis cli del 如...