lucky 7: seven different solutions for bad parameter sniffing · lucky 7: seven different solutions...
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/8.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.site/reader036/viewer/2022062607/6045352e20983f6277201b24/html5/thumbnails/16.jpg)
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