Query Optimization Techniques

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

LCT2506 Internet 2 Further SQL Stored Procedures.
Troubleshooting SQL Server Enterprise Geodatabase Performance Issues
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
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.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
SQL Query Analyzer. Graphical tool that allows you to:  Create queries and other SQL scripts and execute them against SQL Server databases. (Query window)
Oracle9i Developer: PL/SQL Programming Chapter 11 Performance Tuning.
Copyright Sammamish Software Services All rights reserved. 1 Prog 140  SQL Server Performance Monitoring and Tuning.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
CS4432: Database Systems II Query Processing- Part 1 1.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Execution Plans Detail From Zero to Hero İsmail Adar.
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.
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
Eli Robillard Microsoft Office Server & Services
SQL Server Performance Tuning
SQL Server Statistics and its relationship with Query Optimizer
Parameter Sniffing in SQL Server Stored Procedures
Tuning Transact-SQL Queries
Query Optimization Techniques
Dynamic SQL Writing Efficient Queries on the Fly
Stored Procedures – Facts and Myths
Dynamic SQL: Writing Efficient Queries on the Fly
Data Virtualization Tutorial… Semijoin Optimization
Efficiently Searching Schema in SQL Server
Parameter Sniffing in SQL Server Stored Procedures
Dynamic SQL Writing Efficient Queries on the Fly
Chapter 12: Query Processing
Mapping Shema and Recursively Managing Data
Database Performance Tuning and Query Optimization
Reading Execution Plans Successfully
Introduction to Execution Plans
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
Third Party Tools for SQL Server
Cardinality Estimator 2014/2016
Top Tips for Better TSQL Stored Procedures
Execution Plans Demystified
Statistics: What are they and How do I use them
Transactions, Locking and Query Optimisation
Reading Execution Plans Successfully
Tracking Index Usage Like a Pro
Dynamic SQL: Writing Efficient Queries on the Fly
SQL Server Query Plans Journeyman and Beyond
Please thank our sponsors!
Transact SQL Performance Tips
Finding Islands, Gaps, and Clusters in Complex Data
Introduction to Execution Plans
Parameter Sniffing on SQL Server
Chapter 11 Database Performance Tuning and Query Optimization
Insight into the SQL Server Buffer Cache
Query Tuning Fundamentals
Power Query & Database Tuning
Dynamic Sql Not so scary?
Diving into Query Execution Plans
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
The Five Mistakes You are Probably Making with SQL Server
Creating and Using Calendar Tables
T-SQL Basics: Coding for performance
Introduction to Execution Plans
Finding Islands, Gaps, and Clusters in Complex Data
Presentation transcript:

Query Optimization Techniques Edward Pollack Query Optimization Techniques

Thanks to our sponsors And Global Gold Silver Bronze Microsoft JetBrains Rubrik Delphix Solution OMD

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