Mar 26, 2015
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Session 306 ● Advanced SQL Profiler
Bill GrazianoClearData Consulting, Inc.
SQLTeam.com
2003 PASS Community Summit2003 PASS Community SummitNovember 11-14, 2003November 11-14, 2003
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
AgendaAgenda
Introduction / OverviewIntroduction / Overview Performance MonitoringPerformance Monitoring Security MonitoringSecurity Monitoring Scripting TracesScripting Traces ““Blackbox” TracesBlackbox” Traces Replaying TracesReplaying Traces
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
ProfilerProfiler
A Quick Demo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
SQL ServerSQL Server
Trace ArchitectureTrace Architecture
Producer Filter #1Duration: > 10 seconds
Filter #2App Like “MyApp%”
Queue
Filtered Data
Destination
Fileor
Recordset
EventsEvents
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Trace TemplatesTrace Templates
Predefined set ofPredefined set of EventsEvents Data columnsData columns FiltersFilters
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Handy TipsHandy Tips
Data Column GroupsData Column Groups Really just sortsReally just sorts
Event / Data Column MappingEvent / Data Column Mapping Not all events create all data columnsNot all events create all data columns
Store traces in a database tableStore traces in a database table Not the server you’re tracing!Not the server you’re tracing!
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
CPU Monitoring CPU Monitoring
EventsEvents TSQLTSQL
SQL:BatchCompletedSQL:BatchCompleted SQL:StmtCompletedSQL:StmtCompleted
StoredProceduresStoredProcedures RPC:CompletedRPC:Completed
A few other miscellaneous eventsA few other miscellaneous events AuditLogout, DTCTransaction, AuditLogout, DTCTransaction,
TransactionLogTransactionLog
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
CPU MonitoringCPU Monitoring
Data ColumnsData Columns CPUCPU
Milliseconds of CPU timeMilliseconds of CPU time Catches queries that have high CPU usageCatches queries that have high CPU usage
DurationDuration Milliseconds of durationMilliseconds of duration Catches long queries regardless of reasonCatches long queries regardless of reason
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Disk MonitoringDisk Monitoring
Reads – Logical Disk ReadsReads – Logical Disk Reads Writes – Logical Disk WritesWrites – Logical Disk Writes Logical <> PhysicalLogical <> Physical
Duration minus CPU => Disk TimeDuration minus CPU => Disk Time Except for locks and “other” things (waits)Except for locks and “other” things (waits)
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
MemoryMemory
Difficult to track using ProfilerDifficult to track using Profiler Server : Server Memory ChangeServer : Server Memory Change
Indicates memory change by 1 MB or 5% Indicates memory change by 1 MB or 5% (which ever is greater)(which ever is greater)
Stored Procedures : SP:CacheMissStored Procedures : SP:CacheMiss Stored Procedures : SP:RecompileStored Procedures : SP:Recompile
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Lock EventsLock Events
Acquired & ReleasedAcquired & Released Filter on object – still impracticalFilter on object – still impractical
DeadlockDeadlock Deadlock ChainDeadlock Chain
Indicates SPIDs involved in a deadlockIndicates SPIDs involved in a deadlock
TimeoutTimeout DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Other EventsOther Events
Cursors : CursorOpenCursors : CursorOpen Database : Data File Auto GrowDatabase : Data File Auto Grow Database : Log File Auto GrowDatabase : Log File Auto Grow Objects: Auto StatsObjects: Auto Stats Scans : Scan StartedScans : Scan Started
Can be misleading as many small tables Can be misleading as many small tables are scanned (Demo)are scanned (Demo)
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Profiling SecurityProfiling Security
Focus on logins and permissionsFocus on logins and permissions Can monitorCan monitor
Adding and removing logins and usersAdding and removing logins and users Granting and revoking permissionsGranting and revoking permissions Changing passwordsChanging passwords Changing role membershipChanging role membership Checking object permissionsChecking object permissions
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Profiling SecurityProfiling Security
Audit all “Security Audit” class events Audit all “Security Audit” class events EXCEPT:EXCEPT: Audit Object Derived PermissionAudit Object Derived Permission Audit Object PermissionAudit Object Permission Audit Statement PermissionAudit Statement Permission
These will fire each time permission is These will fire each time permission is checked on an object or statementchecked on an object or statement
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Scripting TracesScripting Traces
Effective Security TracingEffective Security Tracing AutomaticAutomatic ContinuousContinuous
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Creating TracesCreating Traces
First, we create the traceFirst, we create the trace sp_trace_create @TraceID output, sp_trace_create @TraceID output,
@options = 0, @options = 0, @tracefile = N‘c:\work\trace1', @tracefile = N‘c:\work\trace1', @maxfilesize = 5, @maxfilesize = 5, @stoptime = NULL@stoptime = NULL
Returns a TraceID as intReturns a TraceID as int Used to control the traceUsed to control the trace
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Create Trace Options Create Trace Options
Produce Row Set (1)Produce Row Set (1) This is what Profiler usesThis is what Profiler uses
File Rollover (2)File Rollover (2) Rolls to new file when max file size is Rolls to new file when max file size is
reachedreached Each new file has a sequential number Each new file has a sequential number
appended to itappended to it Tracefile_1.trcTracefile_1.trc
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Create Trace Options Create Trace Options
Shutdown on Error (4)Shutdown on Error (4) Shuts down SQL Server if unable to write Shuts down SQL Server if unable to write
trace filetrace file Used for C2 certificationUsed for C2 certification
Black Box Traces (8)Black Box Traces (8) Always holds last 5MB worth of dataAlways holds last 5MB worth of data Incompatible with other optionsIncompatible with other options
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Adding EventsAdding Events
Adds a data column to the traceAdds a data column to the trace
sp_trace_setevent @TraceID, sp_trace_setevent @TraceID, @eventid = 10, @eventid = 10, @columnid = 1, @columnid = 1, @on = 1 @on = 1
One entry for each event / column One entry for each event / column combinationcombination
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Adding FiltersAdding Filters
Sets a filter on the traceSets a filter on the trace
sp_trace_setfilter @TraceID, sp_trace_setfilter @TraceID, @columnid = 10, @columnid = 10, @logical_operator = 0, @logical_operator = 0, @comparison_operator = 7, @comparison_operator = 7, @value = N'SQL Profiler’@value = N'SQL Profiler’
i.e. - ApplicationName not like “SQL i.e. - ApplicationName not like “SQL Profiler”Profiler”
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Starting TracesStarting Traces
Starts the traceStarts the trace
sp_trace_setstatus @TraceID, sp_trace_setstatus @TraceID, @status = 1@status = 1
Other StatusesOther Statuses 0 – Stops the trace0 – Stops the trace 2 – Deletes the trace definition2 – Deletes the trace definition
A trace must be stopped AND deleted when A trace must be stopped AND deleted when you’re done with ityou’re done with it
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Autorun a Stored Procedure Autorun a Stored Procedure
Can automatically start a stored Can automatically start a stored procedure at the time the service startsprocedure at the time the service starts
sp_procoption sp_procoption @ProcName = ‘usp_BaseSecurity', @ProcName = ‘usp_BaseSecurity', @OptionName = 'startup', @OptionName = 'startup', @OptionValue = true@OptionValue = true
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Processing Trace FilesProcessing Trace Files
Profiler can save an existing trace file Profiler can save an existing trace file to a table (Demo)to a table (Demo)
Or use Or use fn_trace_gettablefn_trace_gettable to query a to query a trace file trace file
Select * Select * from ::fn_trace_gettable( ‘Basefrom ::fn_trace_gettable( ‘BaseSecurity.trc’, default)Security.trc’, default)
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Processing Trace FilesProcessing Trace Files
Can easily INSERT into a tableCan easily INSERT into a table Second parameter indicates to read all Second parameter indicates to read all
trace files (trace.trc, trace_1.trc, etc.)trace files (trace.trc, trace_1.trc, etc.) Scheduled job to import then archive Scheduled job to import then archive
the filesthe files
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
““Blackbox” TracesBlackbox” Traces
Special trace type Special trace type
exec @rc = sp_trace_create @TraceID exec @rc = sp_trace_create @TraceID output, 8output, 8
Automatically captures the last 5-10MB Automatically captures the last 5-10MB of dataof data Reuses two rollover filesReuses two rollover files
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
““Blackbox” TracesBlackbox” Traces
File is stored in the default DATA File is stored in the default DATA directorydirectory
SQL Server determines the events and SQL Server determines the events and columns to trackcolumns to track
Data written in 128KB chunksData written in 128KB chunks
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Handy UDF’sHandy UDF’s
fn_trace_getinfofn_trace_getinfo Returns information about a trace or all Returns information about a trace or all
active tracesactive traces
fn_trace_getfilterinfofn_trace_getfilterinfo fn_trace_geteventinfofn_trace_geteventinfo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Alerts & ErrorsAlerts & Errors
Event classes to capture errorsEvent classes to capture errors Exception EventsException Events
T-SQL exceptionsT-SQL exceptions
ErrorLog EventsErrorLog Events EventLog EventsEventLog Events Attention EventsAttention Events
Broken client connectionsBroken client connections
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
User Configurable EventsUser Configurable Events
T-SQL can create events that appear in T-SQL can create events that appear in ProfilerProfilersp_trace_generateevent sp_trace_generateevent
@eventid = 82, @eventid = 82, @userinfo = N'My Information‘,@userinfo = N'My Information‘,@userdata = @SomeVarBinaryData@userdata = @SomeVarBinaryData
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Replaying TracesReplaying Traces
Profiler can replay tracesProfiler can replay traces Requires specific events to be capturedRequires specific events to be captured Best to use the provided templateBest to use the provided template
Can replay sequentially or multi-Can replay sequentially or multi-threadedthreaded
““Poor man’s load tool”Poor man’s load tool” Capture a training session and replay itCapture a training session and replay it
DemoDemo
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
PerformancePerformance
Performance Impact Difficult to Performance Impact Difficult to QuantifyQuantify Greatly depends on server loadGreatly depends on server load
Try to limit traces to least amount of Try to limit traces to least amount of information neededinformation needed Information vs. PerformanceInformation vs. Performance
Profiler is slower than SQL Server Profiler is slower than SQL Server tracing to a filetracing to a file
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
SummarySummary
Performance MonitoringPerformance Monitoring Security MonitoringSecurity Monitoring Scripting TracesScripting Traces ““Blackbox” TracesBlackbox” Traces Replaying TracesReplaying Traces
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Presentation DownloadPresentation Download
Available tomorrow atAvailable tomorrow at www.cleardata.bizwww.cleardata.biz www.sqlteam.comwww.sqlteam.com
ContainsContains Current slidesCurrent slides Demo filesDemo files
[email protected]@SQLTeam.com
Advanced SQL ProfilerAdvanced SQL Profiler 306 306November 12November 12thth, 2003 , 2003 3:00 PM 3:00 PMCopyright 2003 ClearData Consulting, Inc.Copyright 2003 ClearData Consulting, Inc.
Thank you!Thank you!Thank you for attending this session and the Thank you for attending this session and the
2003 PASS Community Summit in Seattle!2003 PASS Community Summit in Seattle!
Please help us improve the quality of ourPlease help us improve the quality of ourconference by completing your sessionconference by completing your session
evaluation form. evaluation form.
Completed evaluation forms may be givenCompleted evaluation forms may be givento the room monitor as you exit or to staffto the room monitor as you exit or to staff
at the registration desk.at the registration desk.