Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SORT sortedset BY score #98

Closed
bungle opened this issue Sep 22, 2011 · 19 comments
Closed

SORT sortedset BY score #98

bungle opened this issue Sep 22, 2011 · 19 comments

Comments

@bungle
Copy link

bungle commented Sep 22, 2011

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?

@huangzworks
Copy link
Contributor

use ZRANGEBYSCORE or ZREVRANGEBYSCORE instead.

@bungle
Copy link
Author

bungle commented Sep 23, 2011

I should have been more explicit.

How about this:

SORT sortedset BY score GET item:*

@huangzworks
Copy link
Contributor

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
Copy link
Contributor

antirez commented Sep 23, 2011

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
Copy link
Author

bungle commented Sep 23, 2011

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
Copy link

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
Copy link

windix commented Jun 28, 2012

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
Copy link

klodio commented Jul 12, 2012

windix:

I just tried

> SORT sortedset BY score GET item_* 

and it works for me, in 2.6.

@windix
Copy link

windix commented Jul 24, 2012

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
Copy link

klodio commented Jul 24, 2012

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
Copy link

I'd also appreciate this feature.

@swarupe04
Copy link

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
Copy link
Author

bungle commented Sep 26, 2012

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
Copy link

We neeeeeed this feature too)

@antirez
Copy link
Contributor

antirez commented Sep 29, 2012

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

@swarupe04
Copy link

Hi Antirez,

Are u indicating that this feature already exists?

antirez added a commit that referenced this issue Oct 3, 2012
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.
antirez added a commit that referenced this issue Oct 3, 2012
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.
@antirez
Copy link
Contributor

antirez commented Oct 3, 2012

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

@jadlr
Copy link

jadlr commented Oct 8, 2012

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
Copy link

ghost commented Mar 18, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants