Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Optimization Techniques

Similar presentations


Presentation on theme: "Query Optimization Techniques"— Presentation transcript:

1 Query Optimization Techniques
Edward Pollack Query Optimization Techniques

2 Agenda Improving Performance by Writing Better Queries
Quick review of query processing. Tools we will use. What is optimization? What is optimal? Optimization techniques. Conclusion

3 How is a Query Processed?
Parsing = Syntax check. Binding = Object check. Optimization = Find a good execution plan. Execution = Follow the plan.

4 What is Optimal? The query now performs adequately and will for an acceptable amount of time into the future.

5 When Should Stop Optimizing (For Now)?
Resources needed to optimize further are expensive. We have reached the point of diminishing returns. An alternate solution is found that renders this one unneeded. Performance is good enough.

6 What Does the Query Do? What is a query’s purpose?
What should the result set look like? Where is the query coming from?

7 Useful Query Details How large is the result set?
Can parameters have limited values? How often is the query executed? Do any input values indicate an app issue? Are there any obvious logical/syntactical problems? What is acceptable query performance?

8 Optimization Tools Execution plans Statistics IO Query duration
Our eyes More tools are available, but these provide an excellent start.

9 Basic Optimization Tools
Demo Basic Optimization Tools

10 Optimization Tips & Tricks
Common themes. Pattern recognition Things that should catch your attention!

11 Index Scans vs. Index Seeks
Seek = Narrow search using an index. Good for when you need a small data set. Scan = Read the whole blasted table! Good when you need a large data set Lookup = Need more columns Good when the index gets you most of the way there.

12 Implicit Conversions All data has a data type.
When you compare data of 2 different types… …SQL Server will attempt to convert for you. For similar types, this is OK. For different types, you may get an error… …or an inefficient comparison instead!

13 Demo Implicit Conversions

14 Inefficient Joins & Filters
Filters should reduce data size quickly and efficiently. Filter/join columns should be clean: Avoid functions. Avoid type conversions (CAST/CONVERT) Avoid OR. Avoid wildcard searches.

15 Inefficient Joins & Filters
Demo Inefficient Joins & Filters

16 Iteration SQL Server is optimized for set-based operations.
Accessing a table repeatedly wastes resource. 100 rows via row SELECT statements <> 100 rows via row SELECT statements!

17 Demo Iteration

18 High Table Count Optimizer has to evaluate every combination and ordering of tables! More tables = more complexity. Break up large problems when possible.

19 Demo High Table Count

20 Encapsulation Encapsulation = efficient code reuse in code.
Relational databases are not built for this. Objects calling objects calling objects… …can lead to complex/messy code/performance. Beware layers of views, functions, triggers, or stored procedures. Databases are for storing and retrieving data! Encapsulation often results from business logic in the database!

21 Demo Encapsulation

22 Plan Reuse Optimization is an expensive process.
Execution plans are reused when possible. This is an important feature that saves immense resources! Can result in parameter sniffing. The internet is very bad at dealing with this. It is a huge topic. See my article in a few weeks on plan reuse 

23 Avoid Quick Fixes (Unless You’re Sure…)
Query hints. NOLOCK, RECOMPILE, OPTIMIZE FOR, etc… Trace flags. Using local variables instead of parameters. Moving TSQL into dynamic SQL. Adding indexes blindly. Optimizing queries and not applications!

24 Conclusion Performance can be improved by writing better TSQL!
Use multiple metrics to ensure reliable results. Break up large problems into smaller ones. Experiment and play. Be creative! Have fun making things go faster 

25 Questions???

26 Contact Info & Links Ed Pollack @EdwardPollack SQL Shack SQL Server Central SQL Saturday Albany (2018) Thank you!!!


Download ppt "Query Optimization Techniques"

Similar presentations


Ads by Google