PostgreSQL引入JSONB的解释 [英] Explanation of JSONB introduced by PostgreSQL

查看:17
本文介绍了PostgreSQL引入JSONB的解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 texts (however values can be sql NULLs too).

json &jsonb 允许你存储一个有效的 JSON value(定义在它的 spec).

Both json & jsonb allows you to store a valid JSON value (defined in its spec).

F.ex.这些是有效的 JSON 表示:nulltrue[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, while
  • jsonb is stored in some binary representation

这样做有 3 个主要后果:

There are 3 major consequences of this:

  • jsonb 通常需要比 json 更多的磁盘空间来存储(有时不会)
  • jsonbjson
  • 花费更多时间从其输入表示中构建
  • json 操作比 jsonb 花费明显更多的时间(每次在 json 类型值)
  • jsonb usually takes more disk space to store than json (sometimes not)
  • jsonb takes more time to build from its input representation than json
  • json operations take significantly more time than jsonb (& parsing also needs to be done each time you do some operation at a json 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:

  1. 如果您只在应用程序中使用 JSON 表示,则 PostgreSQL 仅用于存储 &检索此表示,您应该使用 json.
  2. 如果你在PostgreSQL中对JSON值做了很多操作,或者对一些JSON字段使用索引,你应该使用jsonb.

这篇关于PostgreSQL引入JSONB的解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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