Sort sets by number of items in Redis

I have a Redis database with many sets, all of which are identified using a common key template, let them say "myset:".

Is there a way from the command line client to sort all my sets by the number of elements they contain and return this information? As far as I know, the SORT command accepts only single keys.

I know that I can easily do this using a programming language, but I prefer to be able to do this without having to install any driver, programming environment, etc. on the server.

Thank you for your help.

+4
source share
2 answers

Caution: The approach in this answer is not intended as a general solution - remember that using keys not recommended in production setup.

So, here is a solution that will output the name of the set, followed by the length (power), sorted by power.

 # Capture the names of the keys (sets) KEYS=$(redis-cli keys 'myset:*') # Paste each line from the key names with the output of `redis-cli scard key` # and sort on the second key - the size - in reverse paste <(echo "$KEYS") <(echo "$KEYS" | sed 's/^/scard /' | redis-cli) | sort -k2 -r -n 

Note the use of the paste command above. I am counting on redis-cli to send me the results in order, and I'm sure he will. Thus, paste will take one name from $KEYS and one value from redis output and output them on the same line.

+3
source

No, there is no simple trick for this.

Redis is a store, not a database management system. It does not support query language. If you need some data to retrieve, you need to provide access paths in advance and create the data structure accordingly.

For example, in your example, you could support zset when adding / removing elements from sets of interest to you. In this zset, the value will be the set key, and the estimate will be the number of the set.

Retrieving zset content by rank will give you a set sorted by power.

If you haven't planned this access path and still need data, you will have no choice but to use a programming language. If you cannot install any Redis driver, you can work with the Redis dump file (which should be generated by the BGSAVE command), load this file into another block and use the following package from Sripathi Krishnan to analyze it and calculate the statistics that you required.

https://github.com/sripathikrishnan/redis-rdb-tools

+5
source

Source: https://habr.com/ru/post/1433309/


All Articles