UG - Procedure Best Practices v2
UG - Procedure Best Practices v2
UG - Procedure Best Practices v2
Procedures
By Kevin Kline
SQL Server MVP
Speaker Bio
• About Quest Software
• Accolades & Awards
• Stored Procedure Best
Practices
• Call to Action
• Q&A
2
Interactive Audience Poll
3
Speaker Bio – Kevin Kline
• Started in IT in 1986. 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 (www.sqlpass.org)
– 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.
4
About Quest Software
• Speaker Bio
• About Quest Software
• Accolades & Awards
Stored Procedure Best
Practices
• Call to Action
• Q&A
8
Naming Procs
9
Calling Procs
10
Common Problems
11
Set Nocount On
• No done_in_proc messages
– <10 rows affected>
– 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
Execute
14
SP Plan Sharing
15
Not Enough Plan Sharing
• Set Options
• Language used by the login
• Dateformat for the connection
16
ON OFF Setable
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
Forceplan SET only
Ansi_Null_Dflt_On S,D,A
Ansi_Null_Dflt_Off S,D,A
18
+
SI +
S
19
Too Much Plan Sharing
Memory
exec getord ‘%’ getord
tbl scan
finduser
index
exec getord ‘05022%’
sp_1 sp_4
20
Monitoring Plan Caching
• DBCC FreeProcCache
• DBCC FlushProcInDB(<dbid>)
• DBCC DropCleanBuffers
• Actual Query Plan
• SQL Profiler
• Syscacheobjects
21
Too Many Recompiles
Execute
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
24
Proc Plan Aging
Memory
getord
16 14
12
16
13
15
finduser
7 7
4
5
6
sp_1 sp_4
3 3
1
0
2 2 2
1
0
25
Interleaved DDL and DML
26
Schema Changes to Objects
• Sysobjects.schema_ver
– Column additions, deletions
– Data type changes
– Constraint additions, deletions
– Rule/Default bindings
27
New Index Statistics
• Auto_update statistics
• Auto_create statistics
• Update statistics
28
Inner proc Recompiles
29
Using Local Variables in Where Clause
• Optimizer guesses
percentage of rows Operator %
returned
= 10
> 30
< 30
Between 10
30
Transactions
• 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
33
dbo.sptest
dbo.test Kev.test
34
Resources
36
Q&A
THANK YOU!
37