Redis:HGETALL的排序问题

HGETALL 介绍

Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

Time complexity:
O(N) where N is the size of the hash.

Return
Array reply: list of fields and their values stored in the hash, or an empty list when key does not exist.

Hash有两种存储格式:ziplist、hashtable

默认情况下,Redis的Hash是使用ziplist进行存储的,当超出一定限制后,再改为hashtable进行存储。

ziplist是双向链表,存储顺序和数据插入顺序一致。查询结果顺序和存储顺序一致。

hashtable是字典结构,查询结果顺序和插入顺序不一致。


两种情况都满足时,Hash会使用 ziplist 进行存储:

  1. 哈希对象保存的所有键值对的键和值的字符串长度都小于 64 字节。对应配置:server.hash_max_ziplist_value。
  2. 哈希对象保存的键值对数量小于 512 个。对应配置:server.hash_max_ziplist_entries。

否则,改为hashtable存储。


查询数据存储结构:OBJECT ENCODING key


结论:

  • HGETALL 没有顺序性。如果需要顺序,业务上层排序。
  • Hash会根据数据量进行数据结构调整。目的:提高性能。
posted @   天下太平  阅读(838)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示