Query Optimization Techniques Edward Pollack Query Optimization Techniques
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
How is a Query Processed? Parsing = Syntax check. Binding = Object check. Optimization = Find a good execution plan. Execution = Follow the plan.
What is Optimal? The query now performs adequately and will for an acceptable amount of time into the future.
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.
What Does the Query Do? What is a query’s purpose? What should the result set look like? Where is the query coming from?
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?
Optimization Tools Execution plans Statistics IO Query duration Our eyes More tools are available, but these provide an excellent start.
Basic Optimization Tools Demo Basic Optimization Tools
Optimization Tips & Tricks Common themes. Pattern recognition Things that should catch your attention!
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.
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!
Demo Implicit Conversions
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.
Inefficient Joins & Filters Demo Inefficient Joins & Filters
Iteration SQL Server is optimized for set-based operations. Accessing a table repeatedly wastes resource. 100 rows via 100 1 row SELECT statements <> 100 rows via 1 100 row SELECT statements!
Demo Iteration
High Table Count Optimizer has to evaluate every combination and ordering of tables! More tables = more complexity. Break up large problems when possible.
Demo High Table Count
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!
Demo Encapsulation
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
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!
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
Questions???
Contact Info & Links Ed Pollack ed7@alum.rpi.edu @EdwardPollack SQL Shack SQL Server Central SQL Saturday Albany (2018) Thank you!!!