How To Use Parameters Like a Pro And Boost Performance Guy Glantser SQL Saturday Sydney 18th February 2017 Name: Email Address: Twitter: Blog: Podcast: Guy Glantser [email protected] @guy_glantser www.madeiradata.com/author/guy 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; SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = ; 17 SELECT ProductName , ProductCategory FROM Production.Product WHERE ProductID = ; 63 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 vs. Forced Parameterization In forced parameterization, the optimizer automatically parameterizes most plans. 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? Please make sure you visit our fantastic sponsors: Name: Email Address: Twitter: Blog: Podcast: Guy Glantser [email protected] @guy_glantser www.madeiradata.com/author/guy www.sqlserverradio.com Image courtesy of Mister GC / FreeDigitalPhotos.net Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net
© Copyright 2026 Paperzz