SQL Server: Optimizing Stored Procedure Performance
When using stored procedures, their performance is dependent on creating the optimal plan for the execution environment and making sure that it remains cached. This comprehensive course will show you all of that and more, and is applicable to SQL Server developers and anyone responsible for ensuring performance of stored procedures, from SQL Server 2005 onward.
What you'll learn
If you want to use SQL Server databases effectively, you will end up using stored procedures. Their aim is that you have optimized and compiled code that resides in a cache to improve workload performance for subsequent executions. However, you might find that it does not always work out that way.
The performance of a stored procedure is heavily dependent on how the plan is chosen and cached. If the plan is not optimal for subsequent executions it could cause performance to suffer greatly. In the end, you might suffer from what is known as parameter-sniffing-problems where the optimal plan for a procedure varies based on the parameters supplied.
This comprehensive course will teach you how stored procedure plan caching works and how to get SQL Server to produce the optimal plan for your stored procedure performance in a variety of circumstances. The course is applicable to SQL Server developers and anyone who is responsible for writing stored procedures that must repeatedly perform well.
The demo database provided is compatible with SQL Server 2008 through SQL Server 2014. All course demos are shown on SQL Server 2014 with references to behaviors (where different) on all versions. Optimization strategies you will learn in the course apply to SQL Server 2005 onward, and some even back to SQL Server 2000.
Table of contents
- Overview 2m
- Different Ways to Execute SQL Statements 2m
- Some Statements Can Be Cached for Reuse (1) 2m
- Some Statements Can Be Cached for Reuse (2) 4m
- Version-Specific Plan Caching Whitepapers 1m
- Reducing Plan Cache Pollution 5m
- Understanding sp_executesql 3m
- Stored Procedures / sp_executesql and the Cache 3m
- Parameter Sniffing 2m
- Summary: Why Use Stored Procedures? 4m
- Overview 2m
- What Happens When You Create a Procedure? 3m
- Where Are Stored Procedures Stored? (1) 2m
- Where Are Stored Procedures Stored? (2) 2m
- Creating Stored Procedures 3m
- Stored Procedure Plans and Caching 5m
- Side Effect: Plan Cache Flush (1) 3m
- Side Effect: Plan Cache Flush (2) 1m
- Side Effect: Plan Cache Flush (3) 3m
- Side Effect: Plan Cache Flush (4) 3m
- DEMO: Generic Demo Intro 2m
- DEMO: Setup Credit Sample Database 5m
- DEMO: Setup Analysis Procedures for Credit 8m
- DEMO: Object-Level Invalidation or Eviction 16m
- DEMO: Database-Level Invalidation or Eviction 4m
- DEMO: Server-Level Invalidation or Eviction 7m
- Plan Invalidation 5m
- Plan Invalidation Due to Statistics Updates 4m
- DEMO: Plan Invalidation and Statistics - Part 1 14m
- Updates to Statistics may not Invalidate Bad Plans 3m
- DEMO: Plan Invalidation and Statistics - Part 2 14m
- Plan Invalidation / Recompilation Causes 3m
- Stored Procedure Caching + Compilation Concerns 4m
- When Should You Recompile? 4m
- Summary: Creation and Compilation 3m
- EXECUTE ... WITH RECOMPILE (1) 4m
- DEMO: EXECUTE ... WITH RECOMPILE For Testing 10m
- EXECUTE ... WITH RECOMPILE (2) 4m
- DEMO: Determining Plan Stability 8m
- Statement-Level Recompilation 6m
- DEMO: Recompilation With OPTION (RECOMPILE) 4m
- DEMO: Recompilation With OPTION (Optimize For Literal) 4m
- DEMO: Recompilation With OPTION (Optimize For UNKNOWN) 9m
- DEMO: Variables Simulate Optimize For UNKNOWN 3m
- OPTION (RECOMPILE) 4m
- OPTIMIZE FOR ... 5m
- DEMO: Scenario - Setup 4m
- DEMO: Scenario - Scenario Explained 5m
- DEMO: Scenario - Conditional Logic Does NOT Work 6m
- DEMO: Scenario - EXEC With RECOMPILE 7m
- DEMO: Scenario - Conditional Logic Plus Modularization WORKS! 7m
- DEMO: Stabilizing Plans With Indexes - Setup and Scenario 5m
- DEMO: Stabilizing Plans With Indexes - Plan Stability 10m
- DEMO: Stabilizing Plans With Indexes - Voila! 10m
- Summary: Walkthrough Demo (1) 2m
- Summary: Walkthrough Demo (2) 3m
- When Does a Procedure Get Optimized? 4m
- Server-Wide: OPTIMIZE FOR UNKNOWN 3m
- The Checkered Past of OPTION (RECOMPILE) 4m
- Dynamic String Execution 3m
- DEMO: Dynamic String Execution 8m
- Multi-Purpose Procedures 3m
- Building Strings Dynamically and Caching 4m
- DEMO: Multi-Purpose Procedures - Setup and Scenario 5m
- DEMO: Multi-Purpose Procedures - The Problem 5m
- DEMO: Multi-Purpose Procedures - Option Recompile too Much CPU 3m
- DEMO: Multi-Purpose Procedures - The Ultimate Multi-Purpose Proc 17m
- Patterns and Practices in Statement Recompilation 3m
- Summary: Stored Procedure Pitfalls/Performance 1m
Course FAQ
Stored procedure is a piece of SQL code that is saved and can be reused as needed instead of writing the same SQL code over and over again. It can be used to insert, delete, or update a SQL statement, and much more.
SQL Server stored procedures help maintain data integrity through consistency. They also improve productivity because certain statements will only need to be written once instead of several times over.
Some of the different types of SQL Server Stored Procedures include:
- System Defined Stored Procedures
- Extended procedures - gives access to external programs
- User Defined Stored Procedures - made by users for their own specific actions
- CLR Stored Procedures - allows coding procedures in a .NET language
This course will be most applicable to SQL Server developers, but will be beneficial to anyone who wants to ensure productivity and performance working within SQL Server.
This course will teach you how stored procedure plan caching works and how to get SQL Server to produce the optimal plan for your stored procedure performance in a variety of circumstances.
You will learn:
- Why it is effective to use stored procedures
- How to create a stored procedure
- Where stored procedures are stored
- Stored procedure plans and caching
- When you should recompile
- Conditional logic
- Much more
Course demos are shown on SQL Server 2014, but they are applicable as far back as SQL Server 2005 through today.
Before taking this course you should be familiar with SQL Server fundamentals.