Redis has a command to delete a single piece of data del but he doesn't have a way to delete multiple pieces of data in bulk, so how do we delete multiple pieces of data in bulk?
The first way
' volume:* ' | Xargs del
Comments:
by: Keys ' Volume ' to match the Key:value entry you want to delete, then Pass the result to Xargs and delete the data
If you want to remove the specified Redis library plus parameters : (-N) It is generally not recommended to use multi-Library storage data in Redis!
' volume:* ' | Xargs del
Such a disadvantage every time to establish a connection, the amount of small words can also be accepted, a large amount of words, efficiency is not.
The second way
" return Redis.call (' del ', Unpack (redis.call (' Keys ', argv[1] )) " ' volume:* '
Comments:
This way, through the built-in LUA interpreter, you can use the EVAL command on LUA scripts
But in this case, the LUA function unpack a problem and reports an error.
" " (Error) ERR error running script (call to f_e177a091510d969af3b388ee986dbe6658df6b57): @user_script: 1: User_script : 1:too Many results to unpack"
The second way after optimization
" Local keys = Redis.call (' Keys ', argv[1]) for I=1, #keys, and do Redis.call (' del ', Unpack (keys, I, Math.min (i+4999, #key s)) End return #keys"'volume:*'
Comments:
In fact, the second way of improvement, one-time unpack too much will be wrong, then simply once 5000, so there will be no problem!
The meaning of this script is to first define an array of keys, which stores all of the pattern matching keys with ' Volume: ', and then for loops, processing 5,000 keys at a time, that is, each del 5,000 key.
Related scripts: http://www.cnblogs.com/luotianshuai/
Note:
The keys operation is prohibited on-line!
Redis is single-threaded, if the amount is large, keys is traversing key, will cause blocking, so that other clients can not connect!
The Third Way
Since redis2.8 began to support the scan command, pattern matching can take the form of batch deletion of a large number of keys
" volume:* " | Xargs-l 5000/work/app/redis/bin/redis-cli-a youpassword-n 0-p 6379 DEL
Results:
" " /work/app/redis/bin/redis-cli-a youpassword-n 0-p 6379--scan--pattern "volume:*" | xargs-l 5000/work/app/redis /bin/redis-cli-a youpassword-n 0-p 6379 DEL (integer) (integer) (integer) (integers) (an) (integer) (integ er) (integer) (integer) 207 "
Reference Xiao Brother blog: https://www.18188.org/articles/2016/04/06/1459930096140.html
Redis "Knowledge points" bulk Delete specified key