Please take a look at this query and try to give me any other ideas that will give the exact same results more efficiently.
SELECT username
FROM users
WHERE username NOT IN (
SELECT DISTINCT username
FROM users, friends
WHERE 'user1' IN (you,friend,username)
AND you IN ('user1',username)
AND friend IN ('user1',username))
AND username <> 'user1'
Here's a live demo.
EXPLAIN SELECT username FROM users WHERE…
and check that the appropriate indexes are in place. \$\endgroup\$you = 'user1'
andfriend = 'user3'
and another forfriend ='user4'
...I am talking about the fiddle now. maybe it is just a subset of the data in the table. \$\endgroup\$