how to use parameters like a pro …and boost performance guy glantser, ceo, madeira
TRANSCRIPT
How to Use Parameters Like a
Pro…and Boost Performance
Guy Glantser, CEO, Madeira
Name: Guy GlantserEmail Address: [email protected]: @guy_glantserBlog:
www.madeiradata.com/author/guyglantserPodcast: www.sqlserverradio.com
A Few Words about Me…
Image courtesy of Mister GC / FreeDigitalPhotos.net2
3
Please silence cell phones
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 COMMUNITY BUSINESS ANALYTICS TRAINING
SESSION RECORDINGS PASS NEWSLETTER
Let’s Beginwith a Story…
5
Once Upon a Time…Video
What is Parameterization?
Image courtesy of iosphere / FreeDigitalPhotos.net7
Why is it So Important?
Image courtesy of iosphere / FreeDigitalPhotos.net8
When does it Become Problematic?
Image courtesy of iosphere / FreeDigitalPhotos.net9
How can we Deal with it?
Image courtesy of iosphere / FreeDigitalPhotos.net10
SELECTProductName ,ProductCategory
FROMProduction.Product
WHEREProductID = 17;
11
SELECTProductName ,ProductCategory
FROMProduction.Product
WHEREProductID = 17;
12
SELECTProductName ,ProductCategory
FROMProduction.Product
WHEREProductID = 63;
13
SELECTProductName ,ProductCategory
FROMProduction.Product
WHEREProductID = @ProductID;
14
The “Customers by Country” Case Study
Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net15
The “Customers by Country” Case Study
16
SELECTId ,Name ,LastPurchaseDate
FROMMarketing.Customers
WHERECountry = N'IL';
Case StudyDemo
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
Plan Caching
19
Plan CachingDemo
7 Ways…Demo
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
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
Parameter Sniffing
25Image courtesy of Stuart Miles / FreeDigitalPhotos.net
Is Parameter SniffingGood or Bad?
Parameter Sniffing
26Image courtesy of stockimages / FreeDigitalPhotos.net
Parameter Sniffing
27Image courtesy of Ambro & artur84 / FreeDigitalPhotos.net
Parameter Sniffing
28Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
Parameter Sniffing
29Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net
Parameter SniffingDemo
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
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. @@
Changing Parameter ValuesDemo
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.
Simpl
e
Simple vs. Forced Parameterization
35
In forced parameterization, the optimizer automatically parameterizes most plans.
Force
d
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
Simple vs. ForcedDemo
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
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
Summary
40Image courtesy of stockimages / FreeDigitalPhotos.net
Tip #1Usually, data distribution is more or less uniform, so, in general, prefer parameterized queries over non-parameterized queries.
Summary
41Image courtesy of stockimages / FreeDigitalPhotos.net
Tip #2When data is not uniformly distributed, rewrite your code using one of the methods shown in this session.
Summary
42Image courtesy of stockimages / FreeDigitalPhotos.net
Tip #3Avoid the use of local variables as an alternative for parameters.Use parameters instead.
Summary
43Image courtesy of stockimages / FreeDigitalPhotos.net
Tip #4Don’t change parameter values inside your stored procedures.Pass the calculated values to inner stored procedures to use parameter sniffing.
Summary
44Image courtesy of stockimages / FreeDigitalPhotos.net
Tip #5Force parameterization only when you have no other choice.Prefer forced parameterization at the query template level.
Back to the Story…
45
My Blog Series About Parameterization…
46Image courtesy of Stuart Miles / FreeDigitalPhotos.net
http://www.madeiradata.com/tag/parameterization-s
eries/
Questions?
47Image courtesy of Master isolated images / FreeDigitalPhotos.net
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 5pmFriday November 6th toWIN prizes
Your feedback is important and valuable.
Name: Guy GlantserEmail Address: [email protected]: @guy_glantserBlog:
www.madeiradata.com/author/guyglantserPodcast: www.sqlserverradio.com
Keep in Touch…
Image courtesy of Mister GC / FreeDigitalPhotos.net49
50Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net