0
votes

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

1 Answers

1
votes

It depends a bit if you want a true foreign key or not. You can't actually create a foreign get to 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.

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.