BTM 382 Database Management Chapter Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia University, Montréal 1
SQL Performance Tuning Evaluated from client perspective –How fast does the query feel to the client? Majority of performance problems are related to poorly written SQL code Although the SQL engine does automatic optimization, it can’t work miracles against inefficiently designed code 2
Index Selectivity Indexes speed up queries that involve searching or sorting SQL engine tries to use indexes whenever possible –Help the engine by changing “whenever” to “almost always” possible Be aware of indexes (or create them if useful), and try to use them in your query design Data sparsity –Low sparsity: relatively small number of different values (e.g. female or male) –High sparsity: relatively large number of different values (e.g. dates of birth) –Indexes are most useful for values with high data sparsity 3
Conditional Expressions Expressed within WHERE or HAVING clauses of a SQL statement –Restricts the output of a query to only rows matching conditional criteria –e.g. =, >, LIKE Careful use of conditional expressions has significant effect on speed of query execution –Various tips on careful usage 4
Sources Most of the slides are adapted from Database Systems: Design, Implementation and Management by Carlos Coronel and Steven Morris. 11th edition (2015) published by Cengage Learning. ISBN 13: Database Systems: Design, Implementation and Management Other sources are noted on the slides themselves 5