Query Optimization Techniques

Slides:



Advertisements
Similar presentations
Phoenix We put the SQL back in NoSQL James Taylor Demos:
Advertisements

Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide
Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
SQL Server performance tuning basics
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
SQL Performance 2011/12 Joe Chang, SolidQ
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
Practical Database Design and Tuning. Outline  Practical Database Design and Tuning Physical Database Design in Relational Databases An Overview of Database.
Access Path Selection in a Relational Database Management System Selinger et al.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
1 Chapter 7 Optimizing the Optimizer. 2 The Oracle Optimizer is… About query optimization Is a sophisticated set of algorithms Choosing the fastest approach.
1 Chapter 10 Joins and Subqueries. 2 Joins & Subqueries Joins – Methods to combine data from multiple tables – Optimizer information can be limited based.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
1 Copyright © 2005, Oracle. All rights reserved. Following a Tuning Methodology.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
Tuning Oracle SQL The Basics of Efficient SQL Common Sense Indexing
SQL Server Statistics and its relationship with Query Optimizer
Practical Database Design and Tuning
Tuning Transact-SQL Queries
Dynamic SQL Writing Efficient Queries on the Fly
Query Tuning without Production Data
Troubleshooting SQL Server When You Cannot Access The Machine
Dynamic SQL: Writing Efficient Queries on the Fly
T-SQL Coding Techniques Are you playing with fire?
Efficiently Searching Schema in SQL Server
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Building Effective Backups
Dynamic SQL Writing Efficient Queries on the Fly
Mapping Shema and Recursively Managing Data
Database Performance Tuning and Query Optimization
Reading Execution Plans Successfully
Peeking into the Plan Cache with SQL Server 2008
Introduction to Execution Plans
Chapter 15 QUERY EXECUTION.
Third Party Tools for SQL Server
Agenda Database Development – Best Practices Why Performance Matters ?
TSQL Coding Techniques
Query Optimization Techniques
Top Tips for Better TSQL Stored Procedures
Execution Plans Demystified
Statistics: What are they and How do I use them
Practical Database Design and Tuning
Tracking Index Usage Like a Pro
Dynamic SQL: Writing Efficient Queries on the Fly
When I Use NOLOCK AND OTHER HINTS
Transact SQL Performance Tips
Finding Islands, Gaps, and Clusters in Complex Data
Introduction to Execution Plans
Chapter 11 Database Performance Tuning and Query Optimization
Insight into the SQL Server Buffer Cache
Query Tuning Fundamentals
“Magic numbers”, local variable and performance
Diving into Query Execution Plans
Query Optimization.
Tracking Index Usage Like a Pro
SQL Server Query Design and Optimization Recommendations
Finding Islands, Gaps, and Clusters in Complex Data
Introduction to Execution Plans
Query Optimization Techniques
Creating and Using Calendar Tables
Introduction to Execution Plans
Presentation transcript:

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