Assumptions
- If A is a friend of B, B is also a friend of A.
I searched for this question and there are already lots of questions on Stack Overflow. But all of them suggest the same approach.
They are creating a table friend
and have three columns from
, to
and status
. This serves both purposes : who sent friend request as well as who are friends if status
is accepted.
But this means if there are m
users and each user has n
friends, then I will have mn
rows in the friends table.
What I was thinking is to store friends list in a text
column. For every user I have a single row and a friends
column which will have all accepted friends' IDs separated by a character, say |
which I can explode to get all friends list. Similarly, I will have another column named pending requests
. When a request is accepted, IDs will move from pending requests
to friends
column.
Now, this should significantly reduce the entries in the table and the search time.
The only overhead will be when I will have to delete a friend, I will have to retrieve the friend string, search the ID of the friend to be deleted, delete the ID and update the column. However, this is almost negligible if I assume a user cannot have more than 2000 friends.
I assume that I will definitely be forgetting some situations or this approach will have certain pitfalls. So please correct if so.