Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

SORT sortedset BY score #98

Closed
bungle opened this Issue · 19 comments

10 participants

@bungle

I just found out that executing this:

SORT sortedset BY nosort

Actually doesn't return the values in the order they are in sorted set. Could we extend SORT command to support this:

SORT sortedset BY score

Or is there another way to archive this without extra keys?

@huangz1990

use ZRANGEBYSCORE or ZREVRANGEBYSCORE instead.

@bungle

I should have been more explicit.

How about this:

SORT sortedset BY score GET item:*
@huangz1990

hmmm, you want something like this?

redis 127.0.0.1:6379> ZADD s 1 a
(integer) 1
redis 127.0.0.1:6379> ZADD s 2 b 
(integer) 1
redis 127.0.0.1:6379> ZADD s 3 c
(integer) 1

redis 127.0.0.1:6379> MSET item:a "item:a" item:b "item:b" item:c "item:c"
OK

redis 127.0.0.1:6379> SORT s ALPHA
1) "a"
2) "b"
3) "c"

redis 127.0.0.1:6379> SORT s ALPHA GET item:*
1) "item:a"
2) "item:b"
3) "item:c"

"BY score" is not needed here, "SORT sortedset" is enough, and i use "SORT s ALPHA" because sorted set 's' is stored string value.

@antirez
Owner

Hello, I'm usually not very inclined about making changes to SORT, it's at its maximum level of complexity already IMHO, and the scripting support in 2.6 will make it easy to do fancy stuff server side.

But... I must agree that "SORT sortedset BY nosort" should return elements ordered by score as it is the least surprise behavior for sure, and is probably also faster since the sorted set is a linked list.

So I'm accepting this feature as: "make sure that when no sorting is performed by SORT, elements in a sorted set are returned in the obvious order, that is, by score".

Thanks for reporting.

@bungle

Hi huangz1990,

I already knew that you can do that (see my original question "without extra keys"). It's rather nasty to duplicate the scores to extra keys when you already have the scores in sorted set.

antirez,

Your suggestion is fine as with this "nosort" behavior we have the same functionality as with for example:

ASC Sorting sorted set (i.e. SORT sortedset BY score):

SORT sortedset BY nosort
SORT sortedset BY nosort ASC

DESC Sorting sorted set (i.e. SORT sortedset BY score DESC):

SORT sortedset BY nosort DESC
@ingoclaro

this would be nice to have. I'm currently making an zrevrange to get the elements ordered, storing them in a list and then doing the sort to retrieve a list of hashes. With this I could skip that extra step.

@windix

Any update on this? We were caught by it recently.

Currently we have to maintain another extra_list just to store the score just for sorting:

SORT sortedset BY extra_list GET another_list

With this implemented we can get rid of that extra_list

@klodio

windix:

I just tried

> SORT sortedset BY score GET item_* 

and it works for me, in 2.6.

@windix

Hi Klodio,

I tried on the latest version 2.6.0-RC5 and it doesn't work for me. I guess it treats the "score" as an non-existing key, so sorting is simply skipped. See "Skip sorting the elements" in http://redis.io/commands/sort

Correct me if i am wrong, thanks :)

@klodio

You're right,
The set I was checking with happened to be already sorted, so I couldn't realize that sort was skipped. My bad, should have done some proper checking before posting :).

@myanimal

I'd also appreciate this feature.

@swarupe04

Any update on this. It would tremendously help in my case, instead of having to keep track of another set of keys just for sorting.

@bungle

Any updates to this? It was posted and approved about a year ago, but it's still unresolved. Antirez even said that it could increase the performance. This sounds trivial to fix. Am I wrong?

@n0nSmoker

We neeeeeed this feature too)

@antirez
Owner

Note: make sure that ASC/DESC will do what makes sense in this context.

@swarupe04

Hi Antirez,

Are u indicating that this feature already exists?

@antirez antirez referenced this issue from a commit
@antirez "SORT by nosort" (skip sorting) respect sorted set ordering.
When SORT is called with the option BY set to a string constant not
inclduing the wildcard character "*", there is no way to sort the output
so any ordering is valid. This allows the SORT internals to optimize its
work and don't really sort the output at all.

However it was odd that this option was not able to retain the natural
order of a sorted set. This feature was requested by users multiple
times as sometimes to call SORT with GET against sorted sets as a way to
mass-fetch objects can be handy.

This commit introduces two things:

1) The ability of SORT to return sorted sets elements in their natural
ordering when `BY nosort` is specified, accordingly to `DESC / ASC` options.
2) The ability of SORT to optimize this case further if LIMIT is passed
as well, avoiding to really fetch the whole sorted set, but directly
obtaining the specified range.

Because in this case the sorting is always deterministic, no
post-sorting activity is performed when SORT is called from a Lua
script.

This commit fixes issue #98.
9a914a6
@antirez antirez referenced this issue from a commit
@antirez "SORT by nosort" (skip sorting) respect sorted set ordering.
When SORT is called with the option BY set to a string constant not
inclduing the wildcard character "*", there is no way to sort the output
so any ordering is valid. This allows the SORT internals to optimize its
work and don't really sort the output at all.

However it was odd that this option was not able to retain the natural
order of a sorted set. This feature was requested by users multiple
times as sometimes to call SORT with GET against sorted sets as a way to
mass-fetch objects can be handy.

This commit introduces two things:

1) The ability of SORT to return sorted sets elements in their natural
ordering when `BY nosort` is specified, accordingly to `DESC / ASC` options.
2) The ability of SORT to optimize this case further if LIMIT is passed
as well, avoiding to really fetch the whole sorted set, but directly
obtaining the specified range.

Because in this case the sorting is always deterministic, no
post-sorting activity is performed when SORT is called from a Lua
script.

This commit fixes issue #98.
2ba9627
@antirez
Owner

Fixed & merged into 2.6. Closing, thanks for your help.

@antirez antirez closed this
@twidi twidi referenced this issue in yohanboniface/redis-limpyd
Merged

Enhance collections #53

@jadlr

Does this mean that this...

redis 127.0.0.1:6379> zadd globalscores 1 user1 2 user2 3 user3 4 user4
(integer) 4
redis 127.0.0.1:6379> SORT globalscores BY nosort DESC
1) "user4"
2) "user3"
3) "user2"
4) "user1"

has the same complexity as:

redis 127.0.0.1:6379> zrevrange globalscores 0 -1
1) "user4"
2) "user3"
3) "user2"
4) "user1"
@ghost

Does anyone knows the answer to the last question?
I would really help me. Thanks

@JackieXie168 JackieXie168 referenced this issue from a commit
@danielmewes danielmewes Push errors while binding to socket down all the way to server and ha…
…ndle it gracefully there (i.e. by shutting down).

Exceptions would have been kind of nice here ;-)

Closes #98.
2c5c4fb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.