Redis删除大Key

原文链接:https://www.dubby.cn/detail.html?id=9112

这里说的大key是指包含很多元素的set,sorted set,list和hash。

删除操作,我们一般想到有2种,delexpire

DEL

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

如果要删除的key是一个集合,包含了很多元素,那么DEL时的耗时和元素个数成正比,所以如果直接DEL,会很慢。

EXPIRE

Note that calling EXPIRE/PEXPIRE with a non-positive timeout or EXPIREAT/PEXPIREAT with a time in the past will result in the key being deleted rather than expired (accordingly, the emitted key event will be del, not expired).

想着expire会不会可以不是直接删除,可惜官网的描述让我心灰意冷,如果expire后指定的timeout不是正数,也就是<=0,那其实就是DEL

一点一点删

我们知道Redis的工作线程是单线程的,如果一个command堵塞了,那所有请求都会超时,这时候,一些骚操作也许可以帮助你。

其实如果想删除key,可以分解成2个目的,1:不想让其他人访问到这个key,2:释放空间。

那其实我们可以分解成两步,先用RENAME把原先的key rename成另一个key,比如:

RENAME userInfo:123 "deleteKey:userInfo:123"

然后可以慢慢去删"deleteKey:userInfo:123",如果是set,那么可以用SREM慢慢删,最后再用DEL彻底删掉。

这里可以搞个task去SCAN deleteKey:*,然后慢慢删除。

UNLINK

Redis 4.0.0提供了一个更加方便的命令

Available since 4.0.0.

Time complexity: O(1) for each key removed regardless of its size. Then the command does O(N) work in a different thread in order to reclaim memory, where N is the number of allocations the deleted objects where composed of.

UNLINK其实是直接返回,然后在后台线程慢慢删除。

如果你的Redis版本>=4.0.0,那么强烈建议使用UNLINK来删除。

删除耗时测试结果

单位:微秒

Set个数

DEL

EXPIRE

UNLINK

1

90

97

75

10

79

67

100

100

51

49

47

1000

303

296

49

10000

2773

2592

52

100000

31210

33157

51

1000000

549388

501536

62

1package main 2 3import ( 4 "github.com/go-redis/redis" 5 "fmt" 6 "time" 7) 8 9func main() { 10 client := redis.NewClient(&redis.Options{ 11 Addr: "localhost:6379", 12 Password: "", 13 DB: 0, 14 ReadTimeout: 1000 * 1000 * 1000 * 60 * 60 * 24, 15 WriteTimeout: 1000 * 1000 * 1000 * 60 * 60 * 24, 16 }) 17 18 maxLength := int64(10000 * 100) 19 20 for n := int64(1); n <= maxLength; n *= 10 { 21 fmt.Println("Set个数", n) 22 TestDelBigSet(client, n) 23 TestExpireBigSet(client, n) 24 TestUnlinkBigSet(client, n) 25 fmt.Println() 26 } 27} 28 29func TestDelBigSet(client *redis.Client, count int64) { 30 redisKey := fmt.Sprintf("%s%d", "del:", time.Now().Nanosecond()) 31 32 for n := int64(0); n < count; n++ { 33 err := client.SAdd(redisKey, fmt.Sprintf("%d", n)).Err() 34 if err != nil { 35 panic(err) 36 } 37 } 38 39 startTime := CurrentTimestampInMicroSecond() 40 client.Del(redisKey) 41 endTime := CurrentTimestampInMicroSecond() 42 43 fmt.Println("Del", endTime-startTime) 44} 45 46func TestUnlinkBigSet(client *redis.Client, count int64) { 47 redisKey := fmt.Sprintf("%s%d", "unlink:", time.Now().Nanosecond()) 48 49 for n := int64(0); n < count; n++ { 50 err := client.SAdd(redisKey, fmt.Sprintf("%d", n)).Err() 51 if err != nil { 52 panic(err) 53 } 54 } 55 startTime := CurrentTimestampInMicroSecond() 56 client.Unlink(redisKey) 57 endTime := CurrentTimestampInMicroSecond() 58 59 fmt.Println("Unlink", endTime-startTime) 60} 61 62func TestExpireBigSet(client *redis.Client, count int64) { 63 redisKey := fmt.Sprintf("%s%d", "expire:", time.Now().Nanosecond()) 64 65 for n := int64(0); n < count; n++ { 66 err := client.SAdd(redisKey, fmt.Sprintf("%d", n)).Err() 67 if err != nil { 68 panic(err) 69 } 70 } 71 startTime := CurrentTimestampInMicroSecond() 72 client.Expire(redisKey, 0) 73 endTime := CurrentTimestampInMicroSecond() 74 75 fmt.Println("Expire", endTime-startTime) 76} 77 78func CurrentTimestampInMicroSecond() int64 { 79 return time.Now().UnixNano() / 1000 80}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解

Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593

Redis删除大Key - HelloWorld