Redis 字符串与 Redis 哈希代表 JSON:效率? [英] Redis strings vs Redis hashes to represent JSON: efficiency?
问题描述
我想将一个 JSON 负载存储到 redis 中.我真的有两种方法可以做到这一点:
I want to store a JSON payload into redis. There's really 2 ways I can do this:
一个使用简单的字符串键和值.
key:user, value:payload(整个 JSON blob,可以是 100-200 KB)
One using a simple string keys and values.
key:user, value:payload (the entire JSON blob which can be 100-200 KB)
SET user:1 payload
使用哈希
HSET 用户:1 用户名某人"
HSET 用户:1 个位置纽约"
HSET 用户:1 个人简介超过 100 行的字符串"
请记住,如果我使用散列,则值长度是不可预测的.它们并不都像上面的生物示例那样简短.
Keep in mind that if I use a hash, the value length isn't predictable. They're not all short such as the bio example above.
哪个内存效率更高?使用字符串键和值,还是使用哈希?
Which is more memory efficient? Using string keys and values, or using a hash?
推荐答案
这取决于您访问数据的方式:
It depends on how you access the data:
选择选项 1:
- 如果您在大多数访问中使用大部分字段.
- 如果可能的键有差异
选择选项 2:
- 如果您在大多数访问中只使用单个字段.
- 如果您始终知道哪些字段可用
P.S.:根据经验,选择在大多数用例中需要较少查询的选项.
P.S.: As a rule of the thumb, go for the option which requires fewer queries on most of your use cases.
这篇关于Redis 字符串与 Redis 哈希代表 JSON:效率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!