Copyright © 2006 Quest Software Best Practices for Stored Procedures By Kevin Kline SQL Server MVP.

Slides:



Advertisements
Similar presentations
Chapter 9. Performance Management Enterprise wide endeavor Research and ascertain all performance problems – not just DBMS Five factors influence DB performance.
Advertisements

SQL Server performance tuning basics
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.
1 Data Concurrency David Konopnicki 1997 Revised by Mordo Shalom 2004.
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
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.
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.
2 Avoiding Stored Procedure Recompiles Dr Greg Low Managing Director Solid Q Australia Session Code: DAT454.
Python MySQL Database Access
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.
Store Procedures Lesson 9. Skills Matrix Stored Procedures Stored procedures in SQL Server are similar to the procedures you write in other programming.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 9 Designing and Implementing Stored Procedures.
Oracle Locking Michael Messina Principal Database Analyst Indiana University.
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.
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.
By Shanna Epstein IS 257 September 16, Cnet.com Provides information, tools, and advice to help customers decide what to buy and how to get the.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Connect with life Nauzad Kapadia Quartz Systems
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
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.
 Free Powerpoint Templates Free Powerpoint Yaduvanshi 1  Free Powerpoint Templates Free Powerpoint Templates Working with Stored.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Module 14: Managing Transactions and Locks. Overview Introducing Transactions and Locks Managing Transactions Understanding SQL Server Locking Architecture.
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.
Locks, Blocks & Isolation Oh My!. About Me Keith Tate Data Professional for over 14 Years MCITP in both DBA and Dev tracks
Session Name Pelin ATICI SQL Premier Field Engineer.
BE PREPARED Error Handling & Optimizing T-SQL Code in SQL Server 2000
Parameter Sniffing in SQL Server Stored Procedures
Tuning Transact-SQL Queries
Query Optimization Techniques
Stored Procedures – Facts and Myths
Query Tuning without Production Data
Parameter Sniffing in SQL Server Stored Procedures
Query Tuning without Production Data
Query Tuning without Production Data
loooooooooooooong Introduction!
Stored Procedure Working with Free Powerpoint Templates
Introduction to Execution Plans
Query Execution Expectation-Reality Denis Reznik
Now where does THAT estimate come from?
Top Tips for Better TSQL Stored Procedures
Steve Hood SimpleSQLServer.com
Transactions, Locking and Query Optimisation
TEMPDB – INTERNALS AND USAGE
The PROCESS of Queries John Deardurff
The PROCESS of Queries John Deardurff Website: ThatAwesomeTrainer.com
The PROCESS of Queries John Deardurff
Introduction to Execution Plans
Diving into Query Execution Plans
Introduction to Execution Plans
Introduction to Execution Plans
Presentation transcript:

Copyright © 2006 Quest Software Best Practices for Stored Procedures By Kevin Kline SQL Server MVP

1 Agenda  Speaker Bio About Quest Software Accolades & Awards Stored Procedure Best Practices Call to Action Q & A

2 Interactive Audience Poll What is your primary job duty? –DBA, Dev, BI, Architect, IT Pro, Mgr, Other? –Helps me tailor the content When will you use SQL Server 2005? –Already using it, 1-3 months, 4-6 months, More than 6 months but less than one year, Later How much SQL Server 2005 are you using? –No production systems on SQL2005, 1 or 2 production systems on SQL2005, as much as 50% on SQL2005, 100%! When will you use 64-bit platforms? –Already using it, 1-3 months, 4-6 months, More than 6 months but less than one year, Later

3 Started in IT in BS in MIS in 1989 from University of Alabama. Microsoft SQL Server MVP since 2004 Author of 7 database books –1 on Oracle, 2 on SQL Server, 2 on SQL, 1 on DB design, 1 on DB benchmarking President of PASS ( –Conference is next Sept in Denver, CO –Over 130 sessions on SQL Server, BI & Dev Blogs for SQLMag.com and SQLBlog.com Writes monthly columns for Database Trends & Applications and SQL Server Magazine Worked for NASA, US Army, and Deloitte & Touche before Quest Software. Speaker Bio – Kevin Kline

4 Profitable & Growing –Over a half-billion in ’06 revenues –Continued growth in ’01 through ‘06 against sector trends Founded in 1987 Public in 1999; Nasdaq: QSFT Customers: 18,000 Worldwide –Including 75 percent of the Fortune 500 over 2,750 employees –Over 950 in R&D –Nearly a dozen R&D centers around the globe Headquarters: Aliso Viejo, CA About Quest Software In Millions

5 Accolades & Awards Best of Tech Ed award 2006 with Spotlight on SQL Server SQL Server Magazine, Platinum reader’s choice award SQL Server Magazine Readers Choice Awards, winner in 11 categories, 2005 No. 1 vendor in Distributed Data Management Facilities, IDC, 2005 Microsoft ISV Partner of the Year, 2004 Microsoft TechEd Best of Show Winner, 2004 Microsoft TechEd Europe Best of Show Winner, 2005 No. 1 vendor in Application Management Software, Gartner Dataquest, 2005 Jolt Productivity Award Winner Network Computing Editor’s Choice Winner No. 8 in the “Who’s Who in Enterprise Software,” Investor’s Business Daily

6

7 Agenda Speaker Bio About Quest Software Accolades & Awards  Stored Procedure Best Practices Call to Action Q & A

8 Naming Procs Do NOT use sp_xxx as a naming convention. Causes additional searches and added I/O. –SQL Server will scan the procedure cache for Master, no matter what database the procedure was executed from –SQL Server will then acquire an exclusive COMPILE lock to perform a second search in the local database –If a user stored procedure has same name as an sp_xxx stored procedure in MASTER, then the user procedure will NEVER be used.

9 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.

10 Common Problems Un-necessary network communication Execution Plans –Not Enough Sharing –Too Much Sharing –Inadequate plan Recompilation Transactions Row Theory when Set Theory is better Other Delays

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

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

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

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

15 Not Enough Plan Sharing Set Options Language used by the login Dateformat for the connection

16 ONOFFSetable By: Arithabort  S,D,A Concat_null_yields_null  S,D,A Quoted_Identifier**  S,D,A Ansi_nulls**  S,D,A Ansi_Padding  S,D,A Ansi_Warnings  S,D,A Numeric_roundabort  S,D,A ForceplanSET only Ansi_Null_Dflt_OnS,D,A Ansi_Null_Dflt_OffS,D,A  = required for index views or computed columns  = required for distributed queries S, D, A = sp_configure, sp_dboption, alter database

17 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

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

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

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

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

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

24 Proc Plan Aging getord Memory finduser sp_1sp_

25 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

26 Schema Changes to Objects Sysobjects.schema_ver –Column additions, deletions –Data type changes –Constraint additions, deletions –Rule/Default bindings Query plan index dropped Related concept: always declare columns –Avoid using SELECT * FROM foo –Especially avoid INSERT without declared columns

27 New Index Statistics Auto_update statistics Auto_create statistics Update statistics

28 Inner proc Recompiles When it uses the outer procs temp table –First time (if called multi times)

29 Using Wildcards in WHERE clauses with Params Found in constructs like: SELECT * FROM foo WHERE emp_ID = ‘%’ Optimizer guesses percentage of rows returned Operator% =10 >30 < Between10

30 Transactions Avoid nested transactions. They aren’t truly nested: –COMMIT only saves data from the outermost transaction –ROLLBACK nukes ALL transactions, both innermost and outermost Orphaned Transactions –Errors don’t usually abort a transaction except for deadlocks –Returning from a procedure or batch does NOT commit a transaction. Once a transaction is started, it remains open until: The transaction is committed The transaction is rolled back The connection ends Use or sys.dm_tran_active_transactions to look for orphaned transactions when entering a new routine Keep transactions as short as possible! Keep transactions explicit! Remember lock escalation!

31 Row Theory when Set Theory is Better C#, VB, VB.NET, etc are procedural languages where you tell the program what to do SQL is a declarative language where you tell it what you want SQL becomes inefficient when you tell it what to do using CURSORs or LOOPs Learn to think in sets! –One pass through a table or tables –Let the computer process joins and filters

32 Best Practices Owner qualify all names (2-part) Standardize user options, language Minimize use of tempdb –Use table variable or smart structures instead Help the optimizer cache plans Avoid using wildcards with parameters in WHERE clause Don’t interleave DDL and DML TEST,TEST, TEST

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

34 Resources 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 (Don’t forget the drawing)

35 Call to Action – Next Steps Attend a live demo, especially Spotlight on SQL Server and Performance Analysis: Download white papers: Get a trial versions: us with your questions: or call DRAWING FOR GOODIES!

36 Q & A Send questions to me at: Send broader technical questions to: Send sales & licensing questions to: THANK YOU!