两个或多个排序集的交集 [英] Intersection of two or more sorted sets

查看:48
本文介绍了两个或多个排序集的交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个已排序的集合,并且想要交集,即.(年龄在 25、35 岁之间)和(工资在 250、350 岁之间)

I have two sorted sets, and want to make intersection, ie. (age BETWEEN 25, 35) AND (salary BETWEEN 250, 350)

有没有比以下更好的方法来提高效率:

Is there a better way regarding efficiency than:

ZUNIONSTORE t_age 1 age WEIGHTS 1
ZREMRANGEBYSCORE t_age -inf (25
ZREMRANGEBYSCORE t_age (35 +inf
ZINTERSTORE result 2 salary t_age WEIGHTS 1 0
ZRANGEBYSCORE result 250 350

推荐答案

你应该先用 ZCARD,然后克隆并修剪较短的一张.

You should first check which ZSET has less elements with ZCARD, and clone and trim the shorter one.

其次,您还剩 2 个剩菜.您可以重复使用相同的辅助 ZSET 以加快清理速度.

Second, you are leaving 2 leftovers. You can reuse the same auxiliary ZSET to have a faster cleanup.

我还想推荐 DUMPRESTORE 用于克隆,但对于排序集情况 ZUNIONSTORE 实际上要快得多.以下是 100 万个元素集的时间安排:

I also wanted to suggest DUMP and RESTORE for the clone, but for the sorted sets case ZUNIONSTORE is actually much faster. Here's a timing of both for a 1M elements set:

1) 1) (integer) 14
   2) (integer) 1444165498
   3) (integer) 936762
   4) Complexity info: N:1000000,M:1000000
   5) 1) "ZUNIONSTORE"
      2) "temp3"
      3) "1"
      4) "temp1"
      5) "WEIGHTS"
      6) "1"
2) 1) (integer) 13
   2) (integer) 1444165421
   3) (integer) 3166360
   4)
   5) 1) "evalsha"
      2) "48286113cfe4b389d516e98646e5f4e086decc34"
      3) "2"
      4) "temp1"
      5) "temp2"
      6) "0"

这篇关于两个或多个排序集的交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆