dat304 managing and executing stored procedures for performance william r. vaughn beta v corporation

46
DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn William R. Vaughn Beta V Corporation Beta V Corporation

Upload: arline-barnett

Post on 30-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

DAT304Managing And Executing Stored Procedures For Performance

DAT304Managing And Executing Stored Procedures For Performance

William R. VaughnWilliam R. VaughnBeta V CorporationBeta V Corporation

Page 2: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

2Copyright © 2004 Beta V Corporation

William R. VaughnWilliam R. VaughnPresident and Founder

Beta V Corporation

Author, Mentor, TrainerMicrosoft MVP

Page 3: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

3Copyright © 2004 Beta V Corporation

AgendaAgenda

Understanding Stored Procedure Understanding Stored Procedure ArchitectureArchitecture

How are Stored Procedures Optimized?How are Stored Procedures Optimized?

Managing the Query Plan for PerformanceManaging the Query Plan for Performance

Managing the Query Plan for PerformanceManaging the Query Plan for Performance

How does ADO.NET Call Stored procedures?How does ADO.NET Call Stored procedures?

Page 4: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

4Copyright © 2004 Beta V Corporation

Understanding Stored Procedure ArchitectureUnderstanding Stored Procedure Architecture

Stored procedures are SQL Server programsStored procedures are SQL Server programsToday, written in TSQLToday, written in TSQLIn Yukon, written in TSQL, Visual Basic .NET, C#In Yukon, written in TSQL, Visual Basic .NET, C#

They manage queries, updates, They manage queries, updates, maintenance…maintenance…They’re stored in and deployed with the They’re stored in and deployed with the databasedatabaseThey’re protected—just like any other objectThey’re protected—just like any other objectThey can gate access to DB objectsThey can gate access to DB objects

Base tables, viewsBase tables, viewsDDLDDL

Page 5: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

5Copyright © 2004 Beta V Corporation

Understanding Stored Procedure ArchitectureUnderstanding Stored Procedure Architecture

Eliminate needless query plan Eliminate needless query plan constructionconstruction

Cached query plans Cached query plans cancan improve improve performance performance

Move logic to the serverMove logic to the server

Permit more structured 3-tier designsPermit more structured 3-tier designs

Protect data, referential integrityProtect data, referential integrity

Increase developer productivityIncrease developer productivity

Page 6: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

6Copyright © 2004 Beta V Corporation

Compiled Compiled Query Plan Query Plan

QueryQuery

ChangesChanges

Understanding Stored Procedure ArchitectureUnderstanding Stored Procedure Architecture

SQL Server

Security

UnauthorizedUnauthorized

Stored Procedure

SELECT logic SELECT logic

Business Rules Business Rules

Constraints Constraints

Page 7: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

7Copyright © 2004 Beta V Corporation

AgendaAgenda

Understanding Stored Procedure Understanding Stored Procedure Architecture.Architecture.

How are Stored Procedures How are Stored Procedures Optimized?Optimized?

Managing the Query Plan for Managing the Query Plan for Performance.Performance.

How does ADO.NET Call Stored How does ADO.NET Call Stored procedures?procedures?

Page 8: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

8Copyright © 2004 Beta V Corporation

How Are Stored Procedures Optimized?How Are Stored Procedures Optimized?

Stored procedures Stored procedures compiled on first usecompiled on first use

Query plan cached in RAMQuery plan cached in RAM

Subsequent references use Subsequent references use cached plancached plan

Recompiled if….Recompiled if….

RA

M C

ach

e

Data Pages

Page 9: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

9Copyright © 2004 Beta V Corporation

What Forces Recompile?What Forces Recompile?

WITH RECOMPILE in CREATE WITH RECOMPILE in CREATE PROCEDURE or EXECUTE statementPROCEDURE or EXECUTE statement

Running sp_recompile for a table Running sp_recompile for a table referenced by the procedurereferenced by the procedure

Schema changes to referenced Schema changes to referenced objects, including adding or dropping objects, including adding or dropping constraints, defaults, or rulesconstraints, defaults, or rules

Restoring the database or any of the Restoring the database or any of the objects the procedure referencesobjects the procedure references

Page 10: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

10Copyright © 2004 Beta V Corporation

What Forces Recompile?What Forces Recompile?

Server activity ages plan out of cacheServer activity ages plan out of cache

Changes in table referenced by the Changes in table referenced by the stored procedurestored procedure

Procedure interleaves DDL and DML.Procedure interleaves DDL and DML.

The procedure performs certain The procedure performs certain operations on temporary tablesoperations on temporary tables

See Microsoft Knowledge Base Article - 243586 See Microsoft Knowledge Base Article - 243586

Page 11: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

11Copyright © 2004 Beta V Corporation

Profiler Trap SP:RecompileProfiler Trap SP:Recompile

CodeCode ReasonReason

11 Schema, bindings, or permissions changed Schema, bindings, or permissions changed between compile or execute between compile or execute

22 Statistics changedStatistics changed

33 Object not found at compile time, deferred Object not found at compile time, deferred check to run timecheck to run time

44 Set option changed in batchSet option changed in batch

55 Temp table schema, binding, or permission Temp table schema, binding, or permission changedchanged

66 Remote rowset schema, binding, or permission Remote rowset schema, binding, or permission changed changed

Page 12: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

12Copyright © 2004 Beta V Corporation

How Are Stored Procedures Optimized?How Are Stored Procedures Optimized?

Ad hoc queries Ad hoc queries compiled on first usecompiled on first use

Query plan cached in RAMQuery plan cached in RAM

Subsequent referencesSubsequent referencesQuery Optimizer compares Query Optimizer compares existing plans with new plan existing plans with new plan

Use cached plan Use cached plan if if it’s it’s recognizedrecognized

RA

M C

ach

e

Data Pages

Page 13: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

13Copyright © 2004 Beta V Corporation

CREATE PROCEDURE…

How Are Stored Procedures Optimized?How Are Stored Procedures Optimized?

Parse TSQL Syntax

Resolve References

Save in Database

EXEC @RC=MyProc…

Resolve references

Optimize

Compile

RAM Cache

Execute

Page 14: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

14Copyright © 2004 Beta V Corporation

RAM Cache

How Are Stored Procedures Optimized?How Are Stored Procedures Optimized?

SQL Server

1st. instance compiled and QP loaded2nd. instance shares loaded QP

3rd. instance shares first

1st. instance finishes

Data Pages

EXEC @RC=MyProc…

Page 15: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

15Copyright © 2004 Beta V Corporation

AgendaAgenda

Understanding Stored Procedure Understanding Stored Procedure Architecture.Architecture.

How are Stored Procedures How are Stored Procedures Optimized?Optimized?

Managing the Query Plan for Managing the Query Plan for Performance.Performance.

How does ADO.NET Call Stored How does ADO.NET Call Stored procedures?procedures?

Page 16: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

16Copyright © 2004 Beta V Corporation

Query OptimizationQuery Optimization

All input parametersAll input parametersUsed or notUsed or not

Suitable indexes (if any)Suitable indexes (if any)

Server statistics Server statistics (data distribution)(data distribution)

AllAll logic in the procedure logic in the procedureWhether or not the code is Whether or not the code is executedexecuted

Complexity of the queryComplexity of the query

IF

IF

Page 17: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

17Copyright © 2004 Beta V Corporation

Managing The Query Plan For PerformanceManaging The Query Plan For Performance

Generated query plan based on parametersGenerated query plan based on parametersProvided by Provided by firstfirst (arbitrary) query (arbitrary) query

Cached and reused for all subsequent useCached and reused for all subsequent useRegardless of suitabilityRegardless of suitability

Some queries Some queries run normally, run normally, others do notothers do not

Page 18: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

18Copyright © 2004 Beta V Corporation

Managing The Query Plan for PerformanceManaging The Query Plan for Performance

Flushing the CacheFlushing the CachePower-cycle the systemPower-cycle the system

Restart the serverRestart the server

DBCC FREEPROCCACHEDBCC FREEPROCCACHE

DBCC DROPCLEANBUFFERSDBCC DROPCLEANBUFFERSCheckpoint first…Checkpoint first…

Page 19: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

19Copyright © 2004 Beta V Corporation

Managing The Query Plan For PerformanceManaging The Query Plan For Performance

Least-frequently-used rules apply to cacheLeast-frequently-used rules apply to cacheWhen cache is full, least important objects When cache is full, least important objects overlaidoverlaid

Common RAM cache is used to store Common RAM cache is used to store Data pages Data pages andand procedures procedures

Monitor cache with PerfmonMonitor cache with Perfmon

Page 20: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

20Copyright © 2004 Beta V Corporation

Managing Query Plans For Performance Managing Query Plans For Performance

Recompilation might Recompilation might helphelp performance performance When “optimized” query is not optimalWhen “optimized” query is not optimal

Cost of recompile might be tiny Cost of recompile might be tiny when compared to poorly running querywhen compared to poorly running query

Test all parameter combinationsTest all parameter combinationsCheck for consistent plans, performanceCheck for consistent plans, performance

If query plan varies based on parametersIf query plan varies based on parametersRecompile for each execution?Recompile for each execution?

Best to redesign procedureBest to redesign procedure

Page 21: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

21Copyright © 2004 Beta V Corporation

Managing Query Plans For Performance Managing Query Plans For Performance

Recompiling on DemandRecompiling on DemandCREATE PROCEDURE … WITH RECOMPILECREATE PROCEDURE … WITH RECOMPILE

Compiles QP each time stored procedure is executedCompiles QP each time stored procedure is executed

EXECUTE … WITH RECOMPILEEXECUTE … WITH RECOMPILEWhen parameters are not “typical” When parameters are not “typical”

sp_recompilesp_recompileForces Forces allall plans to be recompiled (very cheap) plans to be recompiled (very cheap)

Point to stored procedure, table…Point to stored procedure, table…

Statement-based recompileStatement-based recompileDynamic string execution (dangerous, but powerful)Dynamic string execution (dangerous, but powerful)

Smaller, more-focused proceduresSmaller, more-focused procedures

Page 22: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

22Copyright © 2004 Beta V Corporation

Managing Query Plans for Performance Managing Query Plans for Performance

Use Query Analyzer to view query planUse Query Analyzer to view query planExecute query with a range of input Execute query with a range of input parametersparameters

Clear procedure and data cacheClear procedure and data cache

View IO StatisticsView IO Statistics

Enable “Show Execution Plan”Enable “Show Execution Plan”

Page 23: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

23Copyright © 2004 Beta V Corporation

Managing Query Plans For PerformanceManaging Query Plans For Performance

Page 24: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

24Copyright © 2004 Beta V Corporation

Managing Querys Plan For PerformanceManaging Querys Plan For Performance

Page 25: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

25Copyright © 2004 Beta V Corporation

Managing Query Plans For Performance Managing Query Plans For Performance

Flush cache DBCC FREEPROCCACHEFlush cache DBCC FREEPROCCACHE

Force recompile WITH RECOMPILEForce recompile WITH RECOMPILE

Avoid “all-purpose” stored proceduresAvoid “all-purpose” stored procedures

Page 26: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

26Copyright © 2004 Beta V Corporation

Managing Query Plans For Performance Managing Query Plans For Performance

Reengineer stored proceduresReengineer stored proceduresBreak up larger, more complex proceduresBreak up larger, more complex procedures

Each sub-procedure gets its Each sub-procedure gets its own query planown query plan

Design procedures to work Design procedures to work with typical parmswith typical parms

Build special case proceduresBuild special case procedures

Page 27: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

27Copyright © 2004 Beta V Corporation

AgendaAgenda

Understanding Stored Procedure Understanding Stored Procedure Architecture.Architecture.

How are Stored Procedures How are Stored Procedures Optimized?Optimized?

Managing the Query Plan for Managing the Query Plan for Performance.Performance.

How does ADO.NET Call Stored How does ADO.NET Call Stored procedures?procedures?

Page 28: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

28Copyright © 2004 Beta V Corporation

Executing SPs With ADO.NET?Executing SPs With ADO.NET?

Build custom Command objectBuild custom Command object

Describes ParametersDescribes ParametersInputInput

OUTPUTOUTPUT

RETURN ValueRETURN Value

Page 29: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

29Copyright © 2004 Beta V Corporation

Stored Procedure IOStored Procedure IO

SQL Server “Resultset(s)” created to SQL Server “Resultset(s)” created to contain:contain:

0 to 1 Rowset(s)0 to 1 Rowset(s)Each SELECT returns a rowset with 0 to n rowsEach SELECT returns a rowset with 0 to n rows

(Optionally) XML rowset(Optionally) XML rowset

(Optionally) rows affected (@@rowcount) (Optionally) rows affected (@@rowcount) (bigint)(bigint)

Disabled with SET NOCOUT ONDisabled with SET NOCOUT ON

(Optionally) 0 to N OUTPUT Parameters(Optionally) 0 to N OUTPUT Parameters

RETURN value (signed integer) or 0RETURN value (signed integer) or 0

Page 30: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

30Copyright © 2004 Beta V Corporation

Stored Procedure IOStored Procedure IO

Stored procedure can also returnStored procedure can also return0 to “n” PRINT or RAISERROR 0 to “n” PRINT or RAISERROR message(s)message(s)

Application Log messages Application Log messages RAISERROR … WITH LOGRAISERROR … WITH LOG

Assuming account is in the SysAdmins roleAssuming account is in the SysAdmins role

Xp_Logevent functionXp_Logevent functionWrites to logWrites to log

Page 31: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

31Copyright © 2004 Beta V Corporation

Stored Procedure IOStored Procedure IO

Data returned in a defined orderData returned in a defined orderRowset (if any)Rowset (if any)

PRINT or RAISERROR message(s) (if any)PRINT or RAISERROR message(s) (if any)

OUTPUT parameter(s) (if any)OUTPUT parameter(s) (if any)

RETURN value or 0RETURN value or 0

Cannot capture OUTPUT parm or Cannot capture OUTPUT parm or RETURNRETURN

Until rowset reaches EOF or canceledUntil rowset reaches EOF or canceled

Page 32: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

32Copyright © 2004 Beta V Corporation

Building a Command ObjectBuilding a Command Object

Let the Command(Don’tUse)Builder Let the Command(Don’tUse)Builder do it at runtime.do it at runtime.

Page 33: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

33Copyright © 2004 Beta V Corporation

Building a Command ObjectBuilding a Command Object

Let Visual Studio .NET Let Visual Studio .NET do it for you at design time.do it for you at design time.

Drag a stored procedure Drag a stored procedure from server explorer to a formfrom server explorer to a form

Use the DataAdapter Configuration Use the DataAdapter Configuration WizardWizard

Design-time creation meansDesign-time creation meansBetter runtime performanceBetter runtime performance

Tunable codeTunable code

Page 34: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

34Copyright © 2004 Beta V Corporation

Building a Command ObjectBuilding a Command ObjectSet CommandType.StoredProcedureSet CommandType.StoredProcedure

Set CommandText to Stored procedure nameSet CommandText to Stored procedure name““Owner-qualify” the name (dbo.MySP) Owner-qualify” the name (dbo.MySP)

Hand-code the Parameters collectionHand-code the Parameters collection

Set Parameter name to stored procedure nameSet Parameter name to stored procedure name

Set Direction for Set Direction for Any OUTPUT, RETURN value parametersAny OUTPUT, RETURN value parameters

Set Size for variable-length DatatypesSet Size for variable-length DatatypesVarChar, NVarChar, Char, Nchar, VarBinary…VarChar, NVarChar, Char, Nchar, VarBinary…

Set Precision, ScaleSet Precision, Scale

Page 35: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

35Copyright © 2004 Beta V Corporation

Populating Input ParameterPopulating Input Parameter

Procedures defined with Default valuesProcedures defined with Default valuesDefine just the input ParametersDefine just the input Parametersyou want to submityou want to submit

Remaining Parameters take default valueRemaining Parameters take default value

Procedures without Default valuesProcedures without Default valuesDefine Define allall Parameters Parameters

Set Value for Set Value for allall Input Parameters Input Parameters

Page 36: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

36Copyright © 2004 Beta V Corporation

Executing Stored ProceduresExecuting Stored Procedures

DataAdapter FillDataAdapter FillHandles connection automaticallyHandles connection automatically

Builds and populates DataSets with all Builds and populates DataSets with all rowsetsrowsets

Handles multiple resultsets automaticallyHandles multiple resultsets automatically

Page 37: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

37Copyright © 2004 Beta V Corporation

Executing Stored ProceduresExecuting Stored ProceduresExecuteReaderExecuteReader

Returns SqlDataReaderReturns SqlDataReader

Manual connection, rowset population, Manual connection, rowset population, resultset managementresultset management

Page 38: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

38Copyright © 2004 Beta V Corporation

When Using The DataReaderWhen Using The DataReader

Be Be suresure to close the Connection to close the Connection

Once it falls from scope it’s orphanedOnce it falls from scope it’s orphanedThis results in Connection Pool overflowThis results in Connection Pool overflow

Use Use CommandBehavior.CloseConnectionCommandBehavior.CloseConnection

Closes Connection Closes Connection ifif DataReader is DataReader is closedclosed

Avoid passing DataReader between Avoid passing DataReader between scopesscopes

Page 39: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

39Copyright © 2004 Beta V Corporation

Executing Stored ProceduresExecuting Stored ProceduresExecuteScalarExecuteScalar

Returns an object – first column Returns an object – first column of first rowsetof first rowset

Manual connection, rowset populationManual connection, rowset population

Page 40: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

40Copyright © 2004 Beta V Corporation

Executing Stored ProceduresExecuting Stored Procedures

ExecuteQueryNonQueryExecuteQueryNonQueryManual connection handling. No rowset.Manual connection handling. No rowset.

Page 41: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

41Copyright © 2004 Beta V Corporation

Processing OUTPUT ParametersProcessing OUTPUT Parameters

Fetch OUTPUT and Fetch OUTPUT and RETURN values RETURN values

Only after rowset Only after rowset populationpopulation

Immediately after FillImmediately after Fill

After After DataReader.Read DataReader.Read returns False on last returns False on last resultsetresultset

Page 42: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

42Copyright © 2004 Beta V Corporation

Capturing OUTPUT ParmsCapturing OUTPUT Parms

Page 43: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

43Copyright © 2004 Beta V Corporation

Performance From 50,000’Performance From 50,000’

Construct Command object

Execute query

Parse, resolve, build QP

Execute query

Return resultsets

Client-side processing

Don’t sweat the small stuff…Don’t sweat the small stuff…

Client-side preparation

Client-side consumption

Server-side execution

Page 44: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

44Copyright © 2004 Beta V Corporation

SummarySummary

Understand how SQL Server worksUnderstand how SQL Server works

This helps you help SQL Server This helps you help SQL Server execute “optimal” plansexecute “optimal” plans

Keep Stored procedures simpleKeep Stored procedures simple

Leverage output parameter Leverage output parameter performanceperformance

Page 45: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

45Copyright © 2004 Beta V Corporation

For more informationFor more informationVisit Visit www.betav.comwww.betav.com

Read ADO.NET Examples and Best Read ADO.NET Examples and Best PracticesPractices

SQL Server Magazine, MSDN articlesSQL Server Magazine, MSDN articles

Page 46: DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation

46Copyright © 2004 Beta V Corporation

Mentoring, training, and technical content for professionals world wide.

Mentoring, training, and technical content for professionals world wide.

www.betav.com1+ (425) 556-9205

This presentation is for informational purposes only. BETAV CORPORATION MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.This presentation is for informational purposes only. BETAV CORPORATION MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.