Let's take a simple FQL
query to get all links shared by a user's friends since yesterday for example:
SELECT link_id, title, url, owner, created_time
FROM link
WHERE
created_time > strtotime('yesterday') AND
owner IN (
SELECT uid2 FROM friend WHERE uid1 = me()
)
LIMIT 100
If a user has 50 friends, this will execute perfectly. But if a user has hundreds of friends, more often than not Facebook returns an error.
Options:
How can I query Facebook appropriately to ensure successful results?
Notes:
Common Errors associated with Timeout:
1.
Fatal error: Uncaught Exception: 1: An unknown error occurred thrown in /..../facebook/php-sdk/src/base_facebook.php on line 708
line 708 is an exception error:
// results are returned, errors are thrown
if (is_array($result) && isset($result['error_code'])) {
throw new FacebookApiException($result);
}
2.
Fatal error: Uncaught CurlException: 52: SSL read: error:00000000:lib(0):func(0):reason(0), errno 104 thrown in /..../facebook/php-sdk/src/base_facebook.php on line 814
You should loop through using limit/offset like you said, or cache the friends list up front as puffpio suggested.
You said that it still wasn't working reliably - this is because some users may have many, many links, while others not so many. Note also that you may be retrieving uncached data for some users. I would recommend having a single retry in your loop for failed queries - it's often the case that the first one will time out and the second one will succeed due to newly cached data.
Finally, for posterity, I'm opening a task to optimize the link table to do a better job of being efficient when it's being filtered by time.
Some db engines do not optimize the IN
keyword well, or at all. They may be executing the in clause for every single resulting row of your query. Can you join the link and friend tables instead of using an IN with a subquery?
You may find this article interesting. (Discusses issues with IN clause performance on MySQL and Facebook runs MySQL on the back end.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With