当前位置:网站首页>面试官: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)两种。当哈希数据类型的键和值的长度较小并且键值对数量较少时,使用压缩列表作为内部实现,否则使用哈希表作为内部实现。
竟然已经看到这里了,你我定是有缘人,留下你的点赞和关注,他日必成大器。
边栏推荐
- Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
- Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
- The only core indicator of high-quality software architecture
- PostgreSQL 9.1 soaring Road
- 【云原生 | Kubernetes篇】深入了解Ingress(十二)
- WPF double slider control and forced capture of mouse event focus
- [AI system frontier dynamics, issue 40] Hinton: my deep learning career and research mind method; Google refutes rumors and gives up tensorflow; The apotheosis framework is officially open source
- C language Dormitory Management Query Software
- Node の MongoDB 安装
- 2022年中国移动阅读市场年度综合分析
猜你喜欢
Three schemes to improve the efficiency of MySQL deep paging query

比量子化学方法快六个数量级,一种基于绝热状态的绝热人工神经网络方法,可加速对偶氮苯衍生物及此类分子的模拟

《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
![[cloud native | kubernetes] in depth understanding of ingress (12)](/img/34/67eae1e5df89bb0a356a1c29a5e007.png)
[cloud native | kubernetes] in depth understanding of ingress (12)

Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout

Xue Jing, director of insight technology solutions: Federal learning helps secure the flow of data elements

CommVault cooperates with Oracle to provide metallic data management as a service on Oracle cloud

How real-time cloud interaction helps the development of education industry

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

一个数据人对领域模型理解与深入
随机推荐
PostgreSQL 9.1 soaring Road
remount of the / superblock failed: Permission denied
FS4056 800mA充电ic 国产快充电源ic
mysql三级分销代理关系存储
Is the outdoor LED screen waterproof?
Optional values and functions of the itemized contenttype parameter in the request header
【云原生 | Kubernetes篇】深入了解Ingress(十二)
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
C array supplement
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
Master the use of auto analyze in data warehouse
Backgroundworker usage example
JVM系列——栈与堆、方法区day1-2
Zhongang Mining: in order to ensure sufficient supply of fluorite, it is imperative to open source and save flow
C language Dormitory Management Query Software
C#基础深入学习一
Introduction to XML II
Apache服务器访问日志access.log设置
请问大佬们有遇到这个情况吗,cdc 1.4 连接MySQL 5.7 无法使用 timestamp
Dry goods sorting! How about the development trend of ERP in the manufacturing industry? It's enough to read this article