zrangebyscore
ZRANGEBYSCORE
![]() |
![]() Redis Technical Support |
![]() Redis Enterprise Server |
---|
score로 범위를 지정해서 조회
사용법은 zrangebyscore key min max 이다.
min, max는 score의 범위이고, min, max 를 포함해서 조회한다.
모두 조회하려면 -inf, +inf를 사용한다.
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 |
명령> | zrangebyscore mycom -inf +inf withscores |
결과> |
0) 1987 -> American motors 1) 1992 -> Wang 2) 1998 -> Digital Equipment 3) 2002 -> K-mart score가 같을 경우 member로 비교한다 4) 2002 -> Netscape 5) 2009 -> Sun microsystems |
명령> | zrangebyscore mycom 1998 2002 withscores |
결과> |
0) 1998 -> Digital Equipment 1) 2002 -> K-mart 2) 2002 -> Netscape |
명령> | zrangebyscore mycom 2005 +inf withscores |
결과> | 0) 2009 -> Sun microsystems |
포함하지 않게 하려면 min, max에 ( 를 사용
사용법은 zrangebyscore key (min (max 이다.
Example
명령> | zrangebyscore mycom (1987 (2002 withscores |
결과> |
0) 1992 -> Wang 1) 1998 -> Digital Equipment |
명령> | zrangebyscore mycom (1998 2002 withscores 1998 < score <= 2002 |
결과> |
0) 2002 -> K-mart 1) 2002 -> Netscape |
limit offset count 사용
사용법은 zrangebyscore key min max limit offset count 이다.
offset은 시작점을 나타내고, count는 조회할 member의 개수이다.
offset은 0부터 시작할 수 있고, count가 1 이상이여야 한다. 0 이면 조회되지 않는다.
limit가 있으면 offset count 모두 있어야 한다. 생략할 수 없다.
page 별 조회에 유용하게 사용할 수 있다.
Example
명령> | zrangebyscore mycom -inf +inf withscores limit 0 3 |
결과> |
0) 1987 -> American motors 1) 1992 -> Wang 2) 1998 -> Digital Equipment |
명령> | zrangebyscore mycom -inf +inf withscores limit 3 100 |
결과> |
0) 2002 -> K-mart 1) 2002 -> Netscape 2) 2009 -> Sun microsystems |
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
명령> | zrangebyscore post-view -inf +inf withscores JOIN post-good JOIN post-bad table |
결과> |
0) "value" "post-view" "post-good" "post-bad" 1) "post-100" "100" "70" "20" 2) "post-101" "101" "71" "21" 3) "post-102" "102" "72" "22" |
조인 기능은 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
명령> | zrangebyscore 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-100" "100" "70" "20" "user-100" "Title-A" "Content-A" 2) "post-101" "101" "71" "21" "user-101" "Title-B" "Content-B" 3) "post-102" "102" "72" "22" "user-102" "Title-C" "Content-C" |
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
명령> | zrangebyscore 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-100" "100" "70" "20" "user-100" "Title-A" "Content-A" "Kim,kim@naver.com" 2) "post-101" "101" "71" "21" "user-101" "Title-B" "Content-B" "Yun,yun@naver.com" 3) "post-102" "102" "72" "22" "user-102" "Title-C" "Content-C" "Tae,tae@naver.com" |
명령문
ZRANGEBYSCORE key min max [withscores] [limit offset count] [JOIN zset-key JOIN hmget * field1 field2 JOIN get field1]
- 이 명령은 version 1.2.0 부터 사용할 수 있습니다.
- Withscores 옵션은 version 2.0.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 |
<< ZREVRANGE | ZRANGEBYSCORE | ZREVRANGEBYSCORE >> |
---|