How to Use Parameters Like a Pro

Slides:



Advertisements
Similar presentations
SQL Performance 2011/12 Joe Chang, SolidQ
Advertisements

Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Clarity Educational Community Clarity Educational Community Creating and Tuning SQL Queries that Engage Users.
“Participant-Centered Training” Bob Pike Annual Conference September 12-14, 2012 Minneapolis, MN.
SQL Server User Group Meeting Reporting Services Tips & Tricks Presented by Jason Buck of Custom Business Solutions.
The LC-3 – Chapter 7 COMP 2620 Dr. James Money COMP
CREATING COMPLEX QUERIES WITH NESTED QUERIES CS1100: Data, Databases, and Queries CS1100Microsoft Access1.
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Part-time Jobs. What’s the difference between part-time jobs in the West and China?
Driving and Cars. What’s the first thing you should do if you want to learn how to drive?
Your cell phone. In America people don’t say ‘mobile phone’, they say ‘cell phone’.
College Life. Very large classes, may have students for first two years for prerequisites (general knowledge) Image courtesy of smokedsalmon at.
Interesting Fun Funny. ‘ 有趣 ’ Most English learners say ‘interesting’. But sometimes you should say ‘fun’.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Work. Think of and write down at least five jobs that you might like to have.
Driving and Cars. What’s the first thing you should do if you want to learn how to drive?
10 points I always keep in mind when I have to implement change Peter Kesch, MBA Peter Kesch, MBA1.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
High Performance Functions SQLBits VI. Going backwards is faster than going forwards.
Part-time Jobs. What’s the difference between part-time jobs and temporary jobs?
Parameter Sniffing in SQL Server Stored Procedures
Query Optimization Techniques
The DBA's Role within N-tier Applications
Stored Procedures – Facts and Myths
Data Virtualization Tutorial… OAuth Example using Google Sheets
Things You Can Find in the Plan Cache.
CS 326 Programming Languages, Concepts and Implementation
Parameter Sniffing in SQL Server Stored Procedures
Dynamic SQL Writing Efficient Queries on the Fly
College Life.
Serverless CQRS in Azure!
Query Execution Expectation-Reality Denis Reznik
Working with Very Large Tables Like a Pro in SQL Server 2014
CSC 253 Lecture 8.
EXEC and sp_executesql An Ad Hoc Rally
When I Use NOLOCK AND OTHER HINTS
Now where does THAT estimate come from?
Phil Tayco Slide version 1.0 Created Oct 2, 2017
CSC 253 Lecture 8.
Query Optimization Techniques
Top Tips for Better TSQL Stored Procedures
Batches, Transactions, & Errors
Plan cache performance tuning
Mean.
When query plans go wrong
The PROCESS of Queries John Deardurff
When I Use NOLOCK AND OTHER HINTS
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
A lot.
Parameter Sniffing: the Good, the Bad, and the Ugly
The PROCESS of Queries John Deardurff
Batches, Transactions, & Errors
Parameter Sniffing: the Good,the Bad, and the Ugly
When I Use NOLOCK AND OTHER HINTS
Parameter Sniffing on SQL Server
Parameter Sniffing: the Good, the Bad, and the Ugly
COP 3330 Object-oriented Programming in C++
“Magic numbers”, local variable and performance
Creating complex queries using nesting
The PROCESS of Queries John Deardurff August 8, 2015
SQL Server Query Design and Optimization Recommendations
Arrays.
Query Optimization Techniques
The Complete Guide to Temporary Tables and Table Variables
Why should I care about SQL, if I have ORM?
The Complete Guide to Temporary Tables and Table Variables
Working with Very Large Tables Like a Pro in SQL Server 2017
Analyzing Execution Plans
Presentation transcript:

How to Use Parameters Like a Pro …and Boost Performance Guy Glantser

A Few Words about Me… Name: Guy Glantser Email Address: guy@madeiradata.com Twitter: @guy_glantser Blog: www.madeiradata.com/author/guy Podcast: www.sqlserverradio.com Image courtesy of Mister GC / FreeDigitalPhotos.net

Let’s Begin with a Story…

What is Parameterization? Image courtesy of iosphere / FreeDigitalPhotos.net

Why is it So Important? Image courtesy of iosphere / FreeDigitalPhotos.net

When does it Become Problematic? Image courtesy of iosphere / FreeDigitalPhotos.net

How can we Deal with it? Image courtesy of iosphere / FreeDigitalPhotos.net

SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = 17;

17; SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = 17;

63; SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = 63;

@ProductID; SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = @ProductID;

The “Customers by Country” Case Study Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net

The “Customers by Country” Case Study SELECT Id , Name , LastPurchaseDate FROM Marketing.Customers WHERE Country = N'IL';

Case Study Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Plan Caching Query plans are cached in the plan cache in order to be reused and avoid the cost of recompiling the same queries again and again. Image courtesy of Gualberto107 / FreeDigitalPhotos.net

Plan Caching

Plan Caching Image courtesy of Stuart Miles / FreeDigitalPhotos.net

7 Ways To Execute Your Query

7 Ways… Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Parameter Sniffing The query optimizer “sniffs” the parameter values during first execution, and optimizes the query based on these values. Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Parameter Sniffing Local variables are not parameters, so the query optimizer has nothing to sniff. The values of local variables are only assigned at run-time. Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Parameter Sniffing Is Parameter Sniffing Good or Bad? Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Parameter Sniffing Image courtesy of stockimages / FreeDigitalPhotos.net

Parameter Sniffing Image courtesy of Ambro & artur84 / FreeDigitalPhotos.net

Parameter Sniffing Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net

Parameter Sniffing Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net

Parameter Sniffing Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Non-Uniform Data Distribution Possible Solutions: sys.sp_recompile WITH RECOMPILE OPTION (RECOMPILE) OPTION (OPTIMIZE FOR) Multiple Stored Procedures Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net

Changing Parameter Values The optimizer sniffs the parameter values at compile-time, so if you change the parameter values at run-time, the plan will not be suitable for the new values. @

Changing Parameter Values Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Simple vs. Forced Parameterization In simple parameterization, the optimizer automatically parameterizes only simple plans, in which the parameter values have absolutely no effect on the plan chosen. Simple

Simple vs. Forced Parameterization In forced parameterization, the optimizer automatically parameterizes most plans. Forced

Simple vs. Forced Parameterization Parameterization can be applied at the: Database Level Query Template Level Image courtesy of Stuart Miles & Master isolated images / FreeDigitalPhotos.net

Simple vs. Forced Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Summary Parameterization is a very important aspect of the query processor’s job when compiling and executing queries. Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Inappropriate handling of parameterization can lead to poor performance. Unfortunately, it is common to neglect this area. Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Tip #1 Usually, data distribution is more or less uniform, so, in general, prefer parameterized queries over non-parameterized queries. Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Tip #2 When data is not uniformly distributed, rewrite your code using one of the methods shown in this session. Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Tip #3 Avoid the use of local variables as an alternative for parameters. Use parameters instead. Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Tip #4 Don’t change parameter values inside your stored procedures. Pass the calculated values to inner stored procedures to use parameter sniffing or use OPTION (RECOMPILE). Image courtesy of stockimages / FreeDigitalPhotos.net

Summary Tip #5 Force parameterization only when you have no other choice. Prefer forced parameterization at the query template level. Image courtesy of stockimages / FreeDigitalPhotos.net

Back to the Story…

My Blog Series About Parameterization… http://www.madeiradata.com/tag/parameterization-series/ Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Questions? Image courtesy of Master isolated images / FreeDigitalPhotos.net

Keep in Touch… Name: Guy Glantser Email Address: guy@madeiradata.com Twitter: @guy_glantser Blog: www.madeiradata.com/author/guy Podcast: www.sqlserverradio.com Image courtesy of Mister GC / FreeDigitalPhotos.net

Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net