5 ms·
Arrays in Postgres are my guilty pleasure. I know I shouldn’t use them but I just cannot help myself.
by mmcgaha 6y ago
Arrays in Postgres are my guilty pleasure. I know I shouldn’t use them but I just cannot help myself.
- lucian1900 6y agoThey’re great for denormalisation, which is often an appropriate trade off.
- bitexploder 6y agoEven storing JSON blobs isn’t the hit it used to be. It’s not relational, but you can index on fields in the JSON and query it effectively. The Postgres array and other types are great for stuff at the edges of your database or when you know you won’t need to build relations into or on that data. RDBMS like Postgres really provide a powerful, powerful technology for managing data.
- colanderman 6y agoYes! Have one-to-many data that you know you will usually always grab all in one go, and won't ever participate in a relation? Arrays are the way to go. JSONB can be similarly highly appropriate. They can greatly reduce the number of disk reads needed for certain workloads. Don't forget that both can be indexed in Postgres! And the indexes are more powerful than what you can do with the equivalent relational layout, as they support efficient subset queries.
- sk5t 6y agoSame goes for hstore and the @> style operators.
- EdwardDiego 6y agoI've recently used them to replace a many to many relation from table A to table B where the primary query was "give me all As associated with B", so the new table had a schema of unique B, Array<A>, so what was a 20ish second query over several hundred million rows became a 0.3 second query over 600K rows. We did have a use case that wanted to find Bs for a given A, and using a GIN index on the array column along with the PG array contains operator served that up remarkably fast also.