Three ways to realize batch deletion by redis
Redis implements batch deletion:
1. Visit the redis root directory cd / usr/local/redis-2.8.19
2. Log in to redis:redis-cli-h 127.0.0.1-p 6379 (where 127.0.0.1 can be written as the IP address of the server, and 6379 is the port number)
3. View all key values: keys *
4. Delete the value of specified index: del key
5. Clear the data of the entire Redis server: flushall
6. Clear all key:flushdb in the current library
[the first way]:
The following is a batch deletion of all redis data starting with "key_".
Redis-cli-h (IP address)-p 6379 (port number: 6379) KEYS key_* | xargs redis-cli (- h (IP address)-p 6379 (port number: 6379) del = > [number of results returned after execution]: (integer)
[specific usage of DEL function in the above command]:
DEL key [key...]
Deletes one or more given key.
Key that does not exist will be ignored.
Time complexity:
O (N), N is the number of key deleted.
Delete a key of a single string type with a time complexity of O (1).
Delete a key of a single list, collection, ordered collection, or hash table type with a time complexity of O (M), where M is the number of elements in the above data structure.
Return value:
The number of key deleted.
[examples]:
# deleting a single keyredis > SET name huangzOK redis > DEL name (integer) 1 # failed to delete a non-existent keyredis > EXISTS phone (integer) 0 redis > DEL phone #, no key was deleted (integer) 0 # while deleting multiple keyredis > SET name "redis" OK redis > SET type "key-value store" OK redis > SET website "redis.com" OK redis > DEL name type website (integer) 3
[second way]:
The drawback of the first way: such a disadvantage is to establish a connection every time, small quantity is acceptable, large quantity, inefficient.
With the built-in Lua interpreter, you can use the EVAL command to Lua scripts:
Redis-cli-h (IP address)-p 6379 (port number: 6379)-EVAL "return redis.call ('del', unpack (redis.call (' keys', ARGV [1])" 0 'Volume:*'
[note]: however, in the case of a large amount of processing, there will be problems with the lua function unpack and errors will be reported.
'' (error) ERR Error running script (call to f_e177a091510d969af3b388ee986dbe6658df6b57): @ user_script:1: user_script:1: too many results to unpack'''
[after the second method is optimized]:
[note]: first define an array keys, which stores all the key with 'Volume:' for pattern matching, and then the for loop processes 5000 key at a time, that is, 5000 key at a time del
Redis-cli-h (IP address)-p 6379 (port number: 6379)-- EVAL "local keys = redis.call ('keys', ARGV [1]) for iTunes 1 do redis.call (' del', unpack (keys, I, math.min (iTun4999, # keys) end return # keys" 0 'Volume:*'
[disadvantages of the second method]:
KEYS operation is prohibited online!
Redis is single-threaded, if the volume is large, keys traverses the key, which will cause blocking, so that other clients can not connect!
[third way]:
The scan command has been supported since redis2.8, and pattern matching can take the following form to batch delete a large number of key
Redis-cli-a youpassowrd-n 0-p 6379-- scan-- pattern "Volume:*" | xargs-L 5000 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) 5000 (integer) 207
These are the details of the redis command for batch deletion. Please pay attention to other related articles for more information.