how to use parameters like a pro …and boost performance guy glantser, ceo, madeira

50
How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Upload: phillip-bennett

Post on 17-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

How to Use Parameters Like a

Pro…and Boost Performance

Guy Glantser, CEO, Madeira

Page 2: 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

Page 3: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

3

Please silence cell phones

Page 5: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Let’s Beginwith a Story…

5

Page 6: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Once Upon a Time…Video

Page 7: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

What is Parameterization?

Image courtesy of iosphere / FreeDigitalPhotos.net7

Page 8: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Why is it So Important?

Image courtesy of iosphere / FreeDigitalPhotos.net8

Page 9: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

When does it Become Problematic?

Image courtesy of iosphere / FreeDigitalPhotos.net9

Page 10: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

How can we Deal with it?

Image courtesy of iosphere / FreeDigitalPhotos.net10

Page 11: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

SELECTProductName ,ProductCategory

FROMProduction.Product

WHEREProductID = 17;

11

Page 12: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

SELECTProductName ,ProductCategory

FROMProduction.Product

WHEREProductID = 17;

12

Page 13: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

SELECTProductName ,ProductCategory

FROMProduction.Product

WHEREProductID = 63;

13

Page 14: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

SELECTProductName ,ProductCategory

FROMProduction.Product

WHEREProductID = @ProductID;

14

Page 15: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

The “Customers by Country” Case Study

Image courtesy of Salvatore Vuono / FreeDigitalPhotos.net15

Page 16: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

The “Customers by Country” Case Study

16

SELECTId ,Name ,LastPurchaseDate

FROMMarketing.Customers

WHERECountry = N'IL';

Page 17: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Case StudyDemo

Page 18: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 19: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Plan Caching

19

Page 20: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Plan CachingDemo

Page 21: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

7 Ways To Execute Your Query

21

Page 22: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

7 Ways…Demo

Page 23: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 24: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 25: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter Sniffing

25Image courtesy of Stuart Miles / FreeDigitalPhotos.net

Is Parameter SniffingGood or Bad?

Page 26: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter Sniffing

26Image courtesy of stockimages / FreeDigitalPhotos.net

Page 27: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter Sniffing

27Image courtesy of Ambro & artur84 / FreeDigitalPhotos.net

Page 28: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter Sniffing

28Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net

Page 29: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter Sniffing

29Image courtesy of tigger11th & artur84 / FreeDigitalPhotos.net

Page 30: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Parameter SniffingDemo

Page 31: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 32: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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. @@

Page 33: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Changing Parameter ValuesDemo

Page 34: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 35: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Simple vs. Forced Parameterization

35

In forced parameterization, the optimizer automatically parameterizes most plans.

Force

d

Page 36: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 37: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Simple vs. ForcedDemo

Page 38: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 39: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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

Page 40: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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.

Page 41: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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.

Page 42: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Summary

42Image courtesy of stockimages / FreeDigitalPhotos.net

Tip #3Avoid the use of local variables as an alternative for parameters.Use parameters instead.

Page 43: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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.

Page 44: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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.

Page 45: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Back to the Story…

45

Page 46: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

My Blog Series About Parameterization…

46Image courtesy of Stuart Miles / FreeDigitalPhotos.net

http://www.madeiradata.com/tag/parameterization-s

eries/

Page 47: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

Questions?

47Image courtesy of Master isolated images / FreeDigitalPhotos.net

Page 48: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

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.

Page 49: 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

Keep in Touch…

Image courtesy of Mister GC / FreeDigitalPhotos.net49

Page 50: How to Use Parameters Like a Pro …and Boost Performance Guy Glantser, CEO, Madeira

50Image courtesy of David Castillo Dominici / FreeDigitalPhotos.net