JSON Support in PostgreSQL and Entity Framework

JSON 和JSONB的区别(What's difference between JSON and JSONB data type in PosgresSQL?)

  1. When should be used specific one?
  2. What's benefits or disadvantages with respect to other?
  • JSON比JSONB的一个小的潜在好处是,它保留了数据的缩进。所以,如果你非常关心你的JSON的格式,或者有一些需要它在一个 特定的结构,JSON可能是有用的。(One small potential benefit of it over JSONB (which we’ll get to next) is that it preserves the indentation of the data coming in. So if you are extremely particular about the formatting of your JSON, or have some need for it in a particular structure, JSON can be useful.)Postgres的JSON类型只是在文本字段上提供了JSON验证。 如果你正在存储某种形式的日志数据,你很少需要查询,JSON可以正常工作。 因为它非常简单,所以写入吞吐量会更高。 对于任何更复杂的东西,建议使用JSONB。(Postgres’ JSON type simply provides JSON validation on a text field. If you’re storing some form of log data you rarely need to query, JSON can work fine. Because it’s so simple, it will have a lot higher write throughput. For anything more complex, I’d recommend using JSONB, which is covered below.)
  • The B stands for better.JSONB是JSON的二进制表示,这意味着它不仅仅是文本,而是压缩和更高效的存储。它允许嵌套结构,使用基本的数据类型,并有一些内置的函数来处理它。尽管类似于hstore的最好的部分是索引。在JSONB列上创建GIN索引将为该JSON文档中的每个键和值创建一个索引。在文档中嵌套的能力意味着JSONB在大多数情况下优于hstore。

总结:

  JSONB - 在大多数情况下
  JSON - 如果你只是在处理日志,通常不需要查询,并使用更多的审计跟踪
  hstore - 对于基于文本的键值看起来可以正常工作,但是一般来说JSONB在这里仍然可以工作得很好

参考文章:

1.Entity Framework: Storing complex properties as JSON text in database

2.PostgreSQL Data Types——JSON

3.When to use unstructured datatypes in Postgres–Hstore vs. JSON vs. JSONB重点