Take the 2-minute tour ×

I'm trying to select all mutual friends' connections with PHP/FQL. Using my UID (540 friends), which means >12,000 connections, of which >6500 are unique. So this code should return all the connections but Facebook apparently has a 4999/5000 row limit on FQL queries.

// select mutual unique friends
 $unique_connections = $facebook->api_client->fql_query("

  SELECT uid1, uid2 FROM friend 
   WHERE uid1 IN 
   (SELECT uid2 FROM friend WHERE uid1=$uid)
   AND uid2 IN 
   (SELECT uid2 FROM friend WHERE uid1=$uid)

I know the numbers above because the original code I wrote loops through my friend list and sends a getMutualFriend query for each of them.

foreach ($friends as $key) 
    $mutual_friends = $facebook->api_client->friends_getMutualFriends($key);
    foreach ($mutual_friends as $f_uid)
        array_push($all_connections, array($key,$f_uid)); 

Of course it takes almost 3 minutes to run that script, while the FQL query returns in 5 seconds. After an hour of searching for this answer I've come to the conclusion the only way to get around this is to use a mixture of the two methods. Well that, and post here. Any ideas on a better way to write this script and beat the 4999/5000 row limit?

Here's an fql_multiquery that should do the same as above. It is also limited to 4999/5000.

$queries = '{
"user_friends":"SELECT uid2 FROM friend WHERE uid1 = '.$uid.'",
"mutual_friends":"SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM #user_friends) AND uid2 IN (SELECT uid2 FROM #user_friends)"

$mq_test = $facebook->api_client->fql_multiquery(trim($queries));
share|improve this question
Do you want friends of friends or mutual friends? The number of unique mutual friends for all your friends is probably the most pointless statistic I could pull from the FB API. Facebook's API only allows you to lookup friends of the logged in user, so you can't get Friends of Friends. –  Matt Nov 26 '10 at 14:43
I meant Mutual friends; I'll attempt to correct that in the title. The pointlessness depends on your perspective. I created an app for people to export their data and getting mutual friends (presumably for visualization) is the most-requested feature by far. –  ow3n Nov 26 '10 at 17:56
You should be able to achieve that using batch requests and the mutualfriends connection from the Graph API. developers.facebook.com/docs/reference/api/batch I would post a full answer with an example when I have the time. –  Haralan Dobrev Jul 19 '12 at 11:26
add comment

7 Answers

up vote 7 down vote accepted

So, I'm posting the answer to my original question. I was able to circumvent the 5000 row limit on FQL queries by chunking the array of UIDs (using the appropriately-named array_chunk() PHP function) and looping through the chunks to execute mini-queries, and then appending it all back into one array. The whole script averages 14 seconds for over 12,000 rows so that is a huge improvement. You can see the application at work here: givememydata.com

Oh, and Facebook should reconsider their (still undocumented) FQL row limit. What is more taxing on their servers? A single query that executes in 5 seconds or 500 queries that take 180 seconds? Sorry, had to vent. ;-)

share|improve this answer
I'm on the same boat! Querying link_stat table for total_count field and it stops on 500 results! I'm going to cron these queries but I should test the smallest interval between them. –  brainondev Apr 19 '12 at 15:51
add comment
$mutual_friends = $facebook->api('/me/mutualfriends/friendid');
share|improve this answer
Nice of them to add that. –  ow3n Aug 13 '12 at 21:53
add comment

An alternative would be to use the fql.multiquery method, and build a seperate FQL query for each friend (or a group of friends per FQL query), but still sending all queries in the same request.

share|improve this answer
So the total of the multiquery is not limited? I seem to have gotten past the weird whitespace issues with a test but can't seem to repeat my original query with the multiquery. –  ow3n Nov 26 '10 at 16:56
Ok, I figured out the multiquery code but it is also limited. I pasted it above. Any thoughts? –  ow3n Nov 26 '10 at 17:50
@ow3n: Each FQL query within the multiquery is still limited, but you split the query into smaller queries: so instead of running one big query for all 400 friends, run 10 queries each acting on behalf of 40 friends. –  Matt Nov 26 '10 at 18:49
I actually found that the entire multiquery to be limited as well. Their documentation is very vague, and while it suggests what you are saying, I was unable to get it to work. –  ow3n Nov 27 '10 at 23:23
add comment

An interesting observation: When I try to find all the people that have mutual friends me I user the following query

SELECT uid1, uid2 FROM friend WHERE uid1 IN (SELECT uid2 FROM friend WHERE uid1=$uid)

As you can see it is very similar to your query, except that I removed the AND clause.

I get the following error message: "Can't lookup all friends of 208733. Can only lookup for the logged in user or the logged in user's friends that are users of your app".

I guess facebook is smart enough to figure out that I am trying to do something that it doesn't want me to do. It somehow detects the fact that you are trying to find only your friend's friends that are also your friends, while I am trying to find all of my friend's friends.

share|improve this answer
add comment

FQL supports a LIMIT, just like regular SQL. You may try that. http://developers.facebook.com/docs/guides/performance

Otherwise, I suggest getting the friend IDs for each user, storing those in a SQL table, then performing your own join to get the crossover. You may be able to just get the friends lists once and then subscribe to realtime updates to keep your lists up to date. http://developers.facebook.com/docs/api/realtime

share|improve this answer
Facebook doesn't specifically say if they support the 0,100 LIMIT syntax, but that's what I was suggesting to get past the 5K limit. –  Brent Baisley Nov 26 '10 at 18:35
Gotcha. I tried LIMIT in all of the above but sadly it did not work. –  ow3n Nov 27 '10 at 23:22
add comment

One trick it seems i was able to use is to limit the no of queries based on one of the indexable columns from your tables ( using strpos(column_name, character / number)) .


$fql = "SELECT pid,src_big,owner FROM photo WHERE album_object_id IN 
      (SELECT object_id FROM privacy WHERE 
             ( object_id IN ( SELECT object_id FROM album WHERE owner IN (SELECT uid2 FROM friend WHERE 
                   ( uid1 = " . $this->nextUser->fbid . " AND **strpos(uid2,2) = 1**  )))                                   
           AND ( value = 'FRIENDS_OF_FRIENDS' OR value = 'EVERYONE'  ) )) ;

And this way you can split it in 10 subqueries, or for a alphnumeric field in 33.

share|improve this answer
add comment

I was doing a similar thing- but getting fb page posts- and ran into funny caching on the FB servers when I pummeled it with multiqueries - batching FQL is a way to get around this, btw. And yeah, ran into the 5K limit and just batched under 5k and had to set up paginating, which was a pain.

share|improve this answer
add comment

Your Answer


