Does zscan have a defined order?

665 views
Skip to first unread message

Marc Gravell

unread,
Jun 27, 2017, 6:56:28 AM6/27/17
to Redis DB
It isn't obvious to me from the SCAN page (ZSCAN just points to SCAN) whether there is a defined order for ZSCAN - in particular, is this "by score"? This is less relevant for SCAN / SSCAN and HSCAN since they don't really have a defined order in the first place.

Regardless of the answer, can I suggest that the ordering semantics (even if "there is no defined order, even for ZSCAN") be explicit on the SCAN page?

Marc

Joshua Scott

unread,
Jun 28, 2017, 4:03:41 PM6/28/17
to Redis DB

SCAN at least does have a determinate order, but it's not sorted by anything that is meaningful to the user. If you put keys in redis and do SCAN, you'll get the same order every time (assuming that no keys are changed).

I would assume that ZSCAN does the same thing, since the documentation states that they all work the same. ZSCAN definitely doesn't sort by anything before returning entries, since then it would have to retrieve the entire data structure and sort it, which would not be O(1).

Josh

Marc Gravell

unread,
Jun 28, 2017, 4:10:00 PM6/28/17
to redi...@googlegroups.com
The subtle difference being, however, that sorted sets are... sorted. So there is a notional built in sort which the others lack. This was the crux of the question.

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+unsubscribe@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

Dvir Volk

unread,
Jul 3, 2017, 7:17:32 AM7/3/17
to redi...@googlegroups.com
Reading the code on the repo, it seems that no, it's not sorted, unless the sorted set is small and implemented as a ziplist (which is the same for a hash key BTW).

Since a sorted set is basically a Hash (for value access) plus a Skiplist (for score access), ZSCAN does a clever trick of simply passing the sorted set's hash table to the iteration mechanism, and from there on it's basically like doing HSCAN and SCAN. Thus, the order is not guaranteed.



On Wed, Jun 28, 2017 at 11:09 PM Marc Gravell <marc.g...@gmail.com> wrote:
The subtle difference being, however, that sorted sets are... sorted. So there is a notional built in sort which the others lack. This was the crux of the question.
On 28 Jun 2017 9:03 p.m., "Joshua Scott" <joshua...@gmail.com> wrote:

SCAN at least does have a determinate order, but it's not sorted by anything that is meaningful to the user. If you put keys in redis and do SCAN, you'll get the same order every time (assuming that no keys are changed).

I would assume that ZSCAN does the same thing, since the documentation states that they all work the same. ZSCAN definitely doesn't sort by anything before returning entries, since then it would have to retrieve the entire data structure and sort it, which would not be O(1).

Josh

On Tuesday, June 27, 2017 at 5:56:28 AM UTC-5, Marc Gravell wrote:
It isn't obvious to me from the SCAN page (ZSCAN just points to SCAN) whether there is a defined order for ZSCAN - in particular, is this "by score"? This is less relevant for SCAN / SSCAN and HSCAN since they don't really have a defined order in the first place.

Regardless of the answer, can I suggest that the ordering semantics (even if "there is no defined order, even for ZSCAN") be explicit on the SCAN page?

Marc

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.

To post to this group, send email to redi...@googlegroups.com.
Visit this group at https://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages