0

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.