I’m using redis 2.6. I’ve faced with strange behavior of ZRANGEBYSCORE function.
I have a sorted set with a length of about a few million elements.
Something like this:
10 marry
15 john
25 bob
...
So compare to queries:
ZRANGEBYSCORE longset 25 50 LIMIT 0 20 works like a charm, it takes milliseconds
ZRANGEBYSCORE longset 25 50 this one hangs up for a minutes!!
All elements which I’m intrested in are in the first hundred of the set.
I think that there’s no need to scan elements with weight greater than “50”
because it is SORTED set.
Please explain how redis scans sorted sets and why there is such a big difference between these two queries.
One of the best things about Redis, IMO, is that you can check the time complexity of each command in the docs. The docs for zrangebyscore specifies:
This means that if you know that you only need a certain number of items, and specify a
LIMIT offset count, ifoffsetis (close to)0, you can consider it O(log(N)), but if the returned number of items is high (or the offset is high), it can be considered O(N).