简体   繁体   English

Postgres JSONB表查询结构?

[英]Postgres JSONB table querying structure?

So I'm building a table/database in Postgres using its JSONB feature. 因此,我正在使用其JSONB功能在Postgres中构建表/数据库。 My question is about how to structure the table/query against it. 我的问题是关于如何针对它构造表/查询。 Is it faster to include a separate relational column featuring a foreign key relating to something like users? 包括具有与用户之类的外键的独立关系列是否更快? Or just store a user in the JSON data and include that in your JSON query. 或者只是将用户存储在JSON数据中,然后将其包含在JSON查询中。

An example: 一个例子:

|   userID   |           json         |
|     1      |     { 'userID': 1,     |
|            |       'something': 5 } |

Or simply: 或者简单地:

|         json        |
|   { 'userID': 1,    |
|    'something': 5 } |

It depends a bit if you want a true foreign key or not. 是否要使用真正的外键取决于一点。 You can't actually create a foreign get to json->userId . 您实际上无法创建json->userId的外部获取。 If you want to be able to index against it for fast joins, you can absolutely create a functional index on json->userId or you can create a GIN index on the entire document. 如果您希望能够对其进行索引以进行快速连接,则可以绝对在json->userId上创建功能索引,也可以在整个文档上创建GIN索引。

If you do actually want to create a foreign key and have appropriate constraints there then it's worth materializing it out to an actual column. 如果您确实想创建一个外键并具有适当的约束,那么值得将其具体化为实际的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM