Download presentation
Presentation is loading. Please wait.
1
How to Use Parameters Like a Pro
…and Boost Performance Guy Glantser
2
A Few Words about Me… Name: Guy Glantser Address: Blog: Podcast: Image courtesy of Mister GC / FreeDigitalPhotos.net
3
Let’s Begin with a Story…
4
What is Parameterization?
Image courtesy of iosphere / FreeDigitalPhotos.net
5
Why is it So Important? Image courtesy of iosphere / FreeDigitalPhotos.net
6
When does it Become Problematic?
Image courtesy of iosphere / FreeDigitalPhotos.net
7
How can we Deal with it? Image courtesy of iosphere / FreeDigitalPhotos.net
8
SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = 17;
9
17; SELECT ProductName , ProductCategory FROM Production.Product WHERE
ProductID = 17;
10
63; SELECT ProductName , ProductCategory FROM Production.Product WHERE
ProductID = 63;
11
@ProductID; SELECT ProductName , ProductCategory FROM
Production.Product WHERE ProductID = @ProductID;
12
The “Customers by Country” Case Study
Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net
13
The “Customers by Country” Case Study
SELECT Id , Name , LastPurchaseDate FROM Marketing.Customers WHERE Country = N'IL';
14
Case Study Image courtesy of Stuart Miles / FreeDigitalPhotos.net
15
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
16
Plan Caching
17
Plan Caching Image courtesy of Stuart Miles / FreeDigitalPhotos.net
18
7 Ways To Execute Your Query
19
7 Ways… Image courtesy of Stuart Miles / FreeDigitalPhotos.net
20
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
21
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
22
Parameter Sniffing Is Parameter Sniffing Good or Bad?
Image courtesy of Stuart Miles / FreeDigitalPhotos.net
23
Parameter Sniffing Image courtesy of stockimages / FreeDigitalPhotos.net
24
Parameter Sniffing Image courtesy of Ambro & artur84 / FreeDigitalPhotos.net
25
Parameter Sniffing Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
26
Parameter Sniffing Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
27
Parameter Sniffing Image courtesy of Stuart Miles / FreeDigitalPhotos.net
28
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
29
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. @
30
Changing Parameter Values
Image courtesy of Stuart Miles / FreeDigitalPhotos.net
31
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
32
Simple vs. Forced Parameterization
In forced parameterization, the optimizer automatically parameterizes most plans. Forced
33
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
34
Simple vs. Forced Image courtesy of Stuart Miles / FreeDigitalPhotos.net
35
Summary Parameterization is a very important aspect of the query processor’s job when compiling and executing queries. Image courtesy of stockimages / FreeDigitalPhotos.net
36
Summary Inappropriate handling of parameterization can lead to poor performance. Unfortunately, it is common to neglect this area. Image courtesy of stockimages / FreeDigitalPhotos.net
37
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
38
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
39
Summary Tip #3 Avoid the use of local variables as an alternative for parameters. Use parameters instead. Image courtesy of stockimages / FreeDigitalPhotos.net
40
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
41
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
42
Back to the Story…
43
My Blog Series About Parameterization…
Image courtesy of Stuart Miles / FreeDigitalPhotos.net
44
Questions? Image courtesy of Master isolated images / FreeDigitalPhotos.net
45
Keep in Touch… Name: Guy Glantser Address: Blog: Podcast: Image courtesy of Mister GC / FreeDigitalPhotos.net
46
Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.