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

Slides:



Advertisements
Similar presentations
Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
Advertisements

Module 13: Performance Tuning. Overview Performance tuning methodologies Instance level Database level Application level Overview of tools and techniques.
SQL Server performance tuning basics
ADO vs ADO.NET ADOADO.NET Client/server coupledDisconnected collection of data from data server Uses RECORDSET object (contains one table) Uses DATASET.
DEV351 ADO.NET Performance Pablo Castro Program Manager – ADO.NET Team Microsoft Corporation.
Brian Alderman | MCT, CEO / Founder of MicroTechPoint Pete Harris | Microsoft Senior Content Publisher.
Building Enterprise Applications Using Visual Studio ®.NET Enterprise Architect.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Performed by:Gidi Getter Svetlana Klinovsky Supervised by:Viktor Kulikov 08/03/2009.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Chapter 12 Database Connectivity with ASP.NET JavaScript, Third Edition.
SQL Server Stored Procedures Architecture & Performance Victor Isakov MCT, CTT, MSCE, MCDBA
Copyright  Oracle Corporation, All rights reserved. 4 Creating Functions.
Managing and Monitoring SQL Server 2005 Shankar Pal Program Manager SQL Server, Redmond.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Introduction to SQL 2005 Security Nick Ward SQL Server Specialist Nick Ward SQL Server Specialist
Copyright © 2005, 2006 Beta V Corporation All rights reserved Hitchhiker’s Guide to Visual Studio and SQL Server – Innovations in Reporting William R.
Copyright © 2014, 2015 William R. Vaughn All rights reserved William R. Vaughn.
Database Design for DNN Developers Sebastian Leupold.
Functions Lesson 10. Skills Matrix Function A function is a piece of code or routine that accepts parameters and stored as an object in SQL Server. The.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
ADO.NET A2 Teacher Up skilling LECTURE 3. What’s to come today? ADO.NET What is ADO.NET? ADO.NET Objects SqlConnection SqlCommand SqlDataReader DataSet.
By Lecturer / Aisha Dawood 1.  You can control the number of dispatcher processes in the instance. Unlike the number of shared servers, the number of.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
Stored Procedures, Transactions, and Error-Handling
Module 9 Designing and Implementing Stored Procedures.
Oracle Tuning Ashok Kapur Hawkeye Technology, Inc.
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.
Objectives In this lesson, you will learn to: *Identify the need for ADO.NET *Identify the features of ADO.NET *Identify the components of the ADO.NET.
1 Chapter Overview Performing Configuration Tasks Setting Up Additional Features Performing Maintenance Tasks.
ASP.NET OPTIMIZATION. Why Optimize? $$$ Whether you build applications for customers or not, enhanced applications save money.
Advanced ETL: Embedding Integration Services Ashvini Sharma Development Lead DAT411 Microsoft Corporation Sergei Ivanov Technical Lead DAT411 Microsoft.
Christopher M. Pascucci.NET Programming: Databases & ADO.NET.
Windows Forms in Visual Studio 2005 Mike Pelton Systems Engineer Microsoft Ltd
3-Tier Client/Server Internet Example. TIER 1 - User interface and navigation Labeled Tier 1 in the following graphic, this layer comprises the entire.
Presented by Joseph J. Sarna Jr. JJS Systems, LLC
Lecture Set 14 B new Introduction to Databases - Database Processing: The Connected Model (Using DataReaders)
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
DAT410 SQL Server 2005 Optimizing Procedural Code Kimberly L. Tripp President/Founder, SQLskills.com.
Slide 1 Chapter 7 – Part 3 Stored Procedure, Function &Trigger.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Active Data Objects Using.Net ADO.Net Farooq Ahmed Amna Umber Summayya Shehzad.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
Module 2: Using ADO.NET to Access Data. Overview ADO.NET Architecture Creating an Application That Uses ADO.NET to Access Data Changing Database Records.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
SQL Triggers, Functions & Stored Procedures Programming Operations.
 CONACT UC:  Magnific training   
ADO .NET from. ADO .NET from “ADO .Net” Evolution/History of ADO.NET MICROSOFT .NET “ADO .Net” Evolution/History of ADO.NET History: Most applications.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Improve query performance with the new SQL Server 2016 query store!! Michelle Gutzait Principal Consultant at
DAT 390 Advanced ADO.NET Programming Techniques Jackie Goldstein Renaissance Computer Systems
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
SQL Database Management
Building Enterprise Applications Using Visual Studio®
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Database Processing with ADO.NET
Dynamic SQL Writing Efficient Queries on the Fly
Stored Procedures – Facts and Myths
6/16/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Dynamic SQL: Writing Efficient Queries on the Fly
Dynamic SQL Writing Efficient Queries on the Fly
SQL Server 2016 Query Data Store
Dynamic SQL: Writing Efficient Queries on the Fly
Chapter 11 Managing Databases with SQL Server 2000
Presentation transcript:

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

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

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

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 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 Copyright © 2004 Beta V Corporation CREATE PROCEDURE… How Are Stored Procedures Optimized? Parse TSQL Syntax Resolve References Save in Database Resolve references Optimize Compile RAM Cache Execute

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

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 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 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 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 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 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 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 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 Copyright © 2004 Beta V Corporation Managing Query Plans For Performance

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

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 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 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 Copyright © 2004 Beta V Corporation Executing SPs With ADO.NET? Build custom Command object Describes Parameters InputOUTPUT RETURN Value

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 (bigint) Disabled with SET NOCOUT ON (Optionally) 0 to N OUTPUT Parameters RETURN value (signed integer) or 0

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 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 Copyright © 2004 Beta V Corporation Building a Command Object Let the Command(Don’tUse)Builder do it at runtime.

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 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 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 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 Copyright © 2004 Beta V Corporation Executing Stored Procedures ExecuteReader Returns SqlDataReader Manual connection, rowset population, resultset management

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 Copyright © 2004 Beta V Corporation Executing Stored Procedures ExecuteScalar Returns an object – first column of first rowset Manual connection, rowset population

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

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 Copyright © 2004 Beta V Corporation Capturing OUTPUT Parms

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 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 Copyright © 2004 Beta V Corporation For more information Visit Read ADO.NET Examples and Best Practices SQL Server Magazine, MSDN articles

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