用于存储键值映射的最佳PostgreSQL数据类型? [英] Best PostgreSQL datatype for storing key-value maps?

查看:149
本文介绍了用于存储键值映射的最佳PostgreSQL数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个简单的键值字符串映射存储为我的PostgreSQL表中的一个字段.我打算将地图作为一个整体来对待;即,始终选择整个地图,永远不要通过其键或值进行查询.

I'd like to store a simple map of key-value strings as a field in my PostgreSQL table. I intend to treat the map as a whole; i.e, always select the entire map, and never query by its keys nor values.

我已经阅读了hstorejsonjsonb之间进行比较的文章,但是这些并没有帮助我选择最适合我的需求的数据类型,即:

I've read articles comparing between hstore, json and jsonb, but those didn't help me choose which data-type is most fitting for my requirements, which are:

  • 仅键值,无需嵌套.
  • 仅字符串,没有其他类型,也没有null.
  • 考虑到我在该领域的预期用途,存储效率.
  • 快速解析查询的地图.
  • Only key-value, no need for nesting.
  • Only strings, no other types nor null.
  • Storage efficiency, given my intended use for the field.
  • Fast parsing of the queried maps.

哪种数据类型最适合我的用例?

What data-type would best fit my use case?

推荐答案

您可以使用hstore这是一个按键映射,但是我个人会使用jsonb.这有点矫kill过正,但是,大多数语言都可以本地转换json,而无需自己解码.

you could use hstore which is a keymap, however I personally would us jsonb. It's a little overkill, however, most languages can convert json natively without having to decode it yourself.

在json中,我只是存储了一个简单的对象或数组,用于存储您要存储的信息.

In json, I'd just store a simple object or array for the info you're trying to store.

这两个支持索引都被有效地存储.

Both support indexes and are efficiently stored.

Hstore in text是一种自定义类型格式,您的语言可能不知道这种格式,因此需要处理数据才能插入或查询.

Hstore in text is a custom type format that your language may be unaware of and thus require processing the data to insert or query.

这篇关于用于存储键值映射的最佳PostgreSQL数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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