Boosting the Power of FQL
by Julie Tung - June 16, 2009 at 3:21pm

Many of you already use Facebook's query language, FQL, to quickly retrieve Facebook data and integrate it into your application experience. For the uninitiated, FQL uses a familiar, SQL-style interface that gives you a powerful way to express exactly what data you want to fetch. For example, you can fetch specific fields from a given data table, or only rows that match specific constraints, reducing the amount of data that needs to be returned and parsed. You can also perform complex queries, like returning data for all of a user's friends.

Now we're taking steps to make FQL even more powerful.

We've just released a new Platform API method, fql.multiquery, which lets you send a batch of queries that Facebook processes all at once. What's more, you can use the results from query A, for example, in query B, even when both queries are included within the same fql.multiquery call. Simply specify the name of query A as if it were a virtual table when you formulate query B. You can format these queries as you normally would, so you can filter by a WHERE clause, and use ORDER BY and LIMIT, for example.

Say you want to display some profile information of friends who've posted on a user's Wall. Previously, you'd have to run two separate queries. First, you'd have to get all the posts from the user's stream from that user's friends. Then you'd have to extract the actor IDs from the results of the first query before you could make a second query to get the user data about the people who posted to the Wall. Now, with fql.multiquery, you can get all this information with one call. Here's the exact syntax of what you'd pass as the queries parameter in fql.multiquery:

$queries = {
  "user_stream":"SELECT post_id, actor_id, message FROM stream
    WHERE source_id=UID",
  "actor_info":"SELECT uid, name, pic_square FROM user 
    WHERE uid IN (SELECT actor_id FROM #user_stream)"
}

$facebook->api_client->fql_multiquery($queries);

Thus, fql.multiquery reduces the number of API calls you need to make to Facebook. Even if you use the existing batch.run method to perform multiple simultaneous queries, you'd still have to wait for the results of a given query before you could use those results in another query. Also, fql.multiquery can perform better than batch.run as it batches more intelligently.

You can order the queries in the call as you see fit; Facebook processes the queries in parallel as much as possible.

Finally, we're continuing to improve FQL performance on our back end, so your queries should complete even faster than before.

You can read more about fql.multiquery on the Developer Wiki. The method will appear in the PHP client library and in the API test console after tonight's push. And, as always, we welcome your comments and feedback in our Developer Forum.