Redis字符串与Redis散列代表JSON:效率? [英] Redis strings vs Redis hashes to represent JSON: efficiency?

查看:190
本文介绍了Redis字符串与Redis散列代表JSON:效率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将JSON有效负载存储到Redis中.我确实有2种方法可以做到这一点:

I want to store a JSON payload into redis. There's really 2 ways I can do this:

  1. 使用简单的字符串键和值.
    键:用户,值:有效载荷(整个JSON Blob,可以为100-200 KB)

  1. 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 user:1 username "someone"
HSET user:1 location "NY"
HSET user:1 bio "STRING WITH OVER 100 lines"

HSET user:1 username "someone"
HSET user:1 location "NY"
HSET user:1 bio "STRING WITH OVER 100 lines"

请记住,如果使用散列,则值长度是不可预测的.它们并不像上面的生物示例那样短.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆