简体   繁体   中英

Redis Zset Locate record

Why can redis locate a record in zset in log(n) time both by score and key? Does redis actually store two indexes for a zset?

I thought if we have a skiplist that determines a record by its key, we can only index by this key.

SkipNode
   key
     k1  #value
     k2  #score


  |------------------------------>   |
  |-...                              |
  |------------->|-------...---------|
skipNode1 -> skipNode2 -> ... skipNodeN

we can only locate a record by key, in leftmost, (k1, k2), order, how can we index a record by k2 only?

Why can redis locate a record in zset in log(n) time both by score and key?Z

The time complexity of searching by key is O(1), and by score is O(log(n)).

Does redis actually store two indexes for a zset?

Yes, it has two indexes. A hash index for key, and a skip list index for score.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM