Redis - 데이터타입 - sorted set 알아보기

     

     

    이번 포스팅에서 Redis 데이터 타입은 sorted set 에 대해서 알아보도록 하겠습니다.

     

     

     

     

       Linux Redis 이전글

     

     

     

     

    https://server-talk.tistory.com/471 - Redis - In-Memory - 알아보기​

     

     

    https://server-talk.tistory.com/472 - Redis - 서버 설치 및 기본설정 알아보기

     

     

    https://server-talk.tistory.com/473 - Redis - Redis-cli 기본 사용법

     

     

    https://server-talk.tistory.com/474 - Redis - Redis Desktop Manager Tool

     

     

    https://server-talk.tistory.com/475 - phpredis 설치 알아보기

     

     

    https://server-talk.tistory.com/476 - Redis 활용하기 - phpredis 세션관리하기

     

     

    https://server-talk.tistory.com/478 - Redis - 운영시 주의해야되는 명령어

     

     

    https://server-talk.tistory.com/481 - Redis - 데이터타입 - String 알아보기

     

     

    https://server-talk.tistory.com/482 - Redis - 데이터타입 - Lists 알아보기

     

     

    https://server-talk.tistory.com/483 - Redis - 데이터타입 - SETS 알아보기

     

     

     

     

       Redis DataType - Sorte Set 이란?

     

     

     

     

     

     

    우리가 일반적으로 사용되는 MariaDB, Mysql, MSSQL과 같은 관계형 DBMS는 순위를 구현하는 경우 Score 값을 기준으로 조회한후에 정렬하고 순위를 추출하게 됩니다

     

     

     

     

    그러나 관계형 데이터베이스 같은 경우 데이터가 많은 경우 속도가 더욱더 느려지게되고 수만건 또는 수십만건의 데이터를 조회하는데 오랜시간이 소요되면서 서비스의 품질이 내려가게 됩니다

     

     

     

     

    Redis Sorted Set을 이용하는 경우 빠른시간 내에 조회할 수 있습니다 그 이유는 1개의 Key의 데이터를 저장할때 Member(멤버)의 Score를 지정하면 Member(멤버)와 Score가 자동으로 연결되면서 정렬된 형태로 저장되기 때문에 인덱스를 이용하여 빠르게 조회할 수 있습니다

     

     

     

    Redis의 Sorted Set은 Key 안에 중복되는 Member(멤버)를 저장하지 않고 모든 데이터는 Score의 연결되어 있습니다.

     

     

     

       Redis DataType - Sorte Set 명령어 사용법

     

     

    [Rank Data]

     

     

    ZADD Command

     

     

    사용법 ZADD [Key] [Score] [Member]

     

    127.0.0.1:6379> ZADD movie 2810770000 Avatar-2009 
    (integer) 1
    127.0.0.1:6379> ZADD movie 2797500000 Avengers:Endgame-2019 
    (integer) 1
    127.0.0.1:6379> ZADD movie 2201640000 Titanic-1997 
    (integer) 1
    127.0.0.1:6379> ZADD movie 2068450000 StarWars:The-Force-Awakens-2015 
    (integer) 1
    127.0.0.1:6379> ZADD movie 2048350000 Avengers:-Infinity-War2018 
    (integer) 1

     

    ZADD 명령어는 Key, Score, Member를 추가할때 사용되며 Score는 반드시 숫자로 입력하여야 됩니다.

     

     

     

    ZSCORE Command

     

    사용법 : ZSCORE [Key] [Member]

     

    127.0.0.1:6379> ZSCORE movie Avengers:Endgame-2019
    "2797500000"

     

    ZSCORE 명령어는 Member의 Score를 조회합니다.

     

     

     

    ZRANK Command

     

    사용법 : ZRANK [Key] [Member]

     

    127.0.0.1:6379> ZRANK movie Avengers:Endgame-2019 
    (integer) 3

     

    ZRANK 명령어는 Member의 순위를 조회합니다.

     

     

     

    ZRANGE Command

     

    사용법 : ZRANGE [Key] [Index] [Last Value]

     

    127.0.0.1:6379> ZRANK movie Avengers:Endgame-2019 
    (integer) 3
    127.0.0.1:6379> ZRANGE movie 0 -1
    1) "Avengers:-Infinity-War2018"
    2) "StarWars:The-Force-Awakens-2015"
    3) "Titanic-1997"
    4) "Avengers:Endgame-2019"
    5) "Avatar-2009"

     

    ZRANGE 명령어는 Key 값의 인덱스와 Last Value를 지정하여 순위를 조회할 수 있습니다.

     

     

     

    ZRANGEBYSCORE Command

     

    사용법 : ZRANGEBYSCORE [Key] [Min] [Max]

     

    127.0.0.1:6379> ZRANGEBYSCORE movie 2068450000 2797500000 WITHSCORES
    1) "StarWars:The-Force-Awakens-2015"
    2) "2068450000"
    3) "Titanic-1997"
    4) "2201640000"
    5) "Avengers:Endgame-2019"
    6) "2797500000"

     

    ZRANGEBYSCORE 명령어는 Key의 범위를 Min에서 Max를 지정하여 순위를 조회할 수 있습니다.

     

    ZINCRBY  Command

     

    사용법 : ZINCRBY [Key] [Score] [Member]

     

    127.0.0.1:6379> ZINCRBY movie 100 Avengers:Endgame-2019 
    "2797500100"
    127.0.0.1:6379> ZINCRBY movie -100 Titanic-1997
    "2201639900"

     

    ZINCRBY 명령어는 Key 값의 Member 지정하고 Score를 증가, 감소 할수 있습니다.

     

     

     

     

    ZREM Command

     

    사용법 : ZREM [Key] [Member]

     

    127.0.0.1:6379> ZREM movie Avengers:Endgame-2019
    (integer) 1

     

    ZREM 명령어는 사용하지 않는 Member를 삭제합니다.

    Posted by 서버이야기