SQL table for friends, friends, friends, etc.

I was wondering what an easy way for me in the future to make queries efficient and less complicated.

Should I maintain a bilateral relationship, for example

from_id = 1, to_id = 2 from_id = 2, to_id = 1

or just create one unique line

from_id = 1, to_id = 2

any thoughts?

+3
source share
1 answer

I would suggest going on a bilateral relationship. It is flexible, and it's just more work, and inserting and deleterecording.

The benefits that I see:

  • To get all the friends of a person, all you have to do is where from_id=useridinstead where from_id=userid or to_id=userid, which is later expensive.
  • . , userA is friend of userB while userB does not approves the relationship. isApproved true from_id=userA, to_id=userB from_id=userB, to_id=userA - sort select

    . .
+7

Source: https://habr.com/ru/post/1784409/


All Articles