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
Ed Pollack Database Administrator CommerceHub

2 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

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

4 Optimization Tools Execution plans. Statistics IO. Query duration.
Server/instance data (waits, locking, sessions, utilization). Monitoring tools (custom-built or 3rd party).

5 Basic Optimization Tools
Demo

6 How Can We Improve Performance?
Indexing changes. Statistics updates/changes. Add/adjust hardware. Adjust server settings. Adjust system internals. Rewrite the query.

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

8 Implicit Conversions Demo

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

10 Removing Iteration Demo

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

12 Reduce Table Count Demo

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

14 Dangers of Encapsulation
Demo

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

16 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

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

18 Goofing With Parameters
Demo

19 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…

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

21 Bad Join/Filter Clauses
Demo

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

23 Questions???

24 Contact Info & Links for Ed Pollack @EdwardPollack SQL Shack SQL Server Central Dynamic SQL: Applications, Performance, and Security SQL Saturday Albany (2016) Thank you!!!


Download ppt "Query Optimization Techniques"

Similar presentations


Ads by Google