简体   繁体   中英

Postgres JSONB table querying structure?

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 } |

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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