views:

34

answers:

2

I am developing a facebook type application for my institute.

and I am stuck at the friends module. i.e. How to know if the particular users are one's friends.

I googled a lot but didn't get any satisfactory answers. What I got is : there will be many friends of a person and implementing users and their friends in seperate table will only increase redundancy and large DB size.

I thought of using a graph with vertices as users and edges as connection .

But how to implement something like that in db.

Or How Facebook handles such huge amount of relationships?

+1  A: 

Personally, I would have a dedicated table for it:

You could have a table with just two columns: userID and friendID

Since the relationships between users in the db will be many-to-many, normalizing it requires a link table which breaks it into many-to-one-to-many

http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html#03

kskjon
Well I have thought about this approach earlier... but it only leads to more data , more headaches and more programming.
Ashutosh Singh
@Ashutosh This is the best solution. All others would bring even more headache and programming. Many to many is never easy to work with.
serg
A: 

This kind of problems are usually solved by using a different type of database. For a social network, a graph database should make sense, as nodes and relationships are first class citizens in it. There's a social network example for the Neo4j graph database, the full source code of the example is included in the standard dowload package. I've also written a blog post on this theme, with another example as starting point.

nawroth