views:

97

answers:

2

I found out two possible solutions for implementing the database structure for social networking sites like Facebook.

1.: Creating a 'Relationships' table and inserting every friendship into it. For example: user A adds B as friend (A-B), then the logic puts (A-B) and (B-A) into the 'Relationships' table. Then it indexes the first attribute.

2.: Creating a unique table for all the users containing friends. Most databases work with nearly 2 billion unique tables, so it won't be a problem; however, the database size will be nearly 300 times bigger (expecting 300 friends average per user). In this scenario, querying friends would not be a problem (as simple as SELECT * FROM)

Any ideas? Am I wrong somewhere? Thanks all.

+3  A: 

The table-per-user solution that you are describing sounds basically like Oracle's partitions feature.

Not exactly related, but I recommend this awesome post: Presentation Summary “High Performance at Massive Scale: Lessons Learned at Facebook”

I think that the friendships table is the least of their concerns :)

Yoni
There's an excellent article here on structuring a database for the social graph: http://techportal.ibuildings.com/2009/09/07/graphs-in-the-database-sql-meets-social-networks/
Karl B
+1  A: 

You don't have to worry about maximum table size and stuff like that. In order to create a site like Facebook you have to shard/partition all your tables to multiple machines anyway.

truppo