PostgreSQL 引入的 JSONB 说明 [英] Explanation of JSONB introduced by PostgreSQL
问题描述
PostgreSQL 刚刚引入 JSONB 并且它已经成为趋势关于黑客新闻.它与 PostgreSQL 中先前存在的 Hstore 和 JSON 有何不同?
PostgreSQL just introduced JSONB and it's already trending on hacker news. How is it different from Hstore and JSON previously present in PostgreSQL?
它的优点和局限性是什么,什么时候应该考虑使用它?
What are its advantages and limitations and when should someone consider using it?
推荐答案
一、hstore
是一个 contrib 模块,它只允许你存储键 => 值对,其中键和值只能是 text
s(但是值可以是sql NULL
也是).
First, hstore
is a contrib module, which only allows you to store key => value pairs, where keys and values can only be text
s (however values can be sql NULL
s too).
json
和jsonb
允许您存储有效的 JSON 值(在其规范中定义).
Both json
& jsonb
allows you to store a valid JSON value (defined in its spec).
F.ex.这些是有效的 JSON 表示:null
、true
、[1,false,"string",{"foo":"bar"}]
, {"foo":"bar","baz":[null]}
- hstore
与 JSON 的能力相比只是一个小子集(但如果你只是需要这个子集,没关系).
F.ex. these are valid JSON representations: null
, true
, [1,false,"string",{"foo":"bar"}]
, {"foo":"bar","baz":[null]}
- hstore
is just a little subset compared to what JSON is capable (but if you only need this subset, it's fine).
json
& 的唯一区别jsonb
是他们的存储:
The only difference between json
& jsonb
is their storage:
json
以纯文本格式存储,而jsonb
以某种二进制表示形式存储
json
is stored in its plain text format, whilejsonb
is stored in some binary representation
这有 3 个主要后果:
There are 3 major consequences of this:
jsonb
通常比json
需要更多的磁盘空间来存储(有时不会)jsonb
比json
花费更多时间从输入表示构建json
操作比jsonb
花费显着更多的时间(& 每次在json
类型值)
jsonb
usually takes more disk space to store thanjson
(sometimes not)jsonb
takes more time to build from its input representation thanjson
json
operations take significantly more time thanjsonb
(& parsing also needs to be done each time you do some operation at ajson
typed value)
当 jsonb
将在稳定版本中可用时,将有两个主要用例,您可以轻松地在它们之间进行选择:
When jsonb
will be available with a stable release, there will be two major use cases, when you can easily select between them:
- 如果您在应用程序中只使用 JSON 表示,那么 PostgreSQL 仅用于存储 &检索此表示,您应该使用
json
. - 如果你在 PostgreSQL 中对 JSON 值做很多操作,或者对一些 JSON 字段使用索引,你应该使用
jsonb
.
这篇关于PostgreSQL 引入的 JSONB 说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!