FQL multi-query optmization for events listing - facebook

I am working on a custom Events app in Facebook and have ran into an issue where the response seems quite slow. The slowness mostly relate to trying to search for a page that is associated with an events geographical location. I.e "City, Country". Once such URL would be:
http://www.facebook.com/pages/Buenos-Aires-Argentina/106423786059675
I've already determined that it is possible to obtain the page URL for a page like this with the following FQL query:
SELECT page_id, page_url FROM page WHERE name = "Buenos Aires, Argentina" AND type = "CITY"
The type column equals CITY is important, as otherwise other non-relevant pages URLs may be returned.
Anyhow, in order to get all events by a user and display their rsvp status, display information about the venue and also get the geographic places page URL I'm running the following mult-query FQL:
[events_query] => SELECT eid, name, pic_square, start_time, end_time, location, description, all_members_count, venue FROM event WHERE eid IN (SELECT eid FROM event_member WHERE uid = 6747251459 AND start_time > 0) AND start_time > 0 AND end_time < now() ORDER BY start_time desc LIMIT 5
[rsvp_query] => SELECT eid, rsvp_status FROM event_member WHERE eid IN (SELECT eid FROM #events_query) AND uid = YOUR_EVENTS_UID
[venue_page_id_query] => SELECT page_id FROM place WHERE page_id IN (SELECT venue.id FROM #events_query)
[venue_info_query] => SELECT page_id, name, page_url, location FROM page WHERE page_id IN (SELECT page_id FROM #venue_page_id_query)
[geo_city_country_query] => SELECT name, page_url FROM page WHERE name IN (SELECT concat(location.city, ', ', location.country) FROM #venue_info_query) AND type = "CITY"
My problem is that when doing the "geo_city_country_query" the response slows down quite a bit. To approx +5seconds. Without the "geo_city_country_query" query, the response time is approx 1.5sec-2sec.
Is there any way I can get this done faster?
Thanks in advance!

Related

Facebook fql search by first letter of the name

Hello guys i'm trying to do a fql query that search the friends by first letter of the name, i did this query but doesn't show me the right result.
In this case i tried to get all my friends with the first letter "g" but CONTAINS doens't do what i'm looking for. There is another way?
SELECT uid , first_name, last_name FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) AND ( CONTAINS('g')
AND strpos(lower(name),lower('g')) >=0 ) ORDER BY first_name ASC
The problem is with your usage of strpos(). You query will retrieve all your friends having a letter 'g' in their name. And, I think there's no point of using a contains() here.
This query will work fine for your task:
SELECT uid , first_name, last_name FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = me()) AND
strpos(lower(first_name),lower('g')) = 0 ORDER BY first_name ASC
As you can see here, I'm using strpos(lower(first_name),lower('g')) = 0. This will retrieve only those friends having 'g' as the first letter in their first name.

FQL query to get all friends events in a given city

I tried to limit to a latitude/longitude range but don't know how to do it
Here's what I've tried
SELECT name, venue, location, start_time, eid FROM event
WHERE eid IN (
SELECT eid FROM event_member
WHERE (uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) OR uid = me())
)
AND start_time > now()
AND venue.latitude < '-22.821757'
AND venue.latitude > '-23.056989'
but the coordinate comparison doensn't work because i'm comparing to a string
If I take away the quotes I get:
(#601) Parser error: unexpected '-' at position 233
https://developers.facebook.com/tools/explorer
Thanks

FQL not returning photos tagged with friend

I am trying to use FQL to get a list of my own photos that have friends tagged in them. The friends are specified by their name or part of their name. i.e input from a text box
So far I have come up with the FQL below, which almost works, but...
Can anyone tell me why the first query returns a list of records but the second query returns nothing? the only difference between the two is the second one searches on just part of the person's name i.e "laura" vs "lau"
FQL 1
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (
SELECT uid
FROM user
WHERE (strpos(lower(name),"laura") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) ))
)
AND owner = me()
FQL 2
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (
SELECT uid
FROM user
WHERE (strpos(lower(name),"lau") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) ))
)
AND owner = me()
It looks like Facebook has a bug in its system so strpos doesn't work as expected in sub queries. As a workaround you could try splitting it into two queries - one that finds list of id-s:
SELECT uid
FROM user
WHERE (strpos(lower(name),"lau") >=0 )
AND (uid in (SELECT uid2 FROM friend WHERE uid1 = me()) )
And a second one that uses this list:
SELECT object_id, src_big, src_big_width, src_big_height, aid
FROM photo
WHERE object_id in (
SELECT object_id
FROM photo_tag
WHERE subject in (111,222,333,444,555)
)
AND owner = me()

FQL multiqueries without returning everything

I'm using multi queries with FQL, I have 3 queries actually.
The third one is the one that interests me the most, however Facebook returns the result of the previous queries first, which means more overhead for me to process and more time to stream in the result. Is there a way to limit Facebook into only sending the result set of the 3rd query?
JSONqueries["query1"] = encodeURIComponent('SELECT uid1 FROM friend WHERE uid2 = me()');
JSONqueries["query2"] = encodeURIComponent('SELECT eid FROM event_member WHERE uid IN (SELECT uid1 FROM #query1)');
JSONqueries["query3"] = encodeURIComponent('SELECT eid, start_time, end_time, update_time, location, venue FROM event WHERE eid IN (SELECT eid FROM #query2) and privacy=\'OPEN\'');
Here you are. But it's still too complex and therefore extremely slow.
SELECT eid, start_time, end_time, update_time, location, venue FROM event WHERE eid IN(SELECT eid FROM event_member WHERE uid IN (SELECT uid1 FROM friend WHERE uid2 = me())) and privacy="OPEN"

Get photos from stream

Is there a way to get all of the photos form a given stream query? I tried the following but it returns an empty set.
SELECT pid, aid, src_big FROM photo WHERE pid IN (SELECT attachment.media.photo.pid FROM #feed)
Its tough to tell since I can't see your '#feed' query but more than likely you are missing a stream_filter query. This would give you something like the following 3 queries. Good Luck!
Query 'key':
SELECT filter_key
FROM stream_filter
WHERE uid = me() AND name = 'Photos'
Query 'feed':
SELECT attachment.media.photo.pid
FROM stream
WHERE source_id = {id here} AND filter_key IN (SELECT filter FROM #key)
Query 'photos':
SELECT caption, src_big, modified
FROM photo
WHERE pid IN (SELECT pid FROM #feed)