G
G
Gregory Kaplan2017-07-12 03:56:49
PHP
Gregory Kaplan, 2017-07-12 03:56:49

What is the safest way to remove more than 2KK keys from Redis?

Hello! Tell me, it's very necessary.
Let's say there is a database, you need to delete data by prefix (standard).

$keys = $this->redis->keys($this->_prefix . '*');
$this->redis->del($keys);

For small volumes - works well. But what about a large amount of data? Here the KEYS method is not suitable either. has O(N) complexity. Thanks for your options!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrey Shatokhin, 2017-07-12
@hanuka

Since 3.0, it is recommended to use tags for this. Keys hash tags
In your case - they should be used instead of a prefix.
Key names should look something like this - {user1000}.following, where user1000 is the tag.
KEYS by tag will not check all keys in the database, i.e. the complexity will be orders of magnitude less than O(N)
And as a bonus - it will be compatible with redis-cluster

S
Stalker_RED, 2017-07-12
@Stalker_RED

redis-cli KEYS "prefix:*" | xargs redis-cli DEL, For example?

P
Philipp, 2017-07-12
@zoonman

eval "for _,k in ipairs(redis.call('keys','key:*:pattern')) do redis.call('del',k) end" 0

Replace key:*:patternwith your template and feed redis-cli.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question