Reputation: 301
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.
Upvotes: 1
Views: 1927
Reputation: 27295
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.
Upvotes: 1
Reputation: 10321
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 :)
Upvotes: 3