且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何删除Redis集群中匹配模式的键

更新时间:2023-02-22 07:45:14

答案问题 尝试在单个 DEL 中删除多个键.但是,匹配给定模式的键可能不会位于同一个槽中,如果这些键不属于同一个槽,Redis 集群不支持多键命令.这就是您收到错误消息的原因.

Answers for that question try to remove multiple keys in a single DEL. However, keys matching the given pattern might NOT locate in the same slot, and Redis Cluster DOES NOT support multiple-key command if these keys don't belong to the same slot. That's why you get the error message.

为了解决这个问题,你需要一一DEL这些键:

In order to fix this problem, you need to DEL these keys one-by-one:

redis-cli --scan --pattern "foo*" |xargs -L 1 redis-cli del

xargs 命令的 -L 选项指定要删除的键数.您需要将此选项指定为 1.

The -L option for xargs command specifies the number of keys to delete. You need to specify this option as 1.

为了删除与模式匹配的所有键,您还需要为集群中的每个主节点运行上述命令.

In order to remove all keys matching the pattern, you also need to run the above command for every master nodes in your cluster.

注意

  1. 使用这个命令,你必须一个一个地删除这些键,这可能会很慢.您需要考虑重新设计您的数据库,并使用 hash-tags 使匹配模式的键属于同一个槽.这样您就可以在一个 DEL 中删除这些键.

  1. With this command, you have to delete these keys one-by-one, and that might be very slow. You need to consider re-designing your database, and use hash-tags to make keys matching the pattern belong to the same slot. So that you can remove these keys in a single DEL.

SCANKEYS 命令都是低效的,尤其是 KEYS 不应该在生产中使用.您需要考虑为这些键建立索引.

Either SCAN or KEYS command are inefficient, especially, KEYS should not be used in production. You need to consider building an index for these keys.