您的位置:首页 > 数据库 > Redis

使用shell脚本优雅删除redis的大key

2018-07-18 21:36 1186 查看

本文关于大键(key)的描述参考博客:https://www.geek-share.com/detail/2724939666.html

理解大key

理解redis中的大key,可从两方面的来理解:1、时间复杂度,2、空间复杂度。前者主要表示Redis键的占用内存大小;后者表示Redis集合数据类型(set/hash/list/sorted set)键,所含有的元素个数。内存空间复杂性处理耗时都非常小,测试 del 200MB String键耗时约1毫秒,而删除一个含有1kw个字段的Hash键,却会阻塞Redis进程数十秒。

直接删除大Key的风险

DEL命令在删除单个集合类型的Key时,命令的时间复杂度是O(M),其中M是集合类型Key包含的元素个数。

DEL key
Time complexity: O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).

redis是单线程处理的,单个耗时过大命令,会导致阻塞其它命令,容易引起应用程序雪崩或redis集群发生故障切换。所以避免在生产环境中使用耗时过大的命令。

scan命令

关于scan的理解请看:https://blog.csdn.net/still_fantasy/article/details/80963550

为避免删除大key带来的阻塞风险,在下面的脚本例子中会先使用sscan命令遍历set集合中一定数量成员,再使用del命令将这些成员数删除。从而达到将删除一个大key化整为零,分为一小步一小步删除,从而达到避免redis服务器长时间阻塞的目的。

shell脚本

以集合set为例子:

[code]function delSet()
{
key=$3
rs=`redis-cli -p $1 -a $2 exists $key`
scannum=100
#echo $rs
if [ `echo $rs | awk -v tem="0" '{print($1>tem)? "1":"0"}'` -eq "0" ] ; then
echo "redis doesn't exist the key $key"
else
cursor="1"
members=`redis-cli -p $1 -a $2 sscan $key 0 count $scannum`
while [ `echo $cursor | awk -v tem="0" '{print($1>tem)? "1":"0"}'` -eq "1" ] ; do
OLD_IFS="$IFS"
IFS=" "
array=($members)
IFS="$OLD_IFS"
count=1
delkeys=""
for var in ${array[@]}
do
if [ $count -eq 1 ] ; then
cursor=$var
count=$((count+1))
else
#rs=`redis-cli -p $1 -a $2 SREM $key $var`
#echo "del:$var"
delkeys=$delkeys$var" "
fi
done
rs=`redis-cli -p $1 -a $2 SREM $key $delkeys`
if [ `echo $cursor | awk -v tem="0" '{print($1>tem)? "1":"0"}'` -eq "1" ];then
members=`redis-cli -p $1 -a $2 sscan $key $cursor count $scannum`
fi
done
fi
}
key="setkey"
echo "start remove the key $key"
delSet $1 $2 $key
rs=`redis-cli -p $1 -a $2 del $key`
echo "remove the key $key successfully"

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐