What is the best way to do sum/count/groupby in Redis?
13,173
The best way is to store the sum as a separate key, and to 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 the sum at runtime, pre-compute and store the sum. If you want sum on a hourly/daily/monthly basis, you will have to create appropriately named keys.
Related videos on Youtube
Comments
-
fanlix over 1 year
For a Redis list (or set/zset/hset)
['5', '5', '5', '5', '4', '3', '3', '3', '2', '2', '2', '2', '1', '1', '1']
What's the best way to stat it, as sql did
select count(key), sum(key) from table group by key;
Wishing client loop is not the only way.......
-
fanlix over 11 yearsPre-Compute is a good idea. But for old-systems already have a lot datas, or for new-demand, we can't pre-compute.
-
Linus Thiel over 11 yearsIn that case, the only way to do this on the server (redis) side is to use the 2.6 release candidate and use lua scripting.
-
Mahn over 11 years+1 for modeling data according to accesing patterns, that's exactly the mindset one should use when working with redis.
-
Reinsbrain over 7 yearswill this solution (maintaining a separate sum key) not suffer race conditions when there are multiple clients?