SQL Server: Optimizing Ad Hoc Statement Performance
When using ad hoc T-SQL statements, their performance is dependent on you choosing the correct execution method to ensure they are cached correctly and their plans are reused appropriately, and this course will show you all of that and more. This course is applicable to SQL Server developers and anyone who is responsible for writing ad hoc T-SQL statements that must perform well, from SQL Server 2005 onward
What you'll learn
This course is about how different ad hoc statement execution methods affect caching, plan reuse, memory and ultimately performance. Knowing when to use each method is important and understanding how SQL Server works will demystify certain behaviors you may have seen but previously have been unable to explain. SQL Server can support any workload, any design, and any data requests but knowing exactly which one is the most beneficial to use can give you better long-term scalability, availability, and performance. Using the wrong method can cause more memory to be wasted and even result in parameter sniffing problems (where subsequent statements perform poorly because of the plan that’s been cached). This course will show you how each statement execution method works, how it’s cached, whether or not it wastes cache, and finally how to test and rewrite the statement to take better advantage of caching. Along the way we will also cover a variety of other necessary features and tools: estimates, statistics, and heuristics; how to analyze query plans; some indexing strategies to improve performance; and plan guides. This course is an absolute must for everyone that works with SQL Server and it’s also an introduction to concepts that will be built upon in future courses. This course is applicable to all SQL Server versions from SQL Server 2005 onward, and for SQL Server developers as well as anyone responsible for writing data access statements to SQL Server tables. You can have any level of experience to gain from this course but those of you who have experienced what seemed odd behavior/performance with your ad hoc statements will probably benefit the most!
Table of contents
- Introduction 1m
- Different Ways to Execute SQL Statements 4m
- Understanding Ad Hoc Statements 5m
- Understanding sp_executesql 3m
- Understanding Dynamic String Execution 5m
- Dynamic String Execution and SQL Injection 5m
- Demo: Credit Sample Database Setup for This Course 4m
- Demo: Setting Up For Analyzing Cache 8m
- Demo: Part 1 - Ad Hoc Safe Statements 9m
- Demo: Part 2 - Ad Hoc Unsafe Statements 7m
- Demo: Part 3 - Ad Hoc Safe and Unsafe with Variables 4m
- Demo: Part 4 - sp_executesql with Safe Statement 4m
- Demo: Part 5 - sp_executesql with Unsafe Statement 9m
- Demo: Part 6 - Dynamic String Execution with Safe Statement 8m
- Demo: Part 7 - Dynamic String Execution with Unsafe Statement 7m
- Summary: Statement Execution Methods 7m
- Introduction 2m
- Statement Execution Simplified 4m
- Cost-Based Optimization 4m
- Understanding Selectivity 6m
- Demo: Setup and First Look at Statistics 12m
- Demo: Updates and Estimates 11m
- Demo: Ad Hoc Statements and Variables 6m
- Demo: When No Statistics Exist then Heuristics are Used 10m
- Demo Summary: Estimates and Selectivity 3m
- How Do You See Statistics? 5m
- What Do Statistics Tell Us About Our Data? (1) 3m
- What Do Statistics Tell Us About Our Data? (2) 7m
- What Do Statistics Tell Us About Our Data? (3) 2m
- What Do Statistics Tell Us About Our Data? (4) 3m
- Demo: Reading the Histogram 14m
- When and How Does SQL Server Use Statistics? 4m
- Summary: Estimates and Selectivity 3m
- Introduction 2m
- What Affects Ad Hoc Statement Behavior? 2m
- Default Ad Hoc Statement Behavior 3m
- Ad Hoc Statement Textual Matching 3m
- Ad Hoc Statements: Safe vs. Unsafe 5m
- Ad Hoc Statement Caching 8m
- Demo: Ad Hoc Statements and the Plan Cache 13m
- Verifying Plans in Cache NOW 4m
- Analyzing the Plan Cache 3m
- Demo: query_hash and query_plan_hash 10m
- Changing Ad Hoc Statement Behavior (1) 2m
- Changing Ad Hoc Statement Behavior (2) 7m
- Multiple Plans (Tipping/Covering) 4m
- Demo: Part 1 - Making a Statement Safe with Covering 9m
- Demo: Part 2 - The Right Way to Force Statements 13m
- Summary: Statement Caching 4m
- Plan Cache Pollution 2m
- Ad Hoc Plan Cache Pollution Defined 7m
- Plan Cache Stores 2m
- Verifying State of Plan Cache 1m
- Demo: Analyzing for Plan Cache Pollution - Setup 5m
- Demo: Analyzing for Plan Cache Pollution 15m
- Balancing Plan Cache Pollution, CPU, and PSP (1) 5m
- Demo: Part 1a - Optimize for Adhoc Workloads 8m
- Demo: Part 1b - Covering to Make a Query Safe 11m
- Balancing Plan Cache Pollution, CPU, and PSP (2) 3m
- Balancing Plan Cache Pollution, CPU, and PSP (3) 3m
- Balancing Plan Cache Pollution, CPU, and PSP (4) 2m
- Demo: Part 2a - Forcing a Stable Statement With sp_executesql 4m
- Demo: Part 2b - Forcing a Stable Statement with a Plan Guide 6m
- Demo: Part 2c - Optimizing an Expensive Statement 7m
- Balancing Plan Cache Pollution, CPU, and PSP (5) 4m
- Demo: Part 3 - Clearing Single-Use Plan Cache Pollution 7m
- Alternatives to Ad Hoc Statements 2m
- Demo: Summary of All Script Executions 5m
- Summary: Plan Cache Pollution 3m
- Statement Execution Summary 1m
- Statement Execution, Estimates, and Caching (1) 3m
- Statement Execution, Estimates, and Caching (2) 3m
- Statement Execution Methods, Caching, and Concerns 3m
- Bringing It All Together 6m
- Statement Execution Solutions (1) 2m
- Statement Execution Solutions (2) 3m
- Statement Execution Solutions (3) 4m
- Summary: Statement Execution 2m
- Just the Tip of the Iceberg 2m
- Where to Go Next and Final Summary 4m