Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation."— Presentation transcript:

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

2 2 Copyright © 2004 Beta V Corporation William R. Vaughn President and Founder Beta V Corporation Author, Mentor, Trainer Microsoft MVP

3 3 Copyright © 2004 Beta V Corporation Agenda Understanding Stored Procedure Architecture How are Stored Procedures Optimized? Managing the Query Plan for Performance How does ADO.NET Call Stored procedures?

4 4 Copyright © 2004 Beta V Corporation Understanding Stored Procedure Architecture Stored procedures are SQL Server programs Today, written in TSQL In Yukon, written in TSQL, Visual Basic.NET, C# They manage queries, updates, maintenance… They’re stored in and deployed with the database They’re protected—just like any other object They can gate access to DB objects Base tables, views DDL

5 5 Copyright © 2004 Beta V Corporation Understanding Stored Procedure Architecture Eliminate needless query plan construction Cached query plans can improve performance Move logic to the server Permit more structured 3-tier designs Protect data, referential integrity Increase developer productivity

6 6 Copyright © 2004 Beta V Corporation Compiled Query Plan Query Changes Understanding Stored Procedure Architecture SQL Server Security Unauthorized Stored Procedure SELECT logic Business Rules Constraints

7 7 Copyright © 2004 Beta V Corporation Agenda Understanding Stored Procedure Architecture. How are Stored Procedures Optimized? Managing the Query Plan for Performance. How does ADO.NET Call Stored procedures?

8 8 Copyright © 2004 Beta V Corporation How Are Stored Procedures Optimized? Stored procedures compiled on first use Query plan cached in RAM Subsequent references use cached plan Recompiled if…. RAM Cache Data Pages

9 9 Copyright © 2004 Beta V Corporation What Forces Recompile? WITH RECOMPILE in CREATE PROCEDURE or EXECUTE statement Running sp_recompile for a table referenced by the procedure Schema changes to referenced objects, including adding or dropping constraints, defaults, or rules Restoring the database or any of the objects the procedure references

10 10 Copyright © 2004 Beta V Corporation What Forces Recompile? Server activity ages plan out of cache Changes in table referenced by the stored procedure Procedure interleaves DDL and DML. The procedure performs certain operations on temporary tables See Microsoft Knowledge Base Article - 243586

11 11 Copyright © 2004 Beta V Corporation Profiler Trap SP:Recompile CodeReason 1 Schema, bindings, or permissions changed between compile or execute 2 Statistics changed 3 Object not found at compile time, deferred check to run time 4 Set option changed in batch 5 Temp table schema, binding, or permission changed 6 Remote rowset schema, binding, or permission changed

12 12 Copyright © 2004 Beta V Corporation How Are Stored Procedures Optimized? Ad hoc queries compiled on first use Query plan cached in RAM Subsequent references Query Optimizer compares existing plans with new plan Use cached plan if it’s recognized RAM Cache Data Pages

13 13 Copyright © 2004 Beta V Corporation CREATE PROCEDURE… How Are Stored Procedures Optimized? Parse TSQL Syntax Resolve References Save in Database EXEC @RC=MyProc… Resolve references Optimize Compile RAM Cache Execute

14 14 Copyright © 2004 Beta V Corporation RAM Cache How Are Stored Procedures Optimized? SQL Server 1st. instance compiled and QP loaded 2nd. instance shares loaded QP 3rd. instance shares first 1st. instance finishes Data Pages EXEC @RC=MyProc…

15 15 Copyright © 2004 Beta V Corporation Agenda Understanding Stored Procedure Architecture. How are Stored Procedures Optimized? Managing the Query Plan for Performance. How does ADO.NET Call Stored procedures?

16 16 Copyright © 2004 Beta V Corporation Query Optimization All input parameters Used or not Suitable indexes (if any) Server statistics (data distribution) All logic in the procedure Whether or not the code is executed Complexity of the query IF

17 17 Copyright © 2004 Beta V Corporation Managing The Query Plan For Performance Generated query plan based on parameters Provided by first (arbitrary) query Cached and reused for all subsequent use Regardless of suitability Some queries run normally, others do not

18 18 Copyright © 2004 Beta V Corporation Managing The Query Plan for Performance Flushing the Cache Power-cycle the system Restart the server DBCC FREEPROCCACHE DBCC DROPCLEANBUFFERS Checkpoint first…

19 19 Copyright © 2004 Beta V Corporation Managing The Query Plan For Performance Least-frequently-used rules apply to cache When cache is full, least important objects overlaid Common RAM cache is used to store Data pages and procedures Monitor cache with Perfmon

20 20 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance Recompilation might help performance When “optimized” query is not optimal Cost of recompile might be tiny when compared to poorly running query Test all parameter combinations Check for consistent plans, performance If query plan varies based on parameters Recompile for each execution? Best to redesign procedure

21 21 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance Recompiling on Demand CREATE PROCEDURE … WITH RECOMPILE Compiles QP each time stored procedure is executed EXECUTE … WITH RECOMPILE When parameters are not “typical” sp_recompile Forces all plans to be recompiled (very cheap) Point to stored procedure, table… Statement-based recompile Dynamic string execution (dangerous, but powerful) Smaller, more-focused procedures

22 22 Copyright © 2004 Beta V Corporation Managing Query Plans for Performance Use Query Analyzer to view query plan Execute query with a range of input parameters Clear procedure and data cache View IO Statistics Enable “Show Execution Plan”

23 23 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance

24 24 Copyright © 2004 Beta V Corporation Managing Querys Plan For Performance

25 25 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance Flush cache DBCC FREEPROCCACHE Force recompile WITH RECOMPILE Avoid “all-purpose” stored procedures

26 26 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance Reengineer stored procedures Break up larger, more complex procedures Each sub-procedure gets its own query plan Design procedures to work with typical parms Build special case procedures

27 27 Copyright © 2004 Beta V Corporation Agenda Understanding Stored Procedure Architecture. How are Stored Procedures Optimized? Managing the Query Plan for Performance. How does ADO.NET Call Stored procedures?

28 28 Copyright © 2004 Beta V Corporation Executing SPs With ADO.NET? Build custom Command object Describes Parameters InputOUTPUT RETURN Value

29 29 Copyright © 2004 Beta V Corporation Stored Procedure IO SQL Server “Resultset(s)” created to contain: 0 to 1 Rowset(s) Each SELECT returns a rowset with 0 to n rows (Optionally) XML rowset (Optionally) rows affected (@@rowcount) (bigint) Disabled with SET NOCOUT ON (Optionally) 0 to N OUTPUT Parameters RETURN value (signed integer) or 0

30 30 Copyright © 2004 Beta V Corporation Stored Procedure IO Stored procedure can also return 0 to “n” PRINT or RAISERROR message(s) Application Log messages RAISERROR … WITH LOG Assuming account is in the SysAdmins role Xp_Logevent function Writes to log

31 31 Copyright © 2004 Beta V Corporation Stored Procedure IO Data returned in a defined order Rowset (if any) PRINT or RAISERROR message(s) (if any) OUTPUT parameter(s) (if any) RETURN value or 0 Cannot capture OUTPUT parm or RETURN Until rowset reaches EOF or canceled

32 32 Copyright © 2004 Beta V Corporation Building a Command Object Let the Command(Don’tUse)Builder do it at runtime.

33 33 Copyright © 2004 Beta V Corporation Building a Command Object Let Visual Studio.NET do it for you at design time. Drag a stored procedure from server explorer to a form Use the DataAdapter Configuration Wizard Design-time creation means Better runtime performance Tunable code

34 34 Copyright © 2004 Beta V Corporation Building a Command Object Set CommandType.StoredProcedure Set CommandText to Stored procedure name “Owner-qualify” the name (dbo.MySP) Hand-code the Parameters collection Set Parameter name to stored procedure name Set Direction for Any OUTPUT, RETURN value parameters Set Size for variable-length Datatypes VarChar, NVarChar, Char, Nchar, VarBinary… Set Precision, Scale

35 35 Copyright © 2004 Beta V Corporation Populating Input Parameter Procedures defined with Default values Define just the input Parameters you want to submit Remaining Parameters take default value Procedures without Default values Define all Parameters Set Value for all Input Parameters

36 36 Copyright © 2004 Beta V Corporation Executing Stored Procedures DataAdapter Fill Handles connection automatically Builds and populates DataSets with all rowsets Handles multiple resultsets automatically

37 37 Copyright © 2004 Beta V Corporation Executing Stored Procedures ExecuteReader Returns SqlDataReader Manual connection, rowset population, resultset management

38 38 Copyright © 2004 Beta V Corporation When Using The DataReader Be sure to close the Connection Once it falls from scope it’s orphaned This results in Connection Pool overflow Use CommandBehavior.CloseConnection Closes Connection if DataReader is closed Avoid passing DataReader between scopes

39 39 Copyright © 2004 Beta V Corporation Executing Stored Procedures ExecuteScalar Returns an object – first column of first rowset Manual connection, rowset population

40 40 Copyright © 2004 Beta V Corporation Executing Stored Procedures ExecuteQueryNonQuery Manual connection handling. No rowset.

41 41 Copyright © 2004 Beta V Corporation Processing OUTPUT Parameters Fetch OUTPUT and RETURN values Only after rowset population Immediately after Fill After DataReader.Read returns False on last resultset

42 42 Copyright © 2004 Beta V Corporation Capturing OUTPUT Parms

43 43 Copyright © 2004 Beta V Corporation 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… Client-side preparation Client-side consumption Server-side execution

44 44 Copyright © 2004 Beta V Corporation Summary Understand how SQL Server works This helps you help SQL Server execute “optimal” plans Keep Stored procedures simple Leverage output parameter performance

45 45 Copyright © 2004 Beta V Corporation For more information Visit www.betav.com www.betav.com Read ADO.NET Examples and Best Practices SQL Server Magazine, MSDN articles

46 46 Copyright © 2004 Beta V Corporation Mentoring, training, and technical content for professionals world wide. www.betav.com www.betav.com 1+ (425) 556-9205 This presentation is for informational purposes only. BETAV CORPORATION MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.


Download ppt "DAT304 Managing And Executing Stored Procedures For Performance William R. Vaughn Beta V Corporation."

Similar presentations


Ads by Google