I was wondering whether it is cleaner or faster using arrays of integers instead of creating an extra new table in a m-to-m relationship and using foreign keys to ensure data integrity. What do you think? Do you say using arrays instead is a no-go?
1 Answer
Arrays to implement an m-to-n relationship are a no-go.
Write down the join between the two tables. You will notice that instead of two joins with an
=as join condition, you now have a single join with@>as join condition (or aLATERAL unnestof the array, which is worse). That means that you are reduced to nested loop joins which will be slow if the tables are big (even if you create a GIN index to support the@>).You cannot have referential integrity (foreign key constraints) that way.