Query Optimization Techniques Ed Pollack Database Administrator CommerceHub
Agenda Improving Performance By Writing Better Queries Quick Review: How is a query processed? Tools and metrics used to measure performance. Optimization techniques. Common mistakes & assumptions. Conclusion
How is a Query Processed? Parsing: Break a TSQL statement into distinct parts. Binding: Verify objects exist and are being used correctly. Create a query tree for your TSQL. Optimization: The query optimizer determines the best way to execute your TSQL, which becomes an execution plan. Execution: SQL Server executes your query based on the query plan found above.
Optimization Tools Execution plans. Statistics IO. Query duration. Server/instance data (waits, locking, sessions, utilization). Monitoring tools (custom-built or 3rd party).
Basic Optimization Tools Demo
How Can We Improve Performance? Indexing changes. Statistics updates/changes. Add/adjust hardware. Adjust server settings. Adjust system internals. Rewrite the query.
Optimization: Implicit Conversions SQL Server will try to convert data types for you. This may result in excess scans to convert the data. Adjust data types (either one) to be identical.
Implicit Conversions Demo
Optimization: Minimize Iteration SQL Server is optimized for set-based operations. Iteration requires repeated operations. Perform work in a single query or set of queries whenever possible. Consider data size when deciding how to proceed.
Removing Iteration Demo
Optimization: Reduce Table Count Each table in a query adds immense complexity. When possible, separate large queries into smaller, more manageable pieces. Temp tables, table variables, and local variables can help in managing this.
Reduce Table Count Demo
Optimization: Dangers of Encapsulation Nesting objects can result in unnecessary work. Over-encapsulation can obfuscate schema. Complicates database design. Leads potentially to suboptimal execution plans.
Dangers of Encapsulation Demo
Optimization: Caution with Triggers Add layers of complexity to data structures. If needed, keep them as simple as possible. Instead consider constraints, cascade operations, etc… Avoid triggers calling triggers. Avoid nested triggers (SQL Server config option).
Optimization: Beware Auto-TSQL ORMs (Object-relational mapping), schema frameworks, data access layers, business objects mapping, etc… Easy to develop. Fast when data is simple/small. Will often result in suboptimal TSQL Too many columns Too many tables Too many joins
Optimization: Parameter Mistakes Re-declare local variables: Forces optimizer to not use histogram data. Less accurate plans EVERY TIME. Using RECOMPILE hints: Forces a new plan every time. Prevents parameter sniffing, but consumes resources. Not parameterizing: No plan reuse, potentially bloating the plan cache.
Goofing With Parameters Demo
Optimization: Query Hints Hints override the optimizer and force it to use or not use specific transformation rules. Hints that remain in code can become antiquated with time. Only use when you are certain it is the right thing to do! Examples: NOLOCK, OPTIMIZE FOR, RECOMPILE, MAXRECURSION, MAXDOP, FORCESEEK, MERGE, HASH…
Optimization: Joins & Filters Filters and joins should limit data as much as possible. <>, OR, NOT IN will often expand data sets =, IN, AND will typically shrink sets and provide better plans. Break up large queries with complex join predicates.
Bad Join/Filter Clauses Demo
Conclusion We can often improve performance by changing how we write our TSQL. Use multiple performance metrics to ensure a good result (CPU, query cost, IO, duration). Break up large problems into several smaller ones. Don’t be afraid to experiment and play with TSQL! Have fun making things go faster
Questions???
Contact Info & Links for Ed Pollack ed7@alum.rpi.edu @EdwardPollack SQL Shack SQL Server Central Dynamic SQL: Applications, Performance, and Security SQL Saturday Albany (2016) Thank you!!!