lucky 7: seven different solutions for bad parameter sniffing · lucky 7: seven different solutions...

16
Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software The World’s Largest Community of SQL Server Professionals

Upload: others

Post on 12-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Lucky 7: Seven Different Solutions for Bad Parameter Sniffing

Grant Fritchey, Product Evangelist

Red Gate Software

The World’s Largest Community of SQL Server Professionals

Page 2: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Goals

• Understand parameter sniffing

• Recognize bad parameter sniffing

• Learn ways to fix bad parameter sniffing

2 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 3: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Who?

• Product Evangelist for Red Gate Software

• Microsoft SQL Server MVP

• PASS Chapter President

• Author: – SQL Server Execution Plans

– SQL Server 2008 Query Performance Tuning Distilled

– SQL Server MVP Deep Dives II

3

The World’s Largest Community of SQL Server Professionals

Page 4: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Agenda

• Parameter Sniffing • Bad Parameter Sniffing • Local Variables • OPTIMIZE FOR <value> • OPTIMIZE FOR UNKOWN • WITH RECOMPILE • Statistics • Plan Guides • Turning Parameter Sniffing Off

4 The World’s Largest Community of SQL Server Professionals

Page 5: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Parameter Sniffing

• It’s a good thing… except when it isn’t

• Automatic

• Only works on parameters

• Statistics

– Average vs. Specific

• Parameter list in execution plan

5 The World’s Largest Community of SQL Server Professionals

Page 6: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Bad Parameter Sniffing

• Differentiate from Parameter Sniffing

• Still about statistics

• Intermittent

• Different plans

• Focus on the compiled value

• Compare to runtime

• When it’s bad, it’s very bad

6 The World’s Largest Community of SQL Server Professionals

Page 7: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Local Variables

• Eliminate parameters

• Turn parameters into local variables

• Produces “generic” plan

7 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 8: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

OPTIMIZE FOR <value>

• Specific and accurate

• Changes over time

• Produces “precise” plan

8 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 9: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

OPTIMIZE FOR UNKNOWN

• For when you’re not sure

• Changes over time

• Produces “generic” plan

9 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 10: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

WITH RECOMPILE

• Specific every time

• Increases overhead

• May be more costly than the parameter sniffing

• Produces “precise” plan

10 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 11: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Update Statistics

• After all, it’s all about the statistics

• Stats can age without updating

• You may have auto-update turned off

• Sampled updates may be inadequate

11 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 12: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Plan Guides

• Just a different way to use hints

• Produces whatever plan you define

12 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 13: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Turn Sniffing Off

• Dangerous choice

• Last for a reason

• Turns it all off

• Everywhere

• Did I mention it’s dangerous?

13 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 14: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Goals

• Understand Parameter Sniffing

• Recognize Bad Parameter Sniffing

• Learn Ways to Fix Bad Parameter Sniffing

14 The World’s Largest Community of SQL Server Professionals Certain images and/or photos on this page are the copyrighted property of 123RF Limited, their Contributors or Licensed Partners and are being used with permission under license. These images and/or photos may not be copied or downloaded without permission from 123RF Limited.

Page 15: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Questions?

The World’s Largest Community of SQL Server Professionals 15

Page 16: Lucky 7: Seven Different Solutions for Bad Parameter Sniffing · Lucky 7: Seven Different Solutions for Bad Parameter Sniffing Grant Fritchey, Product Evangelist Red Gate Software

Thanks for Attending

Visit www.sqlservercentral.com for free SQL Server eBooks, articles, videos, blogs, news, and more.

Please Don’t Forget to Turn in Your Evaluations

The World’s Largest Community of SQL Server Professionals