查询Facebook上朋友分享的所有链接的最快方式

时间:2012-08-16 03:49:02

标签: facebook facebook-graph-api facebook-javascript-sdk facebook-fql facebook-php-sdk

我想抓住最近几天用户的Facebook好友分享的最新链接。让我们说在过去的7天里:

使用FQL,我可以一次抓住最多20位朋友:

$fql = "SELECT link_id, owner, created_time, title, summary, url, image_urls FROM link WHERE owner IN (select uid2 from friend where uid1 = me() LIMIT 20) AND created_time >= $_7ago";

如果我将好友数限制从20设置为更高,则查询会崩溃。我目前的解决方案是重复这个查询,在一个循环中将1000个朋友分解为20个组。这实际上有效但运行时间超过五分钟。

我也尝试过这种方式进行多次查询(并且速度没有增加):

"links":"select uid2 from friend where uid1 = me() LIMIT 20",
"stats":"SELECT owner, owner_comment, title, summary, url, image_urls FROM link WHERE owner IN (select uid2 from #links) AND created_time  >= \"' . $_7ago . '\""

是否有更快的替代方案/解决方案?

http://www.pixable.com/< - 设法在不到3秒的时间内提取朋友分享的每月照片

1 个答案:

答案 0 :(得分:0)

多个查询也可用于触发多个不相关的查询。首先获取朋友uids,然后通过将uid分成块来创建单个查询,并立即触发所有查询。在js sdk中,这看起来像:

FB.api( {
    method: 'fql.multiquery',
    queries: [
            'SELECT owner, owner_comment, title, summary, url, image_urls FROM link WHERE owner IN ('+chunk0+') AND created_time  >= '+time,
            'SELECT owner, owner_comment, title, summary, url, image_urls FROM link WHERE owner IN ('+chunk1+') AND created_time  >= '+time,
            'SELECT owner, owner_comment, title, summary, url, image_urls FROM link WHERE owner IN ('+chunk2+') AND created_time  >= '+time
     ]}, function(results) {
     console.log(results); // array with the individual query results
         var query_results = null;
         var row = null;
         for (var i = 0, l = results.length; i < l; ++i) {
             var query_results = results[i];
             for (var i2 = 0, l2 = query_results.fql_result_set.length; i2<l2; ++i2) {
                 row = query_results.fql_result_set[i2];
                 console.log(row);
                 // at this point row variable contains individual rows from the link table
                 // with the selected fields
                 console.log(row.owner, row.title); // should log something useful
             }
         }
     }
);

documentations states这应该比使用批处理api接口更快:

  

此方法比使用batch.run运行一系列fql.query调用具有更好的性能。

在firebug中,第一个console.log(results)应显示与此类似的内容(实际id被删除/截断以用于空间约束): Firebug console output for results