PostgreSQL 引入的 JSONB 说明 [英] Explanation of JSONB introduced by PostgreSQL

查看:31
本文介绍了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 模块,它只允许你存储键 => 值对,其中键和值只能是 texts(但是值可以是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).

jsonjsonb 允许您存储有效的 JSON (在其规范中定义).

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天全站免登陆