RamPrakash
RamPrakash

Reputation: 3254

Redis - Sorted Set ZRANGE performance

I am really confused with redis sorted set.

Lets say I have N number of items in the sorted set. I assumed whenever I add an item, it will be sorted in the set.

If I need the lowest score item, I have to use this.

ZRANGE myitem 0 0

But why is the performance is O(log(N)). Should it not be O(1) if it is already sorted & to get the lowest score item?

https://redis.io/commands/zrange

Upvotes: 0

Views: 1859

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48932

As noted in the documentation, Redis uses a skip list to implement sorted sets. Skip lists have a lookup time of O(log n).

There are other answers here that go into more detail on the use of this data structure, see here for example.

Upvotes: 1

Related Questions