Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis: Sum of SCORES in Sorted Set

What's the best way to get the sum of SCORES in a Redis sorted set?

like image 960
ma11hew28 Avatar asked Jan 30 '11 23:01

ma11hew28


4 Answers

The only option I think is iterating the sorted set and computing the sum client side.

like image 130
antirez Avatar answered Oct 05 '22 19:10

antirez


Available since Redis v2.6 is the most awesome ability to execute Lua scripts on the Redis server. This renders the challenge of summing up a Sorted Set's scores to trivial:

local sum=0
local z=redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES')

for i=2, #z, 2 do 
    sum=sum+z[i]
end

return sum

Runtime example:

~$ redis-cli zadd z 1 a 2 b 3 c 4 d 5 e
(integer) 5
~$ redis-cli eval "local sum=0 local z=redis.call('ZRANGE', KEYS[1], 0, -1, 'WITHSCORES') for i=2, #z, 2 do sum=sum+z[i] end return sum" 1 z
(integer) 15
like image 30
Itamar Haber Avatar answered Oct 05 '22 18:10

Itamar Haber


If the sets are small, and you don't need killer performance, I would just iterate (zrange/zrangebyscore) and sum the values client side.

If, on the other hand, you are talking about many thousands - millions of items, you can always keep a reference set with running totals for each user and increment/decrement them as the gifts are sent.

So when you do your ZINCR 123:gifts 1 "3|345", you could do a seperate ZINCR command, which could be something like this:

ZINCR received-gifts 1 <user_id>

Then, to get the # of gifts received for a given user, you just need to run a ZSCORE:

ZSCORE received-gifts <user_id>
like image 40
mkgrunder Avatar answered Oct 05 '22 19:10

mkgrunder


Here is a little lua script that maintains the zset score total as you go, in a counter with key postfixed with '.ss'. You can use it instead of ZADD.

local delta = 0
for i=1,#ARGV,2 do
    local oldScore = redis.call('zscore', KEYS[1], ARGV[i+1])
    if oldScore == false then
        oldScore = 0
    end
    delta = delta - oldScore + ARGV[i]
end
local val = redis.call('zadd', KEYS[1], unpack(ARGV))
redis.call('INCRBY', KEYS[1]..'.ss', delta)
like image 42
Patrick Avatar answered Oct 05 '22 18:10

Patrick