开发者

SQL table for friends, mutual friends, friends friends etc

开发者 https://www.devze.com 2023-02-03 02:40 出处:网络
I was wondering what the easiest method would be for me in the future to make queries both effective and less complex.

I was wondering what the easiest method would be for me in the future to make queries both effective and less complex.

Should I save a two way relationship like

from_id=1, to_开发者_如何转开发id=2 from_id=2, to_id=1

or just create one unique row

from_id=1, to_id=2

any thoughts?


I would suggest to go for two way relationship. It's flexible ans it's only extra work while inserting and deleteing the records.

The benefits that I see is:

  • To get all friend of a person all you need to do is where from_id=userid instead of where from_id=userid or to_id=userid that later is expensive.
  • you can keep extra metadata in forward and reverse relationship. Let's say you have a case like userA is friend of userB while userB does not approves the relationship. To do this you may have an extra isApproved and set it true for from_id=userA, to_id=userB and false for from_id=userB, to_id=userA -- this will allow further cheap sort and select.

    The effect in single row will be a little trickier.
0

精彩评论

暂无评论...
验证码 换一张
取 消