DAT308 SQL Server 2000 Profiler: The Database Programmer's Best Friend Fernando G. Guerrero S olid Q uality L earning
Hi Profiler! What is happening in my Server? What really happen in my database server? What all these GUI tools are doing to my server? This “black box” application is behaving badly, what is it sending to SQL Server? I know I have some deadlocks, but how can I fix them?
Quick info about Fernando (2 milliseconds) MCSD, MCSE+Internet (W2K), MCDBA, MCT, SQL Server MVP CEO and principal mentor at Solid Quality Learning Writing for SQL Sever Magazine and SQL Server Professional Co-author of Microsoft SQL Server 2000 Programming by Example (ISBN : ) Author of the.NET Operations Guide and the.NET Deployment Guide for Microsoft TechNet
S olid Q uality L earning (3 ms) An association of SQL Server experts from around the world Principal Mentors: Itzik Ben-Gan Kalen Delaney Fernando G. Guerrero Michael Hotek Brian Moran Ron Talmage Kimberly L. Tripp Helping you get the best out of your SQL Server: Training Consulting Mentoring Stay tuned to
Agenda SQL Profiler as Management Tool Looking under the hood of database tools Defining effective Traces Scripting SQL Server Traces
SQL Profiler as Management tool Performance/Network Monitor counts events Event log heap where events are logged Query Analyzer tool to analyze queries Tool to run administrative scripts SQL Trace Server side component of the auditing engine SQL Profiler GUI tool for SQL Trace
SQL Trace / SQL Profiler SQL Trace was an external tool in SQL Server 6.5 SQL Profiler was the audit tool in SQL Server 7.0 SQL Trace is the internal server side audit component in SQL Server 2000 SQL Profiler is the GUI tool for SQL Trace in SQL Server 2000 C2 auditing is a special case of comprehensive trace to meet C2 requirements
SQL Trace / SQL Profiler (2) You can use SQL Trace without SQL Profiler… as long as you know how to script it in Transact-SQL SQL Trace writes events into an external file SQL Profiler can write traces directly into a SQL Server table (SQL Trace can’t) SQL Trace provides an open audit framework where you can even define your own events
Key components of SQL Profiler/Trace General Trace Properties Events Data Columns Filters
General Trace Properties
Events
Data Columns
Filters
Performance implications SQL Trace runs in the server SQL Profiler shouldn’t run in the server The SQL Trace file shouldn’t be stored in the server The SQL table used by Profiler to store the trace shouldn’t be in the main server Under these conditions, running a trace shouldn’t be a big problem Communication bottlenecks might make Profiler losing some events!events
Profiler lowers CPU utilization in the server?
Profiler lowers CPU utilization in the server? (2) If that happens… you have PROBLEMS: The CPU utilization was too high originally You start a trace and SQL Server can’t cope with the extra workload SQL Server limits the throughput so it can run the trace So, SQL Server executes fewer transactions
Profiler lowers CPU utilization in the server? (3) It looks like you find a way to lower CPU utilization after all: Then, your CPU utilization looks better But your clients complain because they can’t get through STOP that b#$%#$ Trace!!! FIX what caused the CPU to be high in the first place
Looking under the hood of Database Tools Let’s look at some demos: Scripting SQL Server Enterprise Manager actions Optimistic concurrency in ADO 2.8 How the SqlDataAdapter sends updates to SQL Server by default DTC Transactions with COM+ components
Scripting SQL Server Enterprise Manager actions demo demo
What the SqlDataAdapter Update method does to SQL Server demo demo
DTC Transactions with COM+ components demo demo
Defining effective Traces Finding the worst-performing queries Detecting the use of cursors Detecting deadlocks Prioritizing objects’ optimization Identifying table and index scans
Finding the worst-performing queries You want to capture the duration of your queries: Capture at least the Duration and TextData columns for: SQL:BatchCompleted SQL:StmtCompleted RPC:Completed SP:Completed DCTTransaction SQLTransaction
Detecting the use of cursors Cursors are EVIL… in principle Capture the following events: CursorOpen CursorClose CursorExecute CursorImplicitConversion
Detecting deadlocks This could be the most complex and comprehensive trace you can create Errors and warnings Deadlocks SQL statements Transactions I prefer to show it with a demo
Detecting Deadlocks demo demo
Prioritizing objects’ optimization You want to know: How many times every Procedure/UDF is executed What are the procedures/UDFs using more execution time in SQL Server How long it takes to execute each Procedure/UDF in average You want to focus your optimization work on the most “popular” objects You want to get the highest performance boost from your tuning work
Prioritizing objects’ optimization demo demo
Identifying table and index scans You should monitor: Scan: Started Transaction ID Object ID Index ID Mode Scan: Stopped Object ID Index ID Duration Reads
Scripting SQL Server Traces You can create a trace by using the following procedures: Sp_trace_create Sp_trace_setevent Sp_trace_setfilter Sp_trace_setstatus Get information from a trace: Fn_trace_getinfo Fn_trace_gettable Fn_trace_geteventinfo Fn_tarce_getfilterinfo
Scripting a Trace from SQL Server Profiler demo demo
User-defined events = sp_trace_generateevent = ] event_id [, = ] 'user_info' ] [, = ] user_data ] Event_id should be between 82 and 91 User_info is nvarchar(128) (sysname) User_data is varbinary(8000) These events are traced as any other system events
Summary SQL Profiler as Management Tool Looking under the hood of database tools Defining effective Traces Scripting SQL Server Traces Do you love Profiler a bit more? ♥
Ask The Experts Get Your Questions Answered I’ll be at the Ask the Experts area: 2 July 2003: 12:00-14:00 3 July 2003: 11:00-13:00 4 July 2003: 12:00-14:00 You’ll see me around some more times
Community Resources Most Valuable Professional (MVP) Newsgroups Converse online with Microsoft Newsgroups, including Worldwide User Groups Meet and learn with your peers
Suggested Reading And Resources The tools you need to put technology to work! TITLEAvailable Microsoft® SQL Server 2000™ Performance Tuning Technical Reference: Today Microsoft Press books are 20% off at the TechEd Bookstore Also buy any TWO Microsoft Press books and get a FREE T-Shirt
Thank you! Questions? Download the source code of this session from: You can contact me at:
evaluations evaluations
© 2003 Microsoft Corporation. All rights reserved. This presentation is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.