问题
I have a number of sorted sets that are used as secondary indexes on my system and user queries could hit a number of them.
ZADD scoreSet 1 "fred"
ZADD scoreSet 5 "bob"
ZADD scoreSet 2 "spodrick"
ZADD ageSet 25 "fred"
ZADD ageSet 29 "bob"
ZADD ageSet 38 "spodrick"
To use these indexes to get all users under 30 with a score >2
ZRANGEBYSCORE scoreSet (2 +inf
(store these in my application code)
ZRANGEBYSCORE ageSet -inf (30
(store these in my application code)
(Perform Set intersection in my application code)
but this means I have copied all the data from redis to my app server to perform the intersections, is there a more efficient way to do this where I am not transporting all the matching ranges across the network and instead doing the intersections in Rediss?
What I would like is
ZRANGEBYSCORESTORE tempSet1 scoreSet (2 +inf
ZRANGEBYSCORESTORE tempSet2 ageSet -inf (30
SINTER tempSet1 tempSet2
Where ZRANGEBYSCORESTORE performs the ZRANGEBYSCORE operation and stores the results in a new set instead of returning them. That way Redis does all the heavy lifting and only sends me the actual intersection I am interested in.
How could I do this, since I can do ZINTERSTORE and ZUNIONSTORE not having RANGESTORE seems like I am missing something.
回答1:
That's a question with many possible answers. Here're my top two:
Use the power of Lua to avoid moving data to the client and have all the work done server-side. You can work around the lack of RANGESTORE this way.
Maintain a sorted set where the scores are made of both attributes, using bit-interleaving. In the Redis-verse there exists an experiment by antirez called redimension which does exactly that. The links are https://www.reddit.com/r/redis/comments/3qjlkk/redimension_ruby_library_implementing/ and my port of the above to Redis lua https://www.reddit.com/r/redis/comments/3s0h73/luaredimension_redis_multidimensional_query/. You can read more about the approach at Multi dimensional indexes.
来源:https://stackoverflow.com/questions/49755663/creating-a-new-set-from-a-range-of-a-sorted-set-in-redis