Can PostgreSQL array be optimized for join?

ⅰ亾dé卋堺 提交于 2019-12-02 07:34:37
Erwin Brandstetter

No, storing FKs in an array is never a good idea for general purpose tables. First an foremost, there is the fact you mentioned in passing: Foreign key constraints for array elements are not implemented (yet). This alone should void the idea.

There was an attempt to implement the feature for Postgres 9.3 that was stopped by serious performance issues. See this thread on pgsql-hackers.

Also, while read performance can be improved with arrays for certain use cases, write performance plummets. Think of it: To insert, update or delete a single element from a long array, you now have to write a new row version with the whole array for every canged element. And I see serious lock contention ahead, too.

If your table is read only, the idea starts to make more sense. But then I would consider a materialized view with denormalized arrays on top of a normalized many-to-many implementation:

While being at it, the MV can include all join tables and produce one flat table for even better read performance (for typical use cases). This way you get referential integrity and good read (and write) performance - at the cost of the overhead and additional storage for managing the MV.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!