Stored Procedures – Facts and Myths

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL Performance 2011/12 Joe Chang, SolidQ
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Database Systems More SQL Database Design -- More SQL1.
SQL Server Stored Procedures Architecture & Performance Victor Isakov MCT, CTT, MSCE, MCDBA
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
Chapter Oracle Server An Oracle Server consists of an Oracle database (stored data, control and log files.) The Server will support SQL to define.
CSE314 Database Systems More SQL: Complex Queries, Triggers, Views, and Schema Modification Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software.
DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
© Dennis Shasha, Philippe Bonnet – 2013 Communicating with the Outside.
Module 9 Designing and Implementing Stored Procedures.
Triggers A Quick Reference and Summary BIT 275. Triggers SQL code permits you to access only one table for an INSERT, UPDATE, or DELETE statement. The.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
3-Tier Client/Server Internet Example. TIER 1 - User interface and navigation Labeled Tier 1 in the following graphic, this layer comprises the entire.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
Module 11: Managing Transactions and Locks
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Pinal Dave Mentor | Solid Quality India |
Maciej Pilecki Consultant Project Botticelli Ltd. DAT404.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
SQL Triggers, Functions & Stored Procedures Programming Operations.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
A deep dive into SQL Server Plan Cache Management.
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
SQL Server Magic Buttons! What are Trace Flags and why should I care? Steinar Andersen, SQL Service Nordic AB Thanks to Thomas Kejser for peer-reviewing.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Server Statistics and its relationship with Query Optimizer
Parameter Sniffing in SQL Server Stored Procedures
SQL Environment.
Cleveland SQL Saturday Catch-All or Sometimes Queries
Query Optimization Techniques
Module 11: File Structure
Dynamic SQL Writing Efficient Queries on the Fly
Query Tuning without Production Data
Dynamic SQL: Writing Efficient Queries on the Fly
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Query Tuning without Production Data
Dynamic SQL Writing Efficient Queries on the Fly
Database Performance Tuning and Query Optimization
Peeking into the Plan Cache with SQL Server 2008
Query Execution Expectation-Reality Denis Reznik
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
Query Optimization Techniques
Statistics: What are they and How do I use them
Dynamic SQL: Writing Efficient Queries on the Fly
TEMPDB – INTERNALS AND USAGE
Hugo Kornelis Now where does THAT estimate come from? The nuts and bolts of cardinality estimation.
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Parameter Sniffing on SQL Server
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
Query Tuning Fundamentals
Diving into Query Execution Plans
SQL Server Query Design and Optimization Recommendations
Prof. Arfaoui. COM390 Chapter 7
Query Optimization Techniques
Presentation transcript:

Stored Procedures – Facts and Myths

About Me Over 15 years’ experience with SQL Server Trainer & Consultant Books & Articles Writer OLTP and DW/BI Systems Architect SQL Microsoft Most Valuable Professional since 2006 Founder of SQLExpert.pl www.sqlexpert.pl www.blog.sqlexpert.pl http://www.facebook.com/SQLExpertpl marcin@sqlexpert.pl 2 |

Agenda Is Plan Caching and Reuse a Good Thing? What is its cost in terms of time, memory and concurrency? Are there other options beside stored procedures? Can You Avoid Recompilations? Will putting all DML statements at the beginning help? What is the impact of changing SET options? Shoud you control recomplilations due to statistics changes? When Plan Reusing is a Bad Thing? Is Parameter Sniffing a bug or a feature? Does a conditional logic hurt SPs performance? Can a SP name affect performance? 3 |

Is Plan Caching and Reuse a Good Thing? Stored procedures Are a convenient container for code reuse They can be called many times and the query plans can be reused, saving the time, CPU and memory Are optimized and compiled during their first execution Optimization is based on parameters supplied during this execution Future executions will reuse the query plan stored in plan cache There are at most two instances of a query plan at any time in plan cache: One for all of the serial executions One for all of the parallel executions From a query plan, an execution context is derived Execution contexts hold the values needed for a specific execution of a query plan, and they are also cached and reused

Does Compilation/Recompilation Really Matter? In terms of time Compilation is really expensive and CPU-heavy process In terms of memory Storing large number of execution plans the procedure cache “steals” memory from the Buffer Pool The currently used formula looks like this: 75% 0-4GB + 10% 4-64GB + 5% 64GB On 16GB box 4.2 GB may be used for the procedure cache In terms of concurrency Getaways throttle simultaneous compilation to really small number Three of them: Small - 4 x number of logical CPUs allocated to SQL Server Medium - Number of logical CPUs allocated to SQL Server Large - 1 Look for 'RESOURCE_SEMAPHORE_QUERY_COMPILE' wait

Does Compilation/Recompilation Really Matter? Measuring compilation cost, in terms of time and memory Looking inside the Procedure Cache YES, IT DOES

Are There Other Options ? SQL Server can cache query plans for different types of batches beside ad-hoc queries and stored procedures: Query preparation ODBC and OLE DB expose this functionality via SQLPrepare/SQLExecute and ICommandPrepare interfaces Very similar result can be achieved by using sp_executesql Simple parameterization SQL Server automatically replaces constant literal values with variables before the query plan will be compiled SQL Server 2008 allows us to forced parameterization of almost all types of queries Dynamic SQL Strings submitted via EXEC for execution

Are There Other Options ? Checking two other options: query preparation and forced parameterization YES, THERE ARE

Can You Avoid Recompilations? Stored procedure (or a batch) must be recompiled if not doing so would result in incorrect results or actions 14 recompilation reasons 2 sub-categories: Scheme changes SET options changes

Will Putting All DML Statements at the Beginning Help? SQL Server 2005 onwards does statement level recompilations Deferred compilations mean that objects didn't exist during creation the stored procedure The only solution is to use temporary tables SQL Server caches temporary objects but altering definitions of those temporary objects disables this caching mechanism In addition, objects cannot have named constraints Accessing objects created outside the current scope requires object id to name resolution Recompilation due to the changed scheme occurs

Will Putting All DML Statements at the Beginning Help? Avoiding recompilation due to schema changes NOT AT ALL

Recompilation Due to SET Option Changes When an execution plan is created, the SQL Server stores the environmental setting with it Some SET options used when the stored procedure was created or altered will be saved as part of its metadata Some execution plan attributes are considered cache key ones Subsequent execution will not trigger recompilations During the first execution a procedure is recompiled with the correct values, and from now on its execution plan is valid NEGLIGIBLE IMPACT

Recompilations Due to Plan Optimality Related Reasons SQL Server collects statistics about individual columns or sets of columns Statistics contain: The average key length A single-column histogram, including up to 200 values of a given column The estimated number of rows matching the filter, or all rows in the table Statistics can be not only automatically created but also kept up to date Statistics will be considered stale when: The first row is inserted into the empty table The number of rows in the table was less or equal to 500 and the colmodctr of the leading column of the statistics object has changed by more than 500 The table had more than 500 and the colmodctr of the leading column of the statistics object has changed by more than 500 + 20% of the number of rows If the statistics object is defined on a temporary table, there is an additional threshold for recomputation at 6 rows

Shoud You Control Recomplilations Due to Statistics Changes? Even if a statistic becomes outdated, it will not be automatically updated after the modification completes It will automatically update the next time a query plan uses it Default mechanism may lead to unnecessary recompilations Triggers are also being recompiled when the number of rows in the inserted or deleted virtual tables changes significantly from one execution to the next This is where hints come in KEEP PLAN, disables the “6 rows” rule KEEPFIXED PLAN disables recompilations due to statistics changes completely

Shoud You Control Recomplilations Due to Statistics Changes? Avoiding recompilation due to statistics changes YES, YOU SHOUD

When Plan Reusing is a Bad Thing Optimization and execution are two completely separate processes Sometime during an optimization SQL Server does not know the actual values used in queries Incorrect estimation in one part of the execution plan can (and probably will) be spread to others part of the plan Parameter Sniffing is an expected SQL Server behavior Parameter values used for the first execution of a stored procedure will be used to compile an execution plan and this plan will be used over and over again The problem arises when a stored procedure is executed for the first time with unusual parameter values

Is Parameter Sniffing a Bug or a Feature? To deal with it you can: Disable plan caching for the problematic stored procedure by adding WITH RECOMPILE to the procedure header Force a new compilation for a specific execution only by adding WITH RECOMPILE to the EXEC statement Force a compilation with typical parameter values By using variables By using OPTIMIZE FOR UNKNOWN Recompiling a new plan for a given parameter value might be way cheaper than reusing a plan optimized for different values Especially for DWs

Is Parameter Sniffing a Bug or a Feature? Parameter Sniffing Revealed Dealing with Parameter Sniffing IT’S A FEATURE

Does a Conditional Logic Hurt Performance? During the first execution SQL Server compiles and stores the execution plan for the whole stored procedure If there are multiple execution paths, all of them will be included, though only one of them will be actually used In all other branches the estimated numbers of rows will be one There are some exeptions The optimizer has to find good enough execution plans for a query that will never be executed with the given parameter values The only robust solution is to move the conditional logic to the wrapper stored procedure, from which we will call the inner ones

Does a Conditional Logic Hurt Performance? Following multiple execution paths REALLY BADLY

Can a Name Affect Performance? sp_ stands for Special Prefix not System Procedure Microsoft uses the sp_ naming convention for their system stored procedures and so this should be avoided Procedure prefixed with sp_ might conflict with one of system procedures and thus may never be called Always use at least two part names And be careful with DBO schema

Can a Name Affect Performance? DBO Schema and sp_ prefix nuisance STILL IT CAN

Sponsors