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 模块,它只允许你存储 key => value 对,其中 key 和 value 只能是 text
(但是 value 可以是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 value(定义在它的 spec).
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屋!