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 Thanks to our sponsors And
Global Gold Silver Bronze Microsoft JetBrains Rubrik Delphix Solution OMD

3 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

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

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

6 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.

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

8 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?

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

10 Basic Optimization Tools
Demo Basic Optimization Tools

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

12 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.

13 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!

14 Demo Implicit Conversions

15 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.

16 Inefficient Joins & Filters
Demo Inefficient Joins & Filters

17 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!

18 Demo Iteration

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

20 Demo High Table Count

21 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!

22 Demo Encapsulation

23 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 

24 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!

25 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 

26 Questions???

27 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