Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software.

Slides:



Advertisements
Similar presentations
SQL Server performance tuning basics
Advertisements

Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Aaron Bertrand SQL Sentry, Senior Kevin Kline SQL Sentry, Dir of Engineering
Copyright © 200\8 Quest Software High Performance PL/SQL Guy Harrison Chief Architect, Database Solutions.
Understanding Parameter Sniffing Benjamin Nevarez Blog: benjaminnevarez.com 1.
Every SQL Programmer Should Know Kevin Kline Director of Engineering Services at SQL Sentry Microsoft MVP since 2003 Facebook, LinkedIn, Twitter at KEKLINE.
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
Virtual techdays INDIA │ 9-11 February 2011 SQL 2008 Query Tuning Praveen Srivatsa │ Principal SME – StudyDesk91 │ Director, AsthraSoft Consulting │ Microsoft.
SQL Server Stored Procedures Architecture & Performance Victor Isakov MCT, CTT, MSCE, MCDBA
Chapter 9 Overview  Reasons to monitor SQL Server  Performance Monitoring and Tuning  Tools for Monitoring SQL Server  Common Monitoring and Tuning.
Module 8: Monitoring SQL Server for Performance. Overview Why to Monitor SQL Server Performance Monitoring and Tuning Tools for Monitoring SQL Server.
Managing and Monitoring SQL Server 2005 Shankar Pal Program Manager SQL Server, Redmond.
DBA 322 Optimizing Stored Procedure Performance Kimberly L. Tripp Solid Quality Learning – SolidQualityLearning.com
Module 5: Data Access. Overview Introduce database components involved in data access Introduce concepts of Transact -SQL and Procedural SQL as tools.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation.
Store Procedures Lesson 9. Skills Matrix Stored Procedures Stored procedures in SQL Server are similar to the procedures you write in other programming.
Copyright © 2006 Quest Software Best Practices for Stored Procedures By Kevin Kline SQL Server MVP.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 9 Designing and Implementing Stored Procedures.
By: Matt Batalon, MCITP  Another form of temporary storage that can be queried or joined against, much like a table variable, temp.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Improving Database Performance Derrick Rapley
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Connect with life Nauzad Kapadia Quartz Systems
1 Chapter Overview Developing a Performance Monitoring Methodology Choosing Among Monitoring Tools Performing Monitoring Tasks.
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.
20 Copyright © 2008, Oracle. All rights reserved. Cache Management.
Meta Data Cardinality Explored CSSQLUG User Group - June 2009.
 Free Powerpoint Templates Free Powerpoint Yaduvanshi 1  Free Powerpoint Templates Free Powerpoint Templates Working with Stored.
DML Statements contd.. SQL Server CURSORS Cursor is used in handling results of select query for data calculations Cursors are used as buffered.
Virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
Kevin Kline Director of Engineering Services, SQL Sentry SQL Server MVP since 2003 Twitter, FB, LI: KEKline Blog:

Diving into Query Execution Plans ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
Oracle9i Developer: PL/SQL Programming Chapter 6 PL/SQL Packages.
Oracle Database Architectural Components
Session Name Pelin ATICI SQL Premier Field Engineer.
Understanding Core Database Concepts Lesson 1. Objectives.
Parameter Sniffing in SQL Server Stored Procedures
SQL Environment.
Query Optimization Techniques
Stored Procedures – Facts and Myths
Things You Can Find in the Plan Cache.
Dynamic SQL: Writing Efficient Queries on the Fly
Parameter Sniffing in SQL Server Stored Procedures
loooooooooooooong Introduction!
Microsoft Dumps Question Answer - Dumps4download
Microsoft ASP.NET Connections
Optimizing Microsoft SQL Server 2008 Applications Using Table Valued Parameters, XML, and MERGE
Stored Procedure Working with Free Powerpoint Templates
Peeking into the Plan Cache with SQL Server 2008
Top Tips for Better TSQL Stored Procedures
Dynamic SQL: Writing Efficient Queries on the Fly
TEMPDB – INTERNALS AND USAGE
The PROCESS of Queries John Deardurff
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
Statistics for beginners – In-Memory OLTP
Introduction to Execution Plans
SQL Server Query Design and Optimization Recommendations
Introduction to Execution Plans
Introduction to Execution Plans
Presentation transcript:

Atlanta SQL Server Users Group April 10, 2006 Stored Procedure Best Practices Kevin Kline Director of Technology Quest Software

Agenda Overview Polling Question Discuss Best Practices Review Common Problems Cover a few tuning recommendations

Started in IT in BS in MIS in 1989 from University of Alabama. Microsoft SQL Server MVP Author of 5 database books –1 on Oracle, 2 on SQL Server, 2 on SQL. Daily blogger on ATE on President of PASS ( –Conference is next Nov 13 – 17 in Seattle –Over 130 sessions on SQL Server, BI & Dev Worked for NASA, US Army, and Deloitte before Quest Software. Bio

Naming Procs Do NOT use sp_xxx as a naming convention. Causes additional searches and added I/O. –If a user stored procedure has same name as an sp_xxx stored procedure in MASTER, then the user procedure will NEVER be used.

Calling Procs Use stored procedure calls rather than embedded SQL EXEC versus SP_EXECUTESQL –same behavior with regard to batches, the scope of names, and database context –EXEC compiles entire SQL at one time –SP_EXECUTE compiles and executes as an execution plan separate from the execution plan of the batch that called sp_executesql itself. –SP_EXECUTESQL executes a Transact-SQL statement or batch that can be reused many times, or that has been built dynamically, using a single execution plan. –Often better than EXEC.

Common Problems Un-necessary network communication Execution Plans –Not Enough Sharing –Too Much Sharing –Inadequate plan Recompilation Other Delays

Set Nocount On No done_in_proc messages – –Extra network trip Server setting via Trace 3640 –-T3640 –sp_configure ‘user options’, 512

Create Stored Procedure Creation Parsing SQL Entries into sysobjects and syscomments tables

Exec Stored Procedure Execution Read from syscomments NO In Memory? compile optimize Execute YES

SP Plan Sharing Select * From dbo.titles Where type = ? Query Plan Execution Context Cooking Psychology Business Spid 10 Spid 17 Spid 23

Not Enough Plan Sharing Set Options Language used Dateformat

ONOFFUser Options Set opts Arithabort Concat_null_yields_null Quoted_Identifier** Ansi_nulls** Ansi_Padding Ansi_Warnings Numeric_roundabort Forceplan Ansi_Null_Dflt_On Ansi_Null_Dflt_Off

Option Hierarchy Set Statement in code OLEDB/ODBC Connection string ODBC –Control Panel –SQLConfigDatasource OLEDB/ODBC auto set of 7 –DB-Library/Embedded SQL for C-NOT Database Level Settings (alter database) Server Wide Settings (sp_configure)

+ + SIS

Too Much Plan Sharing getord tbl scan exec getord ‘05022%’ exec getord ‘%’ Memory finduserindex sp_1sp_4

Monitoring Plan Caching DBCC FreeProcCache DBCC FlushProcInDB( ) DBCC DropCleanBuffers Actual Query Plan SQL Profiler Syscacheobjects

Too Many Recompiles Execution Read from syscomments NO In Memory? compile optimize Execute YES ReComp

SP Recompiles Because we request it Previous plan aged out of memory Interleaved DDL and DML Schema changes to objects in sp New index statistics Cursor on temp table (SQL 7.0) Sp_configure

Requested Recompile Create proc … with recompile as Exec myproc … with recompile sp_recompile titles

SP Plan Aging getord Memory finduser sp_1sp_

Interleaved DDL and DML create proc testDDLDML as create table testdml (DDL) insert into testdml (DML – RECOMPILE) alter table testdml (DDL) insert into testdml (DML – RECOMPILE) drop table testdml

Schema Changes to Objects Sysobjects.schema_ver –Column additions, deletions –Data type changes –Constraint additions, deletions –Rule/Default bindings Query plan index dropped

New Index Statistics Auto_update statistics Auto_create statistics Update statistics

Inner SP Recompiles When it uses the outer SPs temp table –First time (if called multi times)

Using Local Variables in Where Clause Optimizer guesses percentage of rows returned Operator% =10 >30 < Between10

Best Practices Owner qualify all names (2-part) Standardize user options, language Minimize use of tempdb –Use table variable instead –Always refer to LOCAL temp tables Help the optimizer cache plans Avoid using local vars in where clause Don’t interleave DDL and DML TEST,TEST, TEST

Other Delay – Compile Locks KB Q spid blocked waittype waittime lastwaittype waitresource x000e 2141 LCK_M_X TAB: 6: [[COMPILE]] x000e 2235 LCK_M_X TAB: 6: [[COMPILE]] x000e 3937 LCK_M_X TAB: 6: [[COMPILE]] x000e 1094 LCK_M_X TAB: 6: [[COMPILE]] x000e 1968 LCK_M_X TAB: 6: [[COMPILE]] x LCK_M_X TAB: 6: [[COMPILE]]

dbo stuff dbo.test Kev stuff Kev.test select * from test dbo.sptest (Kev) Exec sptest

References SQL Server Books Online –“Execution Plan Caching and Reuse” “Analyzing Optimal Compiled Plan Caching” - Sajal Dam Knowledge Base –Q243588, “INF: Troubleshooting Performance of Ad Hoc Queries” –Q243586, “INF: Troubleshooting Stored Procedure Recompilation” –Q263889, “INF: SQL Blocking Due to [COMPILE] locks” Inside SQL Server 2000 – Kalen Delaney Guru’s Guide to … - Ken Henderson

Thank you! questions to Questions & Answers