Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should HStoreField be used instead of JSONField?

Tags:

Django 1.8 provides HStoreField and Django 1.9 will provide JSONField (which uses jsonb) for PostgreSQL.

My understanding is that hstore is faster than json, but does not allow nesting and only allows strings.

When should one be used over the other? Should one be preferred over the other? Is hstore still the clear winner in performance compared to jsonb?

like image 822
mcastle Avatar asked Sep 25 '15 23:09

mcastle


People also ask

When to use JSONField Django?

Typically I use a JSONField in two cases: To save a response from 3rd party APIs (e.g. as an audit trail) To save references to archived objects (e.g. when the live products in my db change but I still have orders referencing the product).

What is Django JSONField?

One of the cool features that was introduced in Django 3.1 was the JSONField . JSONField allows for you to store semi-structured data alongside other data fields in PostgreSQL, MySQL, and SQLite databases and supports introspection, lookups, and transforms.


1 Answers

If you need indexing, use jsonb if you're on 9.4 or newer, otherwise hstore. There's really no reason to prefer hstore over jsonb if both are available.

If you don't need indexing and fast processing and you're just storing and retrieving validated data, use plain json. Unlike the other two options this preserves duplicate keys, formatting, key ordering, etc.

like image 124
Craig Ringer Avatar answered Nov 01 '22 03:11

Craig Ringer