PostgreSQL刚刚在9.4版中引入了JSONB,它已经成为了黑客新闻的热门话题。它与之前在PostgreSQL中出现的Hstore和JSON有什么不同?
它的优点和局限性是什么?什么时候应该考虑使用它?
PostgreSQL刚刚在9.4版中引入了JSONB,它已经成为了黑客新闻的热门话题。它与之前在PostgreSQL中出现的Hstore和JSON有什么不同?
它的优点和局限性是什么?什么时候应该考虑使用它?
当前回答
Peeyush:
简单的回答是:
如果你在PostgreSQL中做很多JSON操作,比如排序、切片、拼接等,出于速度考虑,你应该使用JSONB。 如果需要对JSON上的任意键搜索进行索引查找,那么应该使用JSONB。 如果以上两种方法都不使用,则可能应该使用JSON。 如果需要保留键顺序、空格和重复的键,则应该使用JSON。
想要得到更长的答案,你需要等我在9.4发行版之前写一篇完整的“HowTo”文章。
其他回答
Peeyush:
简单的回答是:
如果你在PostgreSQL中做很多JSON操作,比如排序、切片、拼接等,出于速度考虑,你应该使用JSONB。 如果需要对JSON上的任意键搜索进行索引查找,那么应该使用JSONB。 如果以上两种方法都不使用,则可能应该使用JSON。 如果需要保留键顺序、空格和重复的键,则应该使用JSON。
想要得到更长的答案,你需要等我在9.4发行版之前写一篇完整的“HowTo”文章。
据我所知
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
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代替,但由于这种情况,它并不总是可行的)。