BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate...

5
BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson School of Business, Concordia University, Montréal 1

Transcript of BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate...

Page 1: BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.

BTM 382 Database Management

Chapter 11.5-6Writing optimized SQL queries

Chitu OkoliAssociate Professor in Business Technology ManagementJohn Molson School of Business, Concordia University, Montréal

1

Page 2: BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.

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

Page 3: BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.

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

Page 4: BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.

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

Page 5: BTM 382 Database Management Chapter 11.5-6 Writing optimized SQL queries Chitu Okoli Associate Professor in Business Technology Management John Molson.

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: 978-1-285-19614-5

• Other sources are noted on the slides themselves

5