I'm looking at using PostgreSQL's jsonb
column type for a new backend project that will mainly serve as an REST-ful JSON API. I believe that PostgreSQL's jsonb
will be a good fit for this project as it will give me JSON objects without need for conversion on the backend.
However, I have read that the jsonb
data type slows down as keys are added, and my schema will have need of using primary keys and foreign key references.
I was wondering if having primary keys/foreign keys in their own columns (in the standard relational database way) and then having a jsonb
column for the rest of the data would be beneficial, or would this cause problems (whether now or down the road)?
In short, would:
table car(id int, manufacturer_id int, data jsonb)
perform better or worse than:
table car(data jsonb)
Especially when looking up foreign keys frequently?
Would there be downsides to the first one, from a performance or a schema perspective?
Because JSONB stores data in a binary format, queries process significantly faster. Storing data in binary form allows Postgres to access a particular JSON key-value pair without reading the entire JSON record. The reduced disk load speeds up overall performance. Support for indexing.
Most applications should use JSONB for schemaless data. It stores parsed JSON in a binary format, so queries are efficient.
Yes, you should. Foreign keys are just constrains which helps you to make relationships and be sure that you have correct information in your database. You should use them to prevent incorrect data entry from whatsoever. Save this answer.
In general, most applications should prefer to store JSON data as jsonb , unless there are quite specialized needs, such as legacy assumptions about ordering of object keys. RFC 7159 specifies that JSON strings should be encoded in UTF8.
All values involved in a PRIMARY KEY
or FOREIGN KEY
constraint must be stored as separate columns (best in normalized form). Constraints and references do not work for values nested inside a json
/ jsonb
column.
As for the rest of the data: it depends. Having them inside a jsonb
(or json
) value carries the well-known advantages and disadvantages of storing unstructured document-type data.
For attributes that are present for all or most rows, it is typically better (faster, cleaner, smaller storage) to store them as separate columns. Especially simpler and cheaper to update. Easier indexing and other queries, too. The new jsonb
has amazing index capabilities, but indexing dedicated columns is still simpler / faster.
For rarely used or dynamically appearing attributes, or if you want to store and retrieve JSON values without much handling inside the DB, look to jsonb
.
For basic EAV structures with mainly character data, without nesting and no connection to JSON I would consider hstore
. There are also the xml
(more complex and verbose) and json
data types (mostly superseded by jsonb
), which are losing ground.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With