Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

FQL Overview

31,161 views

Published on

An overview of the Facebook's application query language FQL presented by Craig Saila in August 2007 at FaceBookCamp Toronto.

The video of the presentation (along with two others) can be found here: http://www.thechickentest.com/2007/08/facebookcamptoronto-videos/

Published in: Technology, Business
  • Now there is LIMIT guy :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dear Craig,
    I viewed your slideshow and I loved the facebook != fecal slide. Can you help prove that by telling me what you think the best way to download all the posts + comments in my stream to a local database is? It seems that I can not use multiquery for that because the purpose of the multiquery is to limit the number of results for a query, and a left join is not possible using FQL. I had some issues with subqueries to get the username(friendly) and photo from the stream(which it would appear IS a good use for multiquery). Thanks for your input. I emailed the question as the last slide said to email questions, if you reply I will post it here. thanks!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Craig - really appreciated your presentation. You did a great job on a technical topic. Plus, everyone loves a good potty joke :)
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

FQL Overview

  1. 1. FQL <ul><li>Overview of FQL </li></ul><ul><li>Creating a simple query </li></ul><ul><li>Using more advanced syntax </li></ul><ul><li>Differences between SQL and FQL </li></ul><ul><li>Q & A </li></ul>
  2. 2. FQL: Overview <ul><li>Facebook Query Language introduced in the early spring of 2007 </li></ul><ul><li>F8 appeared a little over two months ago </li></ul><ul><li>Facebook API calls are just pre-packaged FQL queries </li></ul>
  3. 3. FQL: Overview <ul><li>Key advantages: </li></ul><ul><ul><li>Common syntax for all methods </li></ul></ul><ul><ul><li>Condense Facebook queries </li></ul></ul><ul><ul><li>Reduce response size </li></ul></ul>
  4. 4. FQL: Overview <ul><li>FQL is an SQL-like syntax: </li></ul><ul><ul><li>SELECT name FROM user WHERE uid = 00000000 </li></ul></ul>
  5. 5. FQL: Simple Query <ul><li>Three parts to a query: </li></ul><ul><ul><li>SELECT a field </li></ul></ul><ul><ul><li>FROM a table </li></ul></ul><ul><ul><li>WHERE a condition is met </li></ul></ul>
  6. 6. FQL: Simple Query <ul><li>Where do I live? </li></ul><ul><ul><li>SELECT current_location FROM user WHERE uid = 00000000 </li></ul></ul>
  7. 7. FQL: Simple Query <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <users_getInfo_response xmlns=&quot;http://api.facebook.com/1.0/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://api.facebook.com/1.0/ http://api.facebook.com/1.0/facebook.xsd&quot; list=&quot;true&quot;> <user> <uid>503802602</uid> <current_location> <city> Toronto </city> <state> Ontario </state> <country> Canada </country> <zip/> </current_location> </user> </users_getInfo_response>
  8. 8. FQL: Simple Query <ul><li>Has AND , OR , and NOT keywords </li></ul><ul><li>Does simple math </li></ul><ul><li>Uses basic boolean operators </li></ul><ul><ul><li>SELECT uid2 FROM friend WHERE uid1 = 00000000 AND uid2 > (00000000 + 700) </li></ul></ul>
  9. 9. FQL: Simple Query ... <friend_info> <uid2> 00000701 </uid2> </friend_info> <friend_info> <uid2> 00000702 </uid2> </friend_info> <friend_info> <uid2> 00000703 </uid2> </friend_info> ...
  10. 10. FQL: Simple Query <ul><li>Enhance queries with basic functions </li></ul><ul><li>Examples include: </li></ul><ul><ul><li>now() </li></ul></ul><ul><ul><li>strlen() </li></ul></ul><ul><ul><li>substr() </li></ul></ul><ul><ul><li>strpos() </li></ul></ul>
  11. 11. FQL: Advanced Query <ul><li>Use functions and math to construct more accurate responses: </li></ul><ul><ul><li>SELECT location FROM event WHERE eid = 2487044119 AND strpos(name,&quot;facebook&quot;) AND start_time >= 1186536600 </li></ul></ul>
  12. 12. FQL: Advanced Query <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <fql_query_response xmlns=&quot;http://api.facebook.com/1.0/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; list=&quot;true&quot;> <event> <location> MaRS </location> </event> </fql_query_response>
  13. 13. FQL: Advanced Query <ul><li>Queries must be “indexable” </li></ul><ul><li>Restricted by unique IDs, typically a table's primary ID (“uid”, “pid”, “eid”) </li></ul><ul><li>Without an indexable WHERE condition, expect an error </li></ul>
  14. 14. FQL: Advanced Query ... <error_code> 604 </error_code> <error_msg> Your statement is not indexable - the WHERE clause must contain one of the columns marked with a star in http://developers.facebook.com/documentation.php?doc=fql</error_msg> <request_args list=&quot;true&quot;> <arg> <key>query</key> <value> SELECT name FROM event WHERE strpos(location,&quot;facebook&quot;) </value> </arg> ...
  15. 15. FQL: Advanced Query <ul><li>Subqueries allow very narrow results to be returned </li></ul><ul><li>Chain queries together with IN clause </li></ul><ul><li>Limited only by the data itself </li></ul>
  16. 16. FQL: Advanced Query <ul><li>SELECT eid </li></ul><ul><ul><li>FROM event </li></ul></ul><ul><ul><li>WHERE eid </li></ul></ul><ul><ul><li>IN ( </li></ul></ul><ul><ul><ul><li>SELECT eid </li></ul></ul></ul><ul><ul><ul><li>FROM event_member </li></ul></ul></ul><ul><ul><ul><li>WHERE uid = 00000000 </li></ul></ul></ul><ul><ul><ul><li>AND rsvp_status = &quot;attending&quot; </li></ul></ul></ul><ul><ul><li>) </li></ul></ul><ul><ul><li>AND location = &quot;mars&quot; </li></ul></ul>
  17. 17. FQL: Advanced Query <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?> <fql_query_response xmlns=&quot;http://api.facebook.com/1.0/&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; list=&quot;true&quot;> <event> <eid> 2487044119 </eid> </event> </fql_query_response>
  18. 18. FQL: Advanced Query <ul><li>Some columns contain arrays </li></ul><ul><li>Example: current_location also holds city , state , zip </li></ul><ul><li>Usage: work_history.company_name </li></ul>
  19. 19. FQL: Versus SQL FQL != fecal
  20. 20. FQL: Versus SQL but... FQL != SQL
  21. 21. FQL: Versus SQL <ul><li>FQL is designed to be efficient </li></ul><ul><li>Improving performance means restricting queries </li></ul>
  22. 22. FQL: Versus SQL <ul><li>No JOIN </li></ul><ul><li>No ORDER BY </li></ul><ul><li>No GROUP BY </li></ul><ul><li>No LIMIT </li></ul>
  23. 23. FQL: Versus SQL <ul><li>Limitations may mean too much data is returned </li></ul><ul><li>Requires more complicated parsing </li></ul>
  24. 24. FQL: Versus SQL <ul><li>Each FROM can only be against one table </li></ul><ul><li>Not allowed: </li></ul><ul><ul><li>SELECT * FROM user, photo_tag WHERE uid = 00000000 AND subject = `flower` </li></ul></ul>
  25. 25. FQL: Versus SQL <ul><li>Example: display the 5 most recently updated events in Toronto </li></ul>
  26. 26. FQL: Versus SQL <ul><li>In SQL: </li></ul><ul><ul><li>SELECT * FROM event WHERE location = &quot;mars&quot; AND user = 00000000 ORDER BY update_time ASC LIMIT 5 </li></ul></ul>
  27. 27. FQL: Versus SQL <ul><li>In FQL: </li></ul><ul><ul><li>SELECT update_time, eid, name, location, venue </li></ul></ul><ul><ul><li>FROM event WHERE eid </li></ul></ul><ul><ul><li>IN ( </li></ul></ul><ul><ul><li>SELECT eid FROM event_member WHERE uid = 00000000 </li></ul></ul><ul><ul><li>) </li></ul></ul><ul><ul><li>AND location = &quot;mars&quot; </li></ul></ul><ul><li>Then sort the array, and slice it: arsort($result); array_slice($result,0,5); </li></ul>
  28. 28. FQL: Tips <ul><li>Use the developer tools ( fql.query ) </li></ul><ul><li>See what is available by investigating the API documentation </li></ul><ul><li>Share what you learn on your blog and the developer Wiki </li></ul>
  29. 29. FQL Questions? Craig Saila saila.com [email_address]

×