2

I am having 4-5 fql queries in single function. Each of them taking 2-4 seconds to execute. Total 14-15 seconds are required to execute that whole function. User required to wait for long time. So I want to reduce that processing time. ( There is not well supported multi-threading concept in PHP.)

I have heard of batch request concept in graph api. And I have googled a lot but didn't understand how to use batch request for fql queries in graph api.

  • Can anyone give explanation with example for using batch request of fql queries ?
  • By what time query processing time will reduce?
  • Is there any another method to reduce time of many fql queries?
Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226

3 Answers3

7

Updated:

https://graph.facebook.com/?batch=[{"method":"GET","relative_url":"me"},{"method":"GET","relative_url":"me/friends?limit=50"}]&access_token=ACCESS_TOKEN&method=post

More info here: http://developers.facebook.com/blog/post/2011/03/17/batch-requests-in-graph-api/

Alexander Nenkov
  • 2,910
  • 20
  • 28
  • Please do check competing answers. thermz given me that solution only. I want batch request of fql queries in graph api. Batch request feature is given by graph api. – Somnath Muluk Mar 09 '12 at 10:55
  • I hope this one better suits your question – Alexander Nenkov Mar 09 '12 at 11:10
  • I also have read that. That gives batch request of api methods. Please can you give solution for batch request of fql queries in graph api? I am interested in that. – Somnath Muluk Mar 09 '12 at 11:26
  • Sorry but I don't understand what you want. Using multiquery is exactly that - batch request of fql queries. You get the data of N queries in a single call. – Alexander Nenkov Mar 09 '12 at 11:48
  • :There is no any relation between my those 4-5 queries. In FQL Multiquery execution of queries from 1st to last. I have tried FQL Multiquery this also it gives same processing time. So I want to do by batch request which works same as multithreading on facebook side. – Somnath Muluk Mar 09 '12 at 11:55
  • 1
    This is from their site: 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. – Alexander Nenkov Mar 09 '12 at 12:02
4

//$current_user=facebook id

 $query1="SELECT uid, name FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user)";
 $query2="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user )";
 $query3="SELECT uid, name, work, education FROM user WHERE uid = $current_user";
 $queries = array(
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query1)),
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query2)),
           array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query3))
            );

            $objs = $facebook->api('/?batch='.json_encode($queries), 'POST');

$objs gets json array of whole result of thre queries.

And it is saving time a lot. This 3 queries individually takes total 9 seconds. With multiquery it takes 7 seconds. And with batch request it takes 3.6 seconds.

Somnath Muluk
  • 55,015
  • 38
  • 216
  • 226
2

Instead of calling each FQL query separately, why not use FQL Multiquery?

http://developers.facebook.com/docs/reference/rest/fql.multiquery/

UPDATE Otherwise, if you don't want do use multiquery I think what you're looking for is here. I couldn't find anything more about FQL and Batch.

curl \
     -F 'access_token=…' \
     -F 'batch=[{ "method": "POST", \
    "relative_url": "method/fql.query?query=select+name+from+user+where+uid=4", \
     }]
https://graph.facebook.com
thermz
  • 2,386
  • 3
  • 20
  • 28
  • There is no any relation between my those queries. In `FQL Multiquery` execution of queries from 1st to last. I have tried `FQL Multiquery` this also it gives same processing time. So I want to do by batch request which works same as multithreading on facebook side. – Somnath Muluk Mar 09 '12 at 10:22
  • Ok, anyway it's impossible that's it's not better in time execution, for the simple reason that you make 1 HTTP GET instead of 4-5. Perhaps not much but it is surely better ask all in one call instead of more. – thermz Mar 09 '12 at 10:30
  • I am not getting anything! Can you please elaborate more? – Somnath Muluk Mar 09 '12 at 10:50
  • 2
    if (you use Multiquery){ you call facebook only **once**, and this reduce HTTP overhead } – thermz Mar 09 '12 at 10:54
  • 2
    else { you call facebook FQL api 4-5 times, increasing the HTTP overhead } – thermz Mar 09 '12 at 10:54
  • Yes I want to reduce that `HTTP overhead`. I have tried both methods. And checked by benchmarking in codeigniter. It is having hardly 1-2 second difference. Is it possible to reduce `HTTP overhead` more? – Somnath Muluk Mar 09 '12 at 11:29
  • No way: HTTP overhead depends on how many HTTP request you issue on facebook API. I just updated my answer to come more closer to what you're asking for. – thermz Mar 09 '12 at 12:27
  • Is that possible by PHP-SDK? I am unable to use curl. – Somnath Muluk Mar 09 '12 at 13:00
  • I have accepted your answer because it gave some hint towards solution. – Somnath Muluk Mar 09 '12 at 14:02
  • Why? Can you explain a bit better? – thermz Apr 01 '12 at 02:15