Intersection of two or more sorted sets

I have two sorted sets and want to make an intersection, i.e. (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 
+5
source share
2 answers

First you need to check which ZSET fewer ZSET elements, and also clone and trim the shorter one.

Secondly, you leave 2 residues. You can reuse the same helper ZSET for faster cleaning.

I also wanted to offer DUMP and RESTORE for the clone, but for the case of sorted sets, ZUNIONSTORE is actually much faster. Here, the time is set as for 1M elements:

 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" 
+3
source

So, the idea that I had was to use other data structures, namely quadrants, to more efficiently solve the same type of query. You can see my little POC (Redis Quadtree in Hash) made with an “object oriented” Lua: https://gist.github.com/itamarhaber/c1ffda42d86b314ea701

Note: you should be aware that this caused an extremely interesting discussion before, during, and after Redis Developer Day. The intermediate result is a new indexing page, but in the near future Redis is likely to add a higher-level API that makes n-dimensional indexing trivial to use.

+4
source

Source: https://habr.com/ru/post/1232816/


All Articles