Redis:HGETALL的排序问题

时间:2022-09-29
本文章向大家介绍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会根据数据量进行数据结构调整。目的:提高性能。

原文地址:https://www.cnblogs.com/txtp/p/16743010.html