令人窒息,查询太大了。考虑使用多查询、朋友查询的 LIMITs 和一些 OFFSET 来分页数据。
fql?q={"friendsquery":"SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 20 OFFSET 0","linkquery": "SELECT backdated_time, can_backdate, caption, created_time, owner, owner_comment, picture, summary, title, url FROM link WHERE owner IN (SELECT uid2 FROM #friendsquery) AND (strpos(lower(summary),'boston') >=0 OR strpos(lower(owner_comment),'boston') >=0 ) LIMIT 20"}
对于 400 个好友列表,当前查询需要 80 秒。
这是 Python 中的示例,它不是 PHP,但 API 实现是相同的
from facepy import GraphAPI
import time
import urllib
import json
fb_dump = open('dumpfb.txt', 'w')
graph = GraphAPI('MY_ACCESS_TOKEN')
all_posts = []
offset = 0
friendsquery = 'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 20 OFFSET '+ str(offset)
linksquery = 'SELECT backdated_time, can_backdate, caption, created_time, owner, owner_comment, picture, summary, title, url FROM link WHERE owner IN (SELECT uid2 FROM #friendsquery) AND (strpos(lower(summary),"trinidad") >=0 OR strpos(lower(owner_comment),"trinidad") >=2 ) LIMIT 20'
t0 = time.time()
try:
my_posts = graph.fql({'friendsquery': friendsquery, 'linksquery': linksquery})
while my_posts["data"][0]["fql_result_set"]:
all_posts = all_posts + my_posts["data"]
offset = offset + 20
friendsquery = 'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 20 OFFSET '+ str(offset)
my_posts = graph.fql({'friendsquery': friendsquery, 'linksquery': linksquery})
t1 = time.time()
print t1-t0
fb_dump.write(json.dumps(all_posts, indent=4))
except GraphAPI.OAuthError, e:
print e.message
进行批量查询可以使查询时间缩短到 25 秒。不快,但比前两个查询快一个订单。我没有在 PHP 中做过,但你应该明白要点
这是一批中的 20 个多查询和一个 /me/friend 调用
fb_dump = open('lumpfb.txt', 'w')
graph = GraphAPI('MY_ACCESS_TOKEN')
all_posts = []
offset = 0
friendsquery = 'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 20 OFFSET '
linksquery = 'SELECT backdated_time, can_backdate, caption, created_time, owner, owner_comment, picture, summary, title, url FROM link WHERE owner IN (SELECT uid2 FROM #friendsquery) AND (strpos(lower(summary),\'trinidad\') >=0 OR strpos(lower(owner_comment),\'trinidad\') >=2 ) LIMIT 20'
t0 = time.time()
batch_request = []
try:
my_friends = graph.get("me?fields=friends")
counter = len(my_friends["friends"]["data"])
while counter > 0 :
friendsquery0 = 'SELECT uid2 FROM friend WHERE uid1 = me() LIMIT 20 OFFSET '+ str(offset)
the_query = '{"friendsquery":"'+friendsquery0+'","linksquery":"'+linksquery+'"}'
batch_item = {"method":"GET","relative_url":"fql?q=" + the_query}
batch_request.append(batch_item)
offset = offset + 20
counter = counter - 20
my_posts = graph.batch(batch_request)
for post in my_posts:
all_posts = all_posts + post['data']
t1 = time.time()
print t1-t0
except facepy.exceptions.FacepyError, e:
print e.message