Download presentation
Presentation is loading. Please wait.
Published byPhillip Bennett Modified over 9 years ago
1
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira
2
Name:Guy Glantser Email Address:guy@madeiradata.comguy@madeiradata.com Twitter:@guy_glantser@guy_glantser Blog:www.madeiradata.com/author/guyglantserwww.madeiradata.com/author/guyglantser Podcast:www.sqlserverradio.comwww.sqlserverradio.com Image courtesy of Mister GC / FreeDigitalPhotos.net 2
3
3 Please silence cell phones 3
4
4 Explore Everything PASS Has to Offer FREE SQL SERVER AND BI WEB EVENTS FREE 1-DAY TRAINING EVENTS REGIONAL EVENT LOCAL USER GROUPS AROUND THE WORLD FREE ONLINE TECHNICAL TRAINING THIS IS COMMUNITYBUSINESS ANALYTICS TRAINING SESSION RECORDINGSPASS NEWSLETTER 4
5
Let’s Begin with a Story… 5
6
Once Upon a Time… Video
7
What is Parameterization? Image courtesy of iosphere / FreeDigitalPhotos.net 7
8
Why is it So Important? Image courtesy of iosphere / FreeDigitalPhotos.net 8
9
When does it Become Problematic? Image courtesy of iosphere / FreeDigitalPhotos.net 9
10
How can we Deal with it? Image courtesy of iosphere / FreeDigitalPhotos.net 10
11
SELECT ProductName, ProductCategory FROM Production.Product WHERE ProductID = 17; 11
12
SELECT ProductName, ProductCategory FROM Production.Product WHERE ProductID = 17 ; 12
13
SELECT ProductName, ProductCategory FROM Production.Product WHERE ProductID = 63 ; 13
14
SELECT ProductName, ProductCategory FROM Production.Product WHERE ProductID = @ProductID ; 14
15
The “Customers by Country” Case Study Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net 15
16
The “Customers by Country” Case Study 16 SELECT Id, Name, LastPurchaseDate FROM Marketing.Customers WHERE Country = N'IL';
17
Case Study Demo
18
Plan Caching 18 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
19
Plan Caching 19
20
Plan Caching Demo
21
7 Ways To Execute Your Query 21
22
7 Ways… Demo
23
Parameter Sniffing 23 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
24
Parameter Sniffing 24 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
25
Parameter Sniffing 25 Image courtesy of Stuart Miles / FreeDigitalPhotos.net Is Parameter Sniffing Good or Bad?
26
Parameter Sniffing 26 Image courtesy of stockimages / FreeDigitalPhotos.net
27
Parameter Sniffing 27 Image courtesy of Ambro & artur84 / FreeDigitalPhotos.net
28
Parameter Sniffing 28 Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
29
Parameter Sniffing 29 Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
30
Parameter Sniffing Demo
31
Non-Uniform Data Distribution 31 Possible Solutions: sys.sp_recompile WITH RECOMPILE OPTION (RECOMPILE) OPTION (OPTIMIZE FOR) Multiple Stored Procedures Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net
32
Changing Parameter Values 32 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.
33
Changing Parameter Values Demo
34
Simple vs. Forced Parameterization 34 In simple parameterization, the optimizer automatically parameterizes only simple plans, in which the parameter values have absolutely no effect on the plan chosen.
35
Simple vs. Forced Parameterization 35 In forced parameterization, the optimizer automatically parameterizes most plans.
36
Simple vs. Forced Parameterization 36 Parameterization can be applied at the: Database Level Query Template Level Image courtesy of Stuart Miles & Master isolated images / FreeDigitalPhotos.net
37
Simple vs. Forced Demo
38
Summary 38 Parameterization is a very important aspect of the query processor’s job when compiling and executing queries. Image courtesy of stockimages / FreeDigitalPhotos.net
39
Summary 39 Inappropriate handling of parameterization can lead to poor performance. Unfortunately, it is common to neglect this area. Image courtesy of stockimages / FreeDigitalPhotos.net
40
Summary 40 Image courtesy of stockimages / FreeDigitalPhotos.net Tip #1 Usually, data distribution is more or less uniform, so, in general, prefer parameterized queries over non-parameterized queries.
41
Summary 41 Image courtesy of stockimages / FreeDigitalPhotos.net Tip #2 When data is not uniformly distributed, rewrite your code using one of the methods shown in this session.
42
Summary 42 Image courtesy of stockimages / FreeDigitalPhotos.net Tip #3 Avoid the use of local variables as an alternative for parameters. Use parameters instead.
43
Summary 43 Image courtesy of stockimages / FreeDigitalPhotos.net Tip #4 Don’t change parameter values inside your stored procedures. Pass the calculated values to inner stored procedures to use parameter sniffing.
44
Summary 44 Image courtesy of stockimages / FreeDigitalPhotos.net Tip #5 Force parameterization only when you have no other choice. Prefer forced parameterization at the query template level.
45
Back to the Story… 45
46
My Blog Series About Parameterization… 46 Image courtesy of Stuart Miles / FreeDigitalPhotos.net http://www.madeiradata.com/tag/parameterization-series/
47
Questions? 47 Image courtesy of Master isolated images / FreeDigitalPhotos.net
48
48 Session Evaluations ways to access Go to passsummit.com/evals Download the GuideBook App and search: PASS Summit 2015 Follow the QR code link displayed on session signage throughout the conference venue and in the program guide Submit by 5pm Friday November 6 th to WIN prizes Your feedback is important and valuable. 48
49
Name:Guy Glantser Email Address:guy@madeiradata.comguy@madeiradata.com Twitter:@guy_glantser@guy_glantser Blog:www.madeiradata.com/author/guyglantserwww.madeiradata.com/author/guyglantser Podcast:www.sqlserverradio.comwww.sqlserverradio.com Image courtesy of Mister GC / FreeDigitalPhotos.net 49
50
50 Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.