Postgres JSONB table querying structure?

时间:2016-04-04 17:53:33

标签: json postgresql psql jsonb

So I'm building a table/database in Postgres using its JSONB feature. 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.

An example:

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

Or simply:

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

1 个答案:

答案 0 :(得分:1)

如果你想要一个真正的外键,这取决于一点。你实际上无法创建一个外国人获得json->userId。如果您希望能够为快速连接编制索引,则可以在json->userId上创建功能索引,也可以在整个文档上创建GIN索引。

如果你确实想要创建一个外键并且有适当的约束,那么它值得将它实现到一个实际的列。