zrevrangebyscore
ZREVRANGEBYSCORE
![]() |
![]() |
![]() |
---|
score로 범위를 지정해서 역순으로 조회
사용법은 zrevrangebyscore key max min 이다.
max, min 은 score의 범위이고, max, min 를 포함해서 조회한다.
모두 조회하려면 +inf, -inf를 사용한다.
zrangebyscore와 비교할때 조회 순서는 큰 값부터이고, max를 앞에 지정한다.
score를 같이 보려면 withscores 옵션을 사용한다.
Example
명령> | zadd mycom 2009 "Sun microsystems" 1992 Wang 2002 Netscape |
결과> | 3 |
명령> | zadd mycom 1998 "Digital Equipment" 2002 K-mart 1987 "American motors" |
결과> | 3 |
명령> | zrevrangebyscore mycom +inf -inf withscores |
결과> |
0) 2009 -> Sun microsystems 1) 2002 -> Netscape 2) 2002 -> K-mart 3) 1998 -> Digital Equipment 4) 1992 -> Wang 5) 1987 -> American motors |
명령> | zrevrangebyscore mycom 2002 1998 withscores |
결과> |
0) 2002 -> Netscape 1) 2002 -> K-mart 2) 1998 -> Digital Equipment |
명령> | zrevrangebyscore mycom +inf 2005 withscores |
결과> | 0) 2009 -> Sun microsystems |
포함하지 않게 하려면 max, min에 ( 를 사용
사용법은 zrangebyscore key (max (min 이다.
Example
명령> | zrevrangebyscore mycom (2002 (1987 withscores 2002 > score > 1987 |
결과> |
0) 1998 -> Digital Equipment 1) 1992 -> Wang |
명령> | zrevrangebyscore mycom 2002 (1998 withscores 2002 >= score > 1998 |
결과> |
0) 2002 -> Netscape 1) 2002 -> K-mart |
limit offset count 사용
사용법은 zrangebyscore key max min limit offset count 이다.
offset은 시작점을 나타내고, count는 조회할 member의 개수이다.
offset은 0부터 시작하고, count가 1 이상이여야 한다. 0 이면 조회되지 않는다.
limit가 있으면 offset count 모두 있어야 한다. 생략할 수 없다.
page 별 조회에 유용하게 사용할 수 있다.<
Example
명령> | zrevrangebyscore mycom +inf -inf withscores limit 0 3 |
결과> |
0) 2009 -> Sun microsystems 1) 2002 -> Netscape 2) 2002 -> K-mart |
명령> | zrevrangebyscore mycom +inf -inf withscores limit 3 100 |
결과> |
0) 1998 -> Digital Equipment 1) 1992 -> Wang 2) 1987 -> American motors |
JOIN(조인) 기능
JOIN(조인)은 ZSet key1의 멤버와 다른 ZSet key2의 멤버를 조인해서 key1의 value, score와
key2의 score를 한번에 조회합니다.
예) 글(포스트) 관리:
post-view 키에 멤버 post-id(post-100, post-101, ...), 조회수를 스코어로 넣고,
post-good 키에 좋아요수, post-bad에 싫어요수를 넣는다.
ZADD post-view 100 post-100 101 post-101 102 post-102
ZADD post-good 70 post-100 71 post-101 72 post-102
ZADD post-bad 20 post-100 21 post-101 22 post-102
Example
명령> | zrevrangebyscore post-view +inf -inf withscores JOIN post-good JOIN post-bad table |
결과> |
0) "value" "post-view" "post-good" "post-bad" 1) "post-102" "102" "72" "22" 2) "post-101" "101" "71" "21" 3) "post-100" "100" "70" "20" |
조인 기능은 Enterprise 서버에서 사용 가능합니다. |
Hash 키와 조인
Hash 키에 post-id 별로 userid, title, content를 저장한다.
hset post-100 userid user-100 title Title-A content content-A
hset post-101 userid user-101 title Title-B content content-B
hset post-102 userid user-102 title Title-C content content-C
Example
명령> | zrevrangebyscore post-view +inf -inf withscores
JOIN post-good JOIN post-bad JOIN hmget * userid title content table |
결과> |
0) "value" "post-view" "post-good" "post-bad" "userid" "title" "content" 1) "post-102" "102" "72" "22" "user-102" "Title-C" "Content-C" 2) "post-101" "101" "71" "21" "user-101" "Title-B" "Content-B" 3) "post-100" "100" "70" "20" "user-100" "Title-A" "Content-A" |
String 키와 조인
String 키에 user-id 별로 user 정보를 저장한다.
set user-100 "Kim,kim@naver.com"
set user-101 "Yun,yun@naver.com"
set user-102 "Tae,tae@naver.com"
Example
명령> | zrevrangebyscore post-view +inf -inf withscores
JOIN post-good JOIN post-bad JOIN hmget * userid title content JOIN get userid table |
결과> |
0) "value" "post-view" "post-good" "post-bad" "userid" "title" "content" "userid" 1) "post-102" "102" "72" "22" "user-102" "Title-C" "Content-C" "Tae,tae@naver.com" 2) "post-101" "101" "71" "21" "user-101" "Title-B" "Content-B" "Yun,yun@naver.com" 3) "post-100" "100" "70" "20" "user-100" "Title-A" "Content-A" "Kim,kim@naver.com" |
명령문
ZREVRANGEBYSCORE key max min [withscores] [limit offset count] [JOIN zset-key JOIN hmget * field1 field2 JOIN get field1]
- 이 명령은 version 2.2.0 부터 사용할 수 있습니다.
- JOIN은 Enterprise version에서 사용할 수 있습니다.
- 논리적 처리 소요시간은 O(log(N)+M)이다. N은 집합에 속한 member 개수이고, M은 리턴될 member 개수입니다.
관련 명령 | ZRANGE, ZREVRANGE, ZREVRANGEBYSCORE, LRANGE |
Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
<< ZRANGEBYSCORE | ZREVRANGEBYSCORE | ZRANGEBYLEX >> |
---|