Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of two or more sorted sets

Tags:

redis

set

nosql

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
like image 399
mpapec Avatar asked Oct 02 '15 16:10

mpapec


People also ask

What is union and intersection of two sorted arrays?

Union of arrays arr1[] and arr2[] To find union of two sorted arrays, follow the following merge procedure : 1) Use two index variables i and j, initial values i = 0, j = 0. 2) If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i. 3) If arr1[i] is greater than arr2[j] then print arr2[j] and increment j ...

How do you find the intersection of two sets in Redis?

@write , @sortedset , @slow. Computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in destination . It is mandatory to provide the number of input keys ( numkeys ) before passing the input keys and the other (optional) arguments.

How do you find the intersection of two arrays in CPP?

Similarly, intersection of two arrays will be denoted by A ∩ B. It is an array of the elements that are present in both the given arrays. For this, we will traverse through the elements of the first array one by one. Simultaneously we will be checking if that element is present in the second array or not.

What is union of array?

Overview. A union is a set that contains values or elements present in the sets we are comparing. We can use the union() method to get the union between a set and an array.


2 Answers

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

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

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"
like image 95
Ofir Luzon Avatar answered Nov 15 '22 10:11

Ofir Luzon


So the idea I had is to use a different data structures, namely a quadtree, to achieve the same type of query more efficiently. You can see my little POC (Redis Quadtree in Hash) made with "object-oriented" Lua at: https://gist.github.com/itamarhaber/c1ffda42d86b314ea701

Note: you should know that this sparked an extremely interesting discussion before, during and after the Redis Developers Day. The intermediate result is the new indexing page but in the near future Redis is likely to be added with a higher-level API that will make n-dimensional indexing trivial to use.

like image 28
Itamar Haber Avatar answered Nov 15 '22 11:11

Itamar Haber