Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graph API vs FQL which is faster?

just wondering when it comes to just the basic GET/POST. (i.e something simple as getting the users profile and not something complicated like getting a pictures from every album)

which is faster? Graph or FQL.

i know FQL is newer than graph, but unsure if its more efficient.

like image 408
nate Avatar asked Oct 03 '11 23:10

nate


2 Answers

It's in no way comparable like this. While you can get same data (in some cases) from both their intents are completely different: FQL is a query language, Graph is an API.

Calling to FQL query if using new JS SDK is a Graph API call itself...

You can (and probably should, if you worry about real comparison and not theoretical speculation) compare speed of calls returning same data but you should take into account some things:

  • Graph API still sucks at data filtering conditions and aggregation of data
  • FQL just can't provide you too many of functionality required in today's applications (like Real-Time updates)
  • Graph API have an ability batch calls, which may speed up things a lot, this also can be used to call fql.query and fql.multiquery (in a bit cumbersome way).
  • Graph API rocks at data filtering with Field Expansion

Consider following example from FQL documentation, subquery that fetches all user information for the active user and friends:

SELECT uid, name, pic_square FROM user WHERE uid = me()
OR uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

This simply not possible to achieve with a single Graph API call. (see Update)

Once you define desired subset of data to get you can choose appropriate method of retrieval based on your requirements.

BTW, FQL is much older than Graph, we had it aside to FBML (rip) and FBJS (rip)

Update:

Graph API providing way for Batch Requests and specifying dependencies between operations in the request. For example same sample as above can be achieved in a single call to Graph API

POST https://graph.facebook.com/
POST Data:
batch=[
  {
    "method": "GET",
    "name" : "get-friends",
    "relative_url": "me/friends?fields=id",
  },
  {
    "method": "GET",
    "relative_url": "?ids={result=get-friends:$.data.*.id}&fields=id,name,picture"
  }
]

Update 2:
As of 30 August 2012 Graph API is also support Field Expansion as additional (very powerful) mechanism of data retrieval (including nested data)

like image 156
Juicy Scripter Avatar answered Oct 21 '22 05:10

Juicy Scripter


I ran a simple test (getting the cover photo id) using the PHP SDK :

public $microtime_start = 0;

public function getExecutionTime() {
    return round(microtime(true) - $this->microtime_start, 4);
}

public function getFQL($request, $filter = true, $multiFilter = true) {
    if(is_array($request)) {
        $query = '{';
        foreach ($request as $key => $q) {
            $query .= '"'.urlencode($key).'":"'.urlencode(addslashes($q)).'",';
        }
        $query = substr($query, 0, -1).'}';
        $data = $this->callAPI('/fql?q=' . $query);
        if($filter) {
            $data = $data['data'];
            if($multiFilter) {
                $data = $data[count($data)-1]['fql_result_set'];
            }
            return $data;
        }
    } else {
        $data = $this->callAPI('/fql?q=' . urlencode($request));
        if($filter) {
            return $data['data'];
        }
    }
}

public function callAPI($path) {
    $params = array('access_token' => $this->getAccessToken());
    return $this->api($path, 'GET', $params);
}

public function getCoverPhotoId($uid) {
    $time = $this->getExecutionTime();

    $albums = $this->api("/me/albums");
    $album_id = "";
    foreach ($albums["data"] as $item) {
        if ($item["name"] == "Cover Photos") {
            $album_id = $item["id"];
            $profile_picture_id = $item["cover_photo"];
            break;
        }
    }
    $time2 = $this->getExecutionTime();
    echo 'graph api: '.($time2 - $time).'<br/>';

    $data = $this->getFQL(array(
        'query1' => 'SELECT cover_object_id FROM album WHERE owner = ' . $uid.' AND name = "Cover Photos"'
    ));
    $time3 = $this->getExecutionTime();
    echo 'graph api with FQL: '.($time3 - $time2).'<br/>';

    $data = $this->api(
            array(
                'method' => 'fql.query',
                'query' => 'SELECT cover_object_id FROM album WHERE owner = ' . $uid.' AND name = "Cover Photos"'
            )
    );

    $time4 = $this->getExecutionTime();
    echo 'FQL via rest api: '.($time4 - $time3).'<br/>';
}

Results :

graph api: 1.28320002556

graph api with FQL: 0.744100093842

FQL via rest api: 0.544199943542

The basic FQL is much faster, the FQL via graph api is ok, but graph api is waaay too slow... And you will get a bunch of useless stuff with the graph api

like image 26
Olivier Avatar answered Oct 21 '22 05:10

Olivier