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.

Share:
13,173

Related videos on Youtube

fanlix
Author by

fanlix

my program has not met stack overflow for a long time.

Updated on August 03, 2022

Comments

  • fanlix
    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
    fanlix over 11 years
    Pre-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
    Linus Thiel over 11 years
    In 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
    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
    Reinsbrain over 7 years
    will this solution (maintaining a separate sum key) not suffer race conditions when there are multiple clients?