gpt4 book ai didi

database - 使用 Redis 哈希与许多键的性能比较

转载 作者:行者123 更新时间:2023-12-03 06:37:33 25 4
gpt4 key购买 nike

好的,我目前正计划使用 Redis 作为我的 NoSQL 数据库的前端缓存。我将在 Redis 数据库中存储很多经常使用的用户数据。我想知道是否制作 key-value每个用户的条目会更好或使用 Redis hash其中该字段是 user id并且值很大 json object .你认为什么会更好?
我看到了 this article来回答这个问题,但它没有讨论值大小的限制。

最佳答案

选择 hashstring根据用例的不同,有许多优点和一些缺点。如果您要选择散列,最好将您的 json 对象设计为散列字段和值,例如;

127.0.0.1:6379> hset user:1 ssn 10101010101 name john surname wick date 2020-02-02 location continental
(integer) 5
127.0.0.1:6379> hgetall user:1
1) "ssn"
2) "10101010101"
3) "name"
4) "john"
5) "surname"
6) "wick"
7) "date"
8) "2020-02-02"
9) "location"
10) "continental"
以下是 hash 的好处当您进行适当的数据建模时,会覆盖字符串。
  • 在性能方面,大多数字符串和哈希命令都具有相同的复杂性。
  • 与字符串相比,更容易访问/更新/删除散列上的单个 json 字段。您不必获取整个字符串、解码、进行更改并重新设置。您可以使用 HDEL , HSETHGET对于那些没有获取整个对象的操作。
  • 如果您的字符串对象的大小增加,您将在传输(获取/设置)整个对象时受到网络和带宽的影响。正如 documentation 中所述

  • Speed of RAM and memory bandwidth seem less critical for global performance especially for small objects. For large objects (>10 KB), it may become noticeable though.


  • 如果您为设计数据大小制定了良好的基准,则哈希比字符串对内存更友好。正如 documentation 中所述以及 instagram engineering 的示例用例您可能会从特殊编码中获得巨大的好处。

  • Hashes, Lists, Sets composed of just integers, and Sorted Sets, when smaller than a given number of elements, and up to a maximum element size, are encoded in a very memory efficient way that uses up to 10 times less memory (with 5 time less memory used being the average saving).


    另一方面,取决于您的用例;
  • ziplist不是免费的,它是内存和 CPU 之间的权衡。
  • 您不能部分过期哈希字段。如果你分成多个字符串那么你可以EXPIRE它们,但在散列中,只有顶级 key 可以与所有值一起过期。
  • 关于database - 使用 Redis 哈希与许多键的性能比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62974363/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com