Intersection of two or more sorted sets

后端 未结 2 1979
一生所求
一生所求 2021-01-02 19:50

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

相关标签:
2条回答
  • 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.

    0 讨论(0)
  • 2021-01-02 20:34

    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"
    
    0 讨论(0)
提交回复
热议问题