Skip to content

Instantly share code, notes, and snippets.

Created January 10, 2018 02:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/d0214fa75bf55423f57f50c6bc2157c6 to your computer and use it in GitHub Desktop.
Save anonymous/d0214fa75bf55423f57f50c6bc2157c6 to your computer and use it in GitHub Desktop.
Database Structure For Facebook
********************
Database Structure For Facebook ->->->->
********************
http://shurll.com/c0147
(Copy & Paste link)
********************
Whenever a user adds another user as a friend, two new rows are created. For distributed data, they use a non-relational technology called Map/Reduce, included in the Hadoop project. MySQL partitioning? –veidelis Jun 4 '14 at 7:30 2 you can be sure that facebook does not use a RDBMS for this, it is common knowledge that they, twitter and everyone else that needs to run queries like this use a graph database of some flavor. Why? Good, motivating examples of recursion How to partially extract zipped huge plain text file? Should I make my character suspect an upcoming twist or not? Who first used the word "Simplex"? How do I make a single face of an object a particle emitter? Could you strap shotguns to the back of a centaur to be fired during a charge? The length of coil winding on cylinder. –Dirk Vollmar Jun 17 '09 at 19:42 1 divo: clever use of indexes and partitions. If the average user has 100 friends, that means the table would contain 100'000'000'000 rows. –Eric J. It's back! Take the 2018 Developer Survey today . Why am I seeing this? Security Check This is a standard security test that we use to prevent spammers from creating fake accounts and spamming users. –Sasha Chedygov Jun 17 '09 at 19:25 3 freak: Why? The entire concept of voting on this site is for voting to be anonymous. But I'd also put some sort of non-composite identity as the PK with a nonclustered index. But the OrientDB documentation explains the friend connections and everything else can be modelled once the basics are understood. She was soon joined by co-admins Bill Slawski, Ammon Johns and Jill Whalen. I could not find anything on what software / DB they actually use for their cached graphs Let's take a look at this, friend connections are top left: Well, this is a graph. It's probably the best explanation of how FB works behind the scenes you can find. There's a longer paper available at shareimprove this answer answered Jun 28 '13 at 18:07 James Sherwin-Smith 6111 add a comment up vote 5 down vote You're looking for foreign keys. asked 8 years, 6 months ago viewed 143,048 times active 6 months ago Blog Take the 2018 Developer Survey Get the weekly newsletter! In it, you'll get: The week's top questions and answers Important community announcements Questions that need answers see an example newsletter By subscribing, you agree to the privacy policy and terms of service. We were the first forums to discuss usability and accessibility and allow members to participate in the Website Hospital. Why do you feel malfist is entitled to anything? –GEOCHET Jun 17 '09 at 19:30 3 downvotes should leave a comment as to why. I'll miss you Dad. –Jesse C. For examples where friendship or other relations aren't explicitly two way, you would need to also have those rows to indicate the two-way relationship. However, if this scales linear you're already at 1.8 seconds for just 100k users, 18 seconds for 1 million users. Maybe it can be optimized, I'm not a DB genius (suggestions are welcome). Either cache the friendlists per user or map your relational DB in parts or the whole thing to a graph and query the graph DB. The video and article tells you a few things: They're using MySQL at the very bottom of their stack Above the SQL DB there is the TAO layer which contains at least two levels of caching and is using graphs to describe the connections. The URI you submitted has disallowed characters. Stack Overflow works best with JavaScript enabled .. For instance, suppose my id is 'deep9c' and I add a user having id 'akash3b' as my friend, then two new rows are created in table "frndlist" with values ('deep9c','akash3b') and ('akash3b','deep9c'). SEARCH BY NAME SIMILAR NAMES Data Structures Data Structura Data Structural Datta Structure -->. Here is my disappointing test for just findings friends of friends: DB Schema: CREATE TABLE IF NOT EXISTS friends ( id int(11) NOT NULL, userid int(11) NOT NULL, friendid int(11) NOT NULL ) ENGINE=InnoDB AUTOINCREMENT=2 DEFAULT CHARSET=utf8; Friends of Friends Query: ( select friendid from friends where userid = 1 ) union ( select distinct ff.friendid from friends f join friends ff on ff.userid = f.friendid where f.userid = 1 ) I really recommend you to create you some sample data with at least 10k user records and each of them having at least 250 friend connections and then run this query. more stack exchange communities company blog Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Log In Sign Up . On my machine (i7 4770k, SSD, 16gb RAM) the result was 0.18 seconds for that query. Example schema: Users Table userID PK other data Friends Table userID -- FK to users's table representing the user that has a friend. rev2018.1.9.28319 . Facebook uses MySQL extensively, and contributes patches back to the MySQL project. It's back! Take the 2018 Developer Survey today . This might still sound OKish for 100k users but consider that you just fetched friends of friends and didn't do any more complex query like "display me only posts from friends of friends + do the permission check if I'm allowed or NOT allowed to see some of them + do a sub query to check if I liked any of them" 5a02188284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment