ZREVRANGEBYSCORE Optimization for out of range offset (#5773)

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
When the offset is too large, the query is very slow. Especially when the offset is greater than the length of zset it is easy to determine whether the offset is greater than the length of zset at first, and If it exceed the length of zset, then return directly.

Co-authored-by: Oran Agra <oran@redislabs.com>
This commit is contained in:
thomaston 2020-11-18 00:35:56 +08:00 committed by GitHub
parent d638b05834
commit 39f716a121
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 0 deletions

View File

@ -2906,6 +2906,12 @@ void genericZrangebyscoreCommand(client *c, int reverse) {
if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL || if ((zobj = lookupKeyReadOrReply(c,key,shared.emptyarray)) == NULL ||
checkType(c,zobj,OBJ_ZSET)) return; checkType(c,zobj,OBJ_ZSET)) return;
/* For invalid offset, return directly. */
if (offset > 0 && offset >= (long)zsetLength(zobj)) {
addReply(c,shared.emptyarray);
return;
}
if (zobj->encoding == OBJ_ENCODING_ZIPLIST) { if (zobj->encoding == OBJ_ENCODING_ZIPLIST) {
unsigned char *zl = zobj->ptr; unsigned char *zl = zobj->ptr;
unsigned char *eptr, *sptr; unsigned char *eptr, *sptr;

View File

@ -432,6 +432,7 @@ start_server {tags {"zset"}} {
create_default_zset create_default_zset
assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES] assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES]
assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES] assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES]
assert_equal {} [r zrangebyscore zset 2 5 LIMIT 12 13 WITHSCORES]
} }
test "ZRANGEBYSCORE with non-value min or max" { test "ZRANGEBYSCORE with non-value min or max" {