title: Wondering how Facebook does the "Mutual friends" feature
body: <p>I'm currently developing an application to allow students to manage their courses, and I don't really know how to design the database for a specific feature.
  The client wants, a lot like Facebook, that when a student displays the list of people currently in a specific course, the people with the most mutual courses with the logged in user are displayed first. Almost the same as Facebook feature "Friend suggestions" with an additional filter.</p>
  
  <p>As an additional feature, I would like to add a search feature to allow students to search for another one, and displaying first in the search results the people with most mutual courses with the logged in user.</p>
  
  <p>I currently use MySQL, I plan to use Cassandra for some other features, and I also use Memcached for result caching and Sphinx for the search.</p>
  
  <p>Thanks.</p>
  
  <p>--</p>
  
  <p>The app is developed in Python, BTW</p>
  
  <p>And I forgot to mention that the standard approach (using a nice MySQL query to calculate all this with an ORDER BY clause) is wayyyys too slow. So as reads are a lot more frequent than reads, I would like most of the logic to take place once, when the relation people &lt;-> course is added.</p>
  
  <p>I thought about updating a "mutual courses" counter specific to one tuple (user, course) that will be increased for all users of a course when the logged in user joins a new course (or decreased when he leaves it).</p>