Redis: Get all score available for a sorted set

database, nosql, redis

Solution

One way to address this problem is to use server-side Lua scripting.

Consider the following script:

local res = {}
local result = {}
local tmp = redis.call( 'zrange', KEYS[1], 0, -1, 'withscores' )
for i=1,#tmp,2 do
   res[tmp[i+1]]=true
end
for k,_ in pairs(res) do
   table.insert(result,k)
end
return result

You can execute it by using the EVAL command.

It uses the zrange command to extract the content of the zset (with scores), then it builds a set (represented with a table in Lua) to remove redundant scores, and finally build the reply table. So the values of the zset are never sent over the network.

This script has a flaw if the number of items in the zset is really high, because it copies the entire zset in a Lua object (so it takes memory). However, it is easy to alter it to iterate on the zset incrementally (20 items per 20 items). For instance:

local res = {}
local result = {}
local n = redis.call( 'zcard', KEYS[1] )
local i=0
while i<n do
   local tmp = redis.call( 'zrange', KEYS[1], i, i+20, 'withscores' )
   for j=1,#tmp,2 do
      res[tmp[j+1]]=true
      i = i + 1
   end
end
for k,_ in pairs(res) do
   table.insert(result,k)
end
return result

Please note I am a total newbie in Lua, so there are perhaps more elegant ways to achieve the same thing.

Problem

I need to get all score available for a redis sorted set. ``` redis> ZADD myzset 10 "one" (integer) 1 redis> ZADD myzset 20 "two" (integer) 1 redis> ZADD myzset 30 "three" (integer) 1 ``` Now I want to retrieve all score for myzset, ie. 10,20,30.

Original source