In most cases, I would use a normalized scheme with an option_tag table that implements a many-to-many relationship between the option and tag tables. Reference implementation here:
This may not be the fastest option in all respects, but it offers a full range of database functionality, including referential integrity, restrictions, a complete set of data types, all index parameters, and cheap updates.
For completeness, add to the list of options:
hstore (good option)xml more verbose and more complex than hstore or jsonb , so I would use it only when working with XML.- "comma separated string" (very simple, mostly bad option)
- EAV (Entity-Attribute-Value) or โname-value pairsโ (mostly a bad option)
Details on this related question on dba.SE:
If the list is for display purposes only and rarely updated, I would consider a simple array that is usually smaller and works better for this than the rest.
Read Josh Berkus's @a_horse blog post related to his comment. But keep in mind that it focuses on selected reading cases. Josh admits:
I understand that I have not tested the comparative write speeds.
And that where the normalized approach wins big, especially when you change separate tags under parallel loading.
jsonb is only a good option if you are going to work with JSON anyway and can store and retrieve JSON "as is".
source share