相交用排序集设置

相交用排序集设置

本文介绍了Redis:如何与“正常"相交用排序集设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个包含 100 到 1000 个字符串的集合(或排序集合或列表,如果那样更好)A.

Assume I have a set (or sorted set or list if that would be better) A of 100 to 1000 strings.

然后我有一个由更多字符串组成的有序集合 B,比如说一百万.

Then I have a sorted set B of many more strings, say one million.

现在C应该是AB(当然是字符串)的交集.

Now C should be the intersection of A and B (of the strings of course).

我想要 X 在 C 中的每个元组 (X, SCORE_OF_X_IN_B).

I want to have every tuple (X, SCORE_OF_X_IN_B) where X is in C.

有什么想法吗?

我有两个想法:

  1. Interstore
    • 存储一个排序集合,每个分数为 0
    • interstore 到 D
    • 获取 D 的每一项
    • 删除D
  • 在我的客户端程序中循环 A
  • 获取每个字符串的 zscore

虽然1.在redis方面有太多的开销(例如必须写.redis页面也说明了相当高的时间复杂度http://redis.io/commands/zinterstore), 2. 会有 |A|数据库连接,不会是一个好的选择.

While 1. has way too much overhead on the redis side (Has to write for example. The redis page states quite a high time complexity, too http://redis.io/commands/zinterstore), 2. would have |A| database connections and won't be a good choice.

也许我可以编写一个 redis/lua 脚本,它可以像 zscore 一样工作,但具有任意数量的字符串,但我不确定我的主机是否允许脚本...

Maybe I could write a redis/lua script which will work like zscore but with an arbitrary number of strings, but I'm not sure if my hoster allows scripts...

所以我只是想问一下,是否有一个优雅且快速的解决方案,无需编写脚本!

So I just wanted to ask SO, if there is an elegant and fast solution available without scripting!

推荐答案

有一个简单的解决方案可以解决您的问题:ZINTERSTORE 将使用 SET>ZSET.试试:

There is a simple solution to your problem: ZINTERSTORE will work with a SET and a ZSET. Try:

redis> sadd foo a
(integer) 1
redis> zadd bar 1 a
(integer) 1
redis> zadd bar 2 b
(integer) 1
redis> zinterstore baz 2 foo bar AGGREGATE MAX
(integer) 1
redis> zrange baz 0 -1 withscores
1) "a"
2) "1"

我在上面添加了 AGGREGATE MAX,因为 redis 会给(非排序)集合 foo 的每个成员一个默认分数1SUM 与它在(排序的)集合 bar 中的任何分数.

I added AGGREGATE MAX above, since redis will give each member of the (non-sorted) set foo a default score of 1, and SUM that with whatever score it has in the (sorted) set bar.

这篇关于Redis:如何与“正常"相交用排序集设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 03:26