optimizing procedural code - sqlskills.com€¦ · instructor for multiple rotations of both the...

12
.Net Rocks! Visual Studio 2012 Launch Road Trip Optimizing Procedural Code Kimberly L. Tripp [email protected] .Net Rocks! Visual Studio 2012 Launch Road Trip Optimizing Procedural Code Kimberly L. Tripp [email protected]

Upload: others

Post on 21-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

.Net Rocks!Visual Studio 2012 Launch Road Trip

Optimizing Procedural CodeKimberly L. Tripp

[email protected]

.Net Rocks!Visual Studio 2012 Launch Road Trip

Optimizing Procedural CodeKimberly L. Tripp

[email protected]

Page 2: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Kimberly L. TrippKimberly L. Tripp

Consultant/Trainer/Speaker/WriterPresident/Founder, SYSolutions, Inc. 

e‐mail: [email protected]: http://www.SQLskills.com/blogs/KimberlyTwitter: @KimberlyLTripp

Author/Instructor for SQL Server Immersion EventsInstructor for multiple rotations of both the SQL MCM & Sharepoint MCMAuthor/Manager of SQL Server 2005 Launch Content, co‐author/Manager for SQL Server 2008 Jumpstart Content, Author/Speaker at TechEd, SQL Connections, SQLPASS, ITForum, Conference Co‐chair for SQL ConnectionsAuthor of several SQL Server Whitepapers on MSDN/TechNet: Partitioning, Snapshot Isolation, Manageability, SQLCLR for DBAsAuthor/Presenter for more than 25 online webcasts on MSDN and TechNet (two series and other individual webcasts)Co‐author MSPress Title: SQL Server 2008 Internals, the SQL Server MVP Project (1 & 2), and SQL Server 2000 High AvailabilityPresenter/Technical Manager for SQL Server HOL DVDsI love this stuff… feel free to ask questions! 

Consultant/Trainer/Speaker/WriterPresident/Founder, SYSolutions, Inc. 

e‐mail: [email protected]: http://www.SQLskills.com/blogs/KimberlyTwitter: @KimberlyLTripp

Author/Instructor for SQL Server Immersion EventsInstructor for multiple rotations of both the SQL MCM & Sharepoint MCMAuthor/Manager of SQL Server 2005 Launch Content, co‐author/Manager for SQL Server 2008 Jumpstart Content, Author/Speaker at TechEd, SQL Connections, SQLPASS, ITForum, Conference Co‐chair for SQL ConnectionsAuthor of several SQL Server Whitepapers on MSDN/TechNet: Partitioning, Snapshot Isolation, Manageability, SQLCLR for DBAsAuthor/Presenter for more than 25 online webcasts on MSDN and TechNet (two series and other individual webcasts)Co‐author MSPress Title: SQL Server 2008 Internals, the SQL Server MVP Project (1 & 2), and SQL Server 2000 High AvailabilityPresenter/Technical Manager for SQL Server HOL DVDsI love this stuff… feel free to ask questions! 

2

Page 3: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

SQLskillsSQLskills

Team of world‐renowned SQL Server expertsPaul S. Randal @PaulRandalJonathan Kehayias @SQLPoolBoyJoseph Sack @JosephSackGlenn Berry @GlennAlanBerryErin Stellato @ErinStellato

Instructor‐led training  Immersion EventsOnline training  Pluralsight (http://pluralsight.com/) Consulting: health checks, hardware, performanceRemote DBABecome a SQLskills Insider

http://www.sqlskills.com/JoinCommunity.asp

Team of world‐renowned SQL Server expertsPaul S. Randal @PaulRandalJonathan Kehayias @SQLPoolBoyJoseph Sack @JosephSackGlenn Berry @GlennAlanBerryErin Stellato @ErinStellato

Instructor‐led training  Immersion EventsOnline training  Pluralsight (http://pluralsight.com/) Consulting: health checks, hardware, performanceRemote DBABecome a SQLskills Insider

http://www.sqlskills.com/JoinCommunity.asp

Page 4: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

OverviewOverview

Back to the basics…Never say never, never say always…

It depends!Problem area to highlight

Parameter sniffingWhat is it?When/why does it become problematic?

Recompilation in proceduresWhat to recompile?How to effectively use statement‐level recompilation

Back to the basics…Never say never, never say always…

It depends!Problem area to highlight

Parameter sniffingWhat is it?When/why does it become problematic?

Recompilation in proceduresWhat to recompile?How to effectively use statement‐level recompilation

4

Page 5: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Processing Stored ProceduresProcessing Stored Procedures

Compiled plan placed inunified plan cache

Re‐used on subsequent executionsparameter sniffing PROBLEMS

Execution(when a plan does not 

already exist in cache)

Resolution*

Optimization

Parsing

ResolutionCreation

A procedure's plan is NOT saved to disk; only metadata is saved 

at procedure creationUse sys.procedures, sp_procs, functions and views to 

see metadata

5

“sniffing” runtime Compilation

“sniffing” runtime params

Page 6: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Procedure CachingIsn’t That the Point?Procedure CachingIsn’t That the Point?

Reusing plans can be goodWhen different parameters don’t change the optimal plan, then saving and reusing is excellent!SQL Server saves time in compilation

Reusing plans can be VERY badWhen different parameters wildly change the size of the result set and the optimal plans vary, then reusing the plan can be horribly badIf indexes are added to base tables, existing plans may not leverage them

Don’t worry, there’s a simple solution here:EXEC sp_recompile <tablename>

Reusing plans can be goodWhen different parameters don’t change the optimal plan, then saving and reusing is excellent!SQL Server saves time in compilation

Reusing plans can be VERY badWhen different parameters wildly change the size of the result set and the optimal plans vary, then reusing the plan can be horribly badIf indexes are added to base tables, existing plans may not leverage them

Don’t worry, there’s a simple solution here:EXEC sp_recompile <tablename>

6

Page 7: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Recompilation IssuesRecompilation Issues

RECOMPILATION = OPTIMIZATIONOPTIMIZATION = RECOMPILATION

When do you want to recompile?What options do you have for recompilation – and at what granularity?How do you know you need to recompile?Do you want to recompile the entire procedure or only part of it?Can you test it?

RECOMPILATION = OPTIMIZATIONOPTIMIZATION = RECOMPILATION

When do you want to recompile?What options do you have for recompilation – and at what granularity?How do you know you need to recompile?Do you want to recompile the entire procedure or only part of it?Can you test it?

7

Page 8: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Options for RecompilationOptions for Recompilation

CREATE … WITH RECOMPILEEXECUTE … WITH RECOMPILEsp_recompile objnameStatement‐level recompilation

The 2000+ ways (still have benefits)Dynamic string execution (statement’s plan not stored with procedure’s plan)Modularized code (reduced sniffing problems with conditional logic/branching)

Better way to change compilation/optimization2005+: OPTION(RECOMPILE)2005+: OPTION (OPTIMIZE FOR

(@variable_name = constant, ...) )2008+: OPTION (OPTIMIZE FOR UNKNOWN)

CREATE … WITH RECOMPILEEXECUTE … WITH RECOMPILEsp_recompile objnameStatement‐level recompilation

The 2000+ ways (still have benefits)Dynamic string execution (statement’s plan not stored with procedure’s plan)Modularized code (reduced sniffing problems with conditional logic/branching)

Better way to change compilation/optimization2005+: OPTION(RECOMPILE)2005+: OPTION (OPTIMIZE FOR

(@variable_name = constant, ...) )2008+: OPTION (OPTIMIZE FOR UNKNOWN)

8

Page 9: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Statement‐Level RecompilationStatement‐Level Recompilation

In 2005 and 2008: “inline” recompilation for statementsOPTION (RECOMPILE)

Excellent when parameters cause the execution plan to widely varyBad because EVERY execution will recompile – but only for the statements

OPTION (OPTIMIZE FOR (@variable = literal, ...))

Excellent when large majority of executions generate the same optimization timeYou don’t care that the minority may run slower with a less than optimal plan?

2008 only: OPTION (OPTIMIZE FOR UNKNOWN)Use the “all density” (average) instead of the histogram

In 2005 and 2008: “inline” recompilation for statementsOPTION (RECOMPILE)

Excellent when parameters cause the execution plan to widely varyBad because EVERY execution will recompile – but only for the statements

OPTION (OPTIMIZE FOR (@variable = literal, ...))

Excellent when large majority of executions generate the same optimization timeYou don’t care that the minority may run slower with a less than optimal plan?

2008 only: OPTION (OPTIMIZE FOR UNKNOWN)Use the “all density” (average) instead of the histogram

9

Page 10: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

RecompilationRecompilation

Demo

Page 11: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Stored Procedure Resources Stored Procedure Resources 

Plan Caching in SQL Server 2008http://msdn.microsoft.com/en‐us/library/ee343986.aspx

Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005

http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx

PSS SQL Server Engine Bloghttp://blogs.msdn.com/psssql/default.aspx

KB 243586: Troubleshooting Stored Procedure RecompilationKB 308737: How to identify the cause of recompilation in an SP:Recompile event (SQL Server 2000 SP2+ has 6 subclass values, SQL Server 2005 has 11 subclass values and SQL Server 2008 RTM has 14 subclass values)KB 263889: Description of SQL Server blocking caused by compile locks

Plan Caching in SQL Server 2008http://msdn.microsoft.com/en‐us/library/ee343986.aspx

Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005

http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx

PSS SQL Server Engine Bloghttp://blogs.msdn.com/psssql/default.aspx

KB 243586: Troubleshooting Stored Procedure RecompilationKB 308737: How to identify the cause of recompilation in an SP:Recompile event (SQL Server 2000 SP2+ has 6 subclass values, SQL Server 2005 has 11 subclass values and SQL Server 2008 RTM has 14 subclass values)KB 263889: Description of SQL Server blocking caused by compile locks

11

Page 12: Optimizing Procedural Code - SQLskills.com€¦ · Instructor for multiple rotations of both the SQL MCM & Sharepoint MCM Author/Manager of SQL Server 2005 Launch Content, co‐author/Manager

SQLskills.comhttp://www.SQLskills.com

Plan Cache Pollution ResourcesPlan Cache Pollution Resources

Blog posts:Plan cache and optimizing for adhoc workloads Plan cache, adhoc workloads and clearing the single‐use plan cache bloatClearing the cache ‐ are there other options?Statement execution and why you should use stored proceduresCategory: Optimizing Procedural Code

http://www.sqlskills.com/BLOGS/KIMBERLY/category/Optimizing‐Procedural‐Code.aspx

MSDN Article: How Data Access Code Affects Database Performance, Bob Beauchemin

http://msdn.microsoft.com/en‐us/magazine/ee236412.aspx

Blog posts:Plan cache and optimizing for adhoc workloads Plan cache, adhoc workloads and clearing the single‐use plan cache bloatClearing the cache ‐ are there other options?Statement execution and why you should use stored proceduresCategory: Optimizing Procedural Code

http://www.sqlskills.com/BLOGS/KIMBERLY/category/Optimizing‐Procedural‐Code.aspx

MSDN Article: How Data Access Code Affects Database Performance, Bob Beauchemin

http://msdn.microsoft.com/en‐us/magazine/ee236412.aspx

12