In MySQL, the query you wrote will be slower than any other way to find this information. Perhaps slower than asking each person individually. Your request:
select u2 from friends where u1 = userA and u2 IN (select u2 from friends where u1 = userB)
Has a subquery in an IN clause. MySQL evaluates the query for each row encountered. The best way to write this:
select u2 from friends where u1 = userA and exists (select 1 from friends where u1 = userB limit 1)
If your data all fits on the same server and fits into memory, the performance of an optimized MySQL query should be good. Sites like LinkedIn and FaceBook have many problems - constant network updates, significantly large amounts of data, various types of links, etc. Your simple example does not reflect what they are doing. But many of their analyzes use Hadoop or Hadoop in conjunction with relational databases.
source share