当前位置:网站首页>面试官:Redis中哈希数据类型的内部实现方式是什么?
面试官:Redis中哈希数据类型的内部实现方式是什么?
2022-07-04 12:44:00 【51CTO】
面试官:Redis中基本的数据类型有哪些?
我:Redis的基本数据类型有:字符串(string)、哈希(hash)、列表(list)、集合(set)、有序集合(zset)。
面试官:哈希数据类型的内部实现方式是什么?
我还沉浸在上一个问题的沾沾自喜中,顿时表情凝固了,手心开始冒出冷汗。“这个。。没有太深入了解”,我支支吾吾的说到。
面试官:回去等消息吧。
这句话说的干净利落,然后就没有然后了。失败是成功的妈妈,我不气馁,决定马上恶补一下。
哈希的编码
哈希的编码有两种,分别是压缩列表(ziplist)和哈希表(hashtable)。当所有键值对的键和值的长度都小于hash-max-ziplist-value
(默认为64字节),并且键值对的数量小于hash-max-ziplist-entries
(默认为512个)的时候,哈希就会使用压缩列表作为编码,否则使用哈希表作为编码。
我们来举个例子:
此时,所有键值对的键和值的长度和键值对的数量都比较小,哈希使用压缩列表作为编码。我们再加入一个值比较大的键值对:
此时,哈希的编码由压缩列表转化为编码。
当然,了解以上细节还没能完全“征服”面试官,我们需要更深入一些:)
哈希的底层实现
当压缩列表作为哈希的编码时,有新的键值对加入到哈希数据类型中,先把键的压缩列表节点添加到压缩列表的末尾,然后再把值的压缩列表节点添加到压缩列表的末尾。
所以,在哈希数据类型的压缩列表中,先加入的键值对在压缩列表的头部方向,后加入的键值对在压缩列表的末尾方向;同一个键值对的两个节点是紧挨在一起的,键的节点在前,值的节点在后。
压缩列表使用更加紧凑的内存结构实现多个键值对的连续存储,在节省内存方面比哈希表表现的更加优秀。
当哈希表作为哈希的编码时,每个键值对都使用一个字典键值对保存,字典的每个键都是一个字符串对象,对象中保存键值对的键;字典的每个值也都是一个字符串对象,对象中保存键值对的值。
哈希表虽然没有压缩列表节省内存,但是它的读写时间复杂度为O(1),在时间效率方面比压缩列表表现的更加优秀。
总结
哈希数据类型的内部实现有压缩列表(ziplist)和哈希表(hashtable)两种。当哈希数据类型的键和值的长度较小并且键值对数量较少时,使用压缩列表作为内部实现,否则使用哈希表作为内部实现。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- 7、 Software package management
- XML入门三
- 光环效应——谁说头上有光的就算英雄
- 面向个性化需求的在线云数据库混合调优系统 | SIGMOD 2022入选论文解读
- 《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
- Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- 8 expansion sub packages! Recbole launches 2.0!
- C language dormitory management query software
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
猜你喜欢
HAProxy高可用解决方案
When MDK uses precompiler in header file, ifdef is invalid
"Pre training weekly" issue 52: shielding visual pre training and goal-oriented dialogue
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article
Runc hang causes the kubernetes node notready
提高MySQL深分页查询效率的三种方案
Samsung's mass production of 3nm products has attracted the attention of Taiwan media: whether it can improve the input-output rate in the short term is the key to compete with TSMC
Reptile exercises (I)
7 月数据库排行榜:MongoDB 和 Oracle 分数下降最多
从0到1建设智能灰度数据体系:以vivo游戏中心为例
随机推荐
Comprehensive evaluation of modular note taking software: craft, notation, flowus
After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer
使用宝塔部署halo博客
Use fail2ban to prevent password attempts
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Backgroundworker usage example
Agile development / agile testing experience
Introduction to XML I
SQL language
C语言集合运算
Reinforcement learning - learning notes 1 | basic concepts
从0到1建设智能灰度数据体系:以vivo游戏中心为例
2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
聊聊支付流程的设计与实现逻辑
[cloud native | kubernetes] in depth understanding of ingress (12)
实战:fabric 用户证书吊销操作流程
Personalized online cloud database hybrid optimization system | SIGMOD 2022 selected papers interpretation
How real-time cloud interaction helps the development of education industry
XML入门一