在Redis中从一系列排序集中创建一个新集合 [英] Creating a new set from a range of a Sorted Set in Redis
问题描述
我有很多排序集,这些排序集被用作系统上的二级索引,用户查询可能会击中其中一些.
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"
要使用这些索引来使所有30岁以下且得分> 2的用户
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)
但是这意味着我已经将所有数据从redis复制到我的应用服务器以执行交叉,是否有更有效的方法来做到这一点,因为我没有在网络上传输所有匹配范围,而是在Rediss?
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?
我想要的是
ZRANGEBYSCORESTORE tempSet1 scoreSet (2 +inf
ZRANGEBYSCORESTORE tempSet2 ageSet -inf (30
SINTER tempSet1 tempSet2
ZRANGEBYSCORESTORE在其中执行ZRANGEBYSCORE操作并将结果存储在新集中,而不是返回它们.这样,Redis就能完成所有繁重的工作,只向我发送我感兴趣的实际路口.
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.
我该怎么办,因为我可以执行ZINTERSTORE和ZUNIONSTORE,而没有RANGESTORE似乎是我缺少了一些东西.
How could I do this, since I can do ZINTERSTORE and ZUNIONSTORE not having RANGESTORE seems like I am missing something.
推荐答案
这个问题有很多可能的答案.这是我的前两名:
That's a question with many possible answers. Here're my top two:
-
使用Lua的功能来避免将数据移动到客户端,并在服务器端完成所有工作.您可以通过这种方式解决缺少RANGESTORE的问题.
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.
维护一个排序的集合,其中使用位交织对两个属性进行评分.在Redis诗句中,有一个由antirez进行的名为redimension的实验正是这样做的.链接为 https://www.reddit.com/r/redis/comments/3qjlkk/redimension_ruby_library_implementing/和我到Redis lua的端口多维索引中了解更多有关该方法的信息.
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.
这篇关于在Redis中从一系列排序集中创建一个新集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!