PostgreSQL刚刚在9.4版中引入了JSONB,它已经成为了黑客新闻的热门话题。它与之前在PostgreSQL中出现的Hstore和JSON有什么不同?

它的优点和局限性是什么?什么时候应该考虑使用它?


Peeyush:

简单的回答是:

如果你在PostgreSQL中做很多JSON操作,比如排序、切片、拼接等,出于速度考虑,你应该使用JSONB。 如果需要对JSON上的任意键搜索进行索引查找,那么应该使用JSONB。 如果以上两种方法都不使用,则可能应该使用JSON。 如果需要保留键顺序、空格和重复的键,则应该使用JSON。

想要得到更长的答案,你需要等我在9.4发行版之前写一篇完整的“HowTo”文章。

hstore is more of a "wide column" storage type, it is a flat (non-nested) dictionary of key-value pairs, always stored in a reasonably efficient binary format (a hash table, hence the name). json stores JSON documents as text, performing validation when the documents are stored, and parsing them on output if needed (i.e. accessing individual fields); it should support the entire JSON spec. Since the entire JSON text is stored, its formatting is preserved. jsonb takes shortcuts for performance reasons: JSON data is parsed on input and stored in binary format, key orderings in dictionaries are not maintained, and neither are duplicate keys. Accessing individual elements in the JSONB field is fast as it doesn't require parsing the JSON text all the time. On output, JSON data is reconstructed and initial formatting is lost.

在我看来,如果您处理的是机器可读的数据,那么一旦jsonb可用,就没有什么重要的理由不使用它。

首先,hstore是一个contrib模块,它只允许你存储key =>值对,其中键和值只能是文本(但是值也可以是sql null)。

json和jsonb都允许你存储一个有效的json值(在其规范中定义)。

F.ex。这些是有效的JSON表示:null, true, [1,false,"string",{"foo":"bar"}], {"foo":"bar","baz":[null]} - hstore只是JSON功能的一个小子集(但如果你只需要这个子集,也没关系)。

json和jsonb之间唯一的区别是它们的存储:

Json以纯文本格式存储,而 Jsonb以某种二进制表示形式存储

这有3个主要后果:

Jsonb通常比json占用更多的磁盘空间(有时不是) 与json相比,Jsonb需要更多的时间从其输入表示构建 Json操作要比jsonb花费更多的时间(并且每次在Json类型的值上执行一些操作时也需要进行解析)

当jsonb的稳定版本可用时,会有两个主要的用例,你可以很容易地在它们之间进行选择:

如果你只在应用程序中使用JSON表示,PostgreSQL只用于存储和检索这种表示,你应该使用JSON。 如果你在PostgreSQL中对JSON值做了很多操作,或者对一些JSON字段使用索引,你应该使用jsonb。

据我所知

hstore as it currently exists (in PostgreSQL 9.3) does not allow for nesting other objects and arrays as the values of its key/value pairs. However, a future hstore patch will allow for nesting. This patch will not be in the 9.4 release and may not be included any time soon. json as it currently exists does allow for nesting, but it is text-based and does not allow for indexing, thus it is "slow" jsonb that will be released with 9.4 will have the current nesting capabilities of json, as well as the GIN/GIST indexing of hstore, so it will be fast

开发PostgreSQL 9.4的人似乎在说,新的、快速的jsonb类型将吸引那些选择使用像MongoDB这样的NoSQL数据存储的人,但它现在可以将关系数据库和可查询的非结构化数据组合在一个框架下

为什么HStore2/jsonb是9.4最重要的补丁

PostgreSQL 9.4 jsonb的基准测试似乎与MongoDB相当,在某些情况下甚至比MongoDB更快。

http://texture.io/alphabetum/postgresql-incl-hstore-vs-mongodb

我今天在PostgresOpen上,基准测试比MongoDB快多了。我相信它的选择速度快了500%。几乎所有东西都更快,与MongoDB相比至少快了200%。现在的一个例外是需要完全重写整个JSON列的更新——这一点MongoDB处理得更好。

JSONB上的gin索引听起来很棒。

PostgreSQL还将在内部持久化JSONB类型,并将其与数字、文本、布尔等类型进行匹配。

使用JSONB也可以实现连接。

为存储过程添加PLv8,这将是Node.js开发者的梦想成真。

由于它被存储为二进制,JSONB还将删除所有空白,改变属性的顺序,并使用属性的最后一次出现来删除重复的属性。

除了索引,当查询JSONB列和JSON列时,PostgreSQL不需要实际运行将每一行的文本转换为JSON的功能,这可能会节省大量的时间。

json和jsonb之间的区别的简单解释(原始图片来自PostgresProfessional):

SELECT '{"c":0,   "a":2,"a":1}'::json, '{"c":0,   "a":2,"a":1}'::jsonb;

          json          |        jsonb 
------------------------+--------------------- 
 {"c":0,   "a":2,"a":1} | {"a": 1, "c": 0} 
(1 row)

Json:文本存储«as is» Jsonb:没有空格 没有重复的键,最后一个键获胜 Jsonb:键排序

更多的演讲视频和幻灯片演示由jsonb开发人员。他们还引入了JsQuery,一个pg.extension,提供了强大的jsonb查询语言。

JSONB是JSON的“更好”版本。

让我们来看一个例子:

SELECT '{"c":0,   "a":2,"a":1}'::json, '{"c":0,   "a":2,"a":1}'::jsonb;
          json          |        jsonb 
------------------------+--------------------- 
 {"c":0,   "a":2,"a":1} | {"a": 1, "c": 0} 
(1 row)

JSON stores white space, and that is why we can see spaces when key "a" is stored, while JSONB does not. JSON stores all the values of a key. This is the reason you can see multiple values (2 and 1) against the key "a" , while JSONB only "stores" the last value. JSON maintains the order in which elements are inserted, while JSONB maintains the "sorted" order. JSONB objects are stored as a decompressed binary as opposed to "raw data" in JSON, where no reparsing of data is required during retrieval. JSONB also supports indexing, which can be a significant advantage.

一般来说,应该更喜欢JSONB,除非有特殊的需要,比如对对象键的顺序有遗留的假设。

另一个重要的区别是,json类型没有相等操作符,但jsonb有。

这意味着在从表中选择json类型和/或其他字段时不能使用DISTINCT关键字(您可以使用DISTINCT ON代替,但由于这种情况,它并不总是可行的)。

关于json和jsonb数据类型之间的差异,值得一提的是官方的解释:

PostgreSQL offers two types for storing JSON data: json and jsonb. To implement efficient query mechanisms for these data types, PostgreSQL also provides the jsonpath data type described in Section 8.14.6. The json and jsonb data types accept almost identical sets of values as input. The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead, but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage. Because the json type stores an exact copy of the input text, it will preserve semantically-insignificant white space between tokens, as well as the order of keys within JSON objects. Also, if a JSON object within the value contains the same key more than once, all the key/value pairs are kept. (The processing functions consider the last value as the operative one.) By contrast, jsonb does not preserve white space, does not preserve the order of object keys, and does not keep duplicate object keys. If duplicate keys are specified in the input, only the last value is kept. In general, most applications should prefer to store JSON data as jsonb, unless there are quite specialized needs, such as legacy assumptions about ordering of object keys. PostgreSQL allows only one character set encoding per database. It is therefore not possible for the JSON types to conform rigidly to the JSON specification unless the database encoding is UTF8. Attempts to directly include characters that cannot be represented in the database encoding will fail; conversely, characters that can be represented in the database encoding but not in UTF8 will be allowed.

来源:https://www.postgresql.org/docs/current/datatype-json.html