What is the best way to do sum / count / groupby in Redis?

For a Redis list (or set / zset / hset)

['5', '5', '5', '5', '4', '3', '3', '3', '2', '2', '2', '2', '1', '1', '1'] 

What is the best way to install it since sql did

 select count(key), sum(key) from table group by key; 

Wishing a customer a loop is not the only way .......

+6
source share
1 answer

It is best to store the amount as a separate key and update whenever you add / remove a value from your set / hash / zset.

In Redis, you should try to model your data according to your access patterns. If you need an amount at runtime, pre-calculate and save the amount. If you need the amount hourly / daily / monthly, you will need to create the keys corresponding to them.

+16
source

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


All Articles