YuriBro
YuriBro

Reputation: 902

Redis ZRANGEBYSCORE strange behaviour

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.

Upvotes: 1

Views: 2176

Answers (1)

Linus Thiel
Linus Thiel

Reputation: 39223

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:

Time complexity: O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

[...]

Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

This means that if you know that you only need a certain number of items, and specify a LIMIT offset count, if offset is (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).

Upvotes: 5

Related Questions