Are your queries slow? Learn how to speed them up through better SQL crafting and use meaningful indices. Attendees will understand what works well and what doesn't, and will walk away with a checklist for faster databases.
FooLab Multitude of Queries • Loops: • Reduce the number of queries. • Check for lazy loading in your ORM. • Query a set instead. 5 for id in pictures: query_pic_details(id)
FooLab Slow Query Log • Open my.cnf • Queries taking 0.5s or longer will be logged. • Open the log file and analyze the queries. 8 slow_query_log=ON long_query_time=0.5 slow_query_log_file=/path/to/file
FooLab Simple Query • Get all pictures from user #1 • 38 seconds. 10 SELECT picture.id, picture.title FROM picture LEFT JOIN album ON picture.album.id = album.id WHERE album.user_id = 1;
FooLab Explain 14 • Details: • Because of the join, we’re scanning all albums for each picture. • Total 40 billion rows scanned. table : album key : NULL rows : 20,000 table : picture key : NULL rows : 2,000,000
FooLab Under the Hood 18 • Details: • With the index, we find our album right away. • Total 2,000,000 rows scanned. table : album key : PRIMARY rows : 1 table : picture key : NULL rows : 2,000,000
FooLab Airport Customs 25 • 5 customs officers, 1 min to process a traveler. • 5 travelers / min = fluid. • 20 travelers / min = queue slowly builds. • 1000 travellers / min = 3h wait if you arrive on 2nd minute.
FooLab Functions & Indexes 29 • Avoid functions on an indexed column: • The index won’t be used here. SELECT id, title FROM picture WHERE YEAR(create_date) >= 2011;
FooLab Limit 30 • Stop picking up firewood when your hands are full: • Now down to 0.0009 sec. • 100 times faster than the last query. SELECT picture.id, picture.title FROM picture LEFT JOIN album ON picture.album.id = album.id WHERE album.user_id = 1 LIMIT 25;
FooLab Other Constraints 31 • Range: • Only makes sense if that column is indexed: SELECT picture.id, picture.title FROM picture LEFT JOIN album ON picture.album.id = album.id WHERE album.user_id = 1 AND picture.id BETWEEN 26 AND 50; # 0.023 sec ALTER TABLE picture ADD INDEX(id);
FooLab Other Constraints 32 • IN clause: SELECT picture.id, picture.title FROM picture LEFT JOIN album ON picture.album.id = album.id WHERE album.user_id = 1 AND picture.id IN (15,26,29,32,45); # 0.048 sec
FooLab Archives 34 • Don’t underestimate archive tables. • MySQL ARCHIVE storage engine: • Doesn’t support UPDATE or DELETE • Very fast for SELECT • You can archive old transactions, news, etc.
FooLab Split Databases 35 • Also called horizontal partitionning or sharding. • Users 1-1000 and their data in database #1 • Users 1001-2000 and their data in database #2
FooLab The Checklist 36 ✓Count your queries. ✓Log the slow ones. ✓Use EXPLAIN and check number of rows scanned. ✓Try different indexes, remove the unused ones. ✓Use small indexes, preferably numeric. ✓Don’t use functions on indexed columns. ✓Limit number of results. ✓Split tables and databases.