24
Suppose I want to store relationships among the users of my application, similar to Facebook, per se.
That means if A is a friend(or some relation) of B, then B is also a friend of A. To store this relationships I am currently planning to store them in a table for relations as follows
UID FriendID
------ --------
user1 user2
user1 user3
user2 user1
However I am facing two options here:
- The typical case, where I will store both
user1 -> user2anduser2->user1. This will take more space, but (at least in my head) require just one pass over the rows to display the friends of a particular user. - The other option would be to store either
user1->user2ORuser2->user1and whenever I want to find all the friends ofuser1, I will query on both columns of table to find a user's friends. It will take half the space but (again at least in my head) twice the amount of time.
First of all, is my reasoning appropriate? If yes, then are there any bottlenecks that I am forgetting (in terms of scaling / throughput or anything)?
Basically, are there any trade-offs between the two, other than the ones listed here. Also, in industry is one preferred over the other?