Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Three ways to realize batch deletion by redis

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report