简体   繁体   中英

Implement a many to many relation ships in postgresql

I have to implement a many to many relationships between table in postgresql.

The default SQL way to do this is to create :

  • data1
  • data2
  • relation1 (which is a virtual link between data1 and data2)

As I am in postgresql I can do this in three differents way :

  • data1 / data2 / relation1 (as described above)
  • data1 / data2, but with an array in data1 or data2 to store links
  • data1 / data2, but with a json in data1 or data to store links

Which implementations will be faster ?

Regards,

PS : Usage is to create a json representation of data1 containing all data2

It depends on how you will end up using them.

  • The first one: data1/data2+relation is a "general purpose" implementation that will be fast for most uses. It's focused on the generic modeling of entities and its flexibility. The great thing about this option is that it will more easily survive future [unpredictable] changes in the model or application.

  • The second one data1/data2+array , and third one data1/data2+json are special cases that can be very fast for a reduced number of cases. If you need to optimize these ones, then these solutions can be great. However, they won't address very well other cases, and they could become real slow for general queries.

Bottom line: I would [very personally] go with the first one, unless I have a real strong reason to use one of the other ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM