The rowset provider is not designed for data loss guarantee If data is not being consumed quickly enough and its internal buffers fill it waits up to 20 seconds before it begins dr
Trang 1ISQL Users Group May 2009
Tracing and Profiling
SQL Server
Yaniv.Etrogi@gmail.com
http://blogs.microsoft.co.il/blogs/yaniv_etrogi
Trang 2Over the next 1 hour:
SQL Trace Architecture and
Terminology
Security and Permissions
Profiler
Server side Tracing
Saving and Replaying Traces
Querying Server-Side Trace Metadata
Trang 3Introduction
troubleshooting are all possible by
the ability to view what's going on
inside SQL Server
view into what’s going on inside the database engine and at a very
granular level
Trang 4SQL Trace Architecture and Terminology
Trang 5 SQL Trace is an SQL Server database engine
technology
SQL Server Profiler is a NET application that
uses system stored procedures exposing the
functionality of SQL Trace
Microsoft.SqlServer.Management.Trace
http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.t race.aspx
Trang 6events which are generated when
various actions occur in the database engine
– For example, a user login or the execution of a query are each actions that cause events to fire
collection of columns that contain the collected data when the event fires
selectively enables data collection as
required
– Data is never collected until someone asks for it
columns to select from
Trang 7to the final destination
trace data are either a file on the
database server (or a network
share) or a rowset to a client
to ensure that if the data is not
consumed quickly enough (written
to disk or read from the rowset) it
will be queued
Trang 8guarantee that no event data will be lost
writes are not occurring quickly
enough the internal buffers begin to fill
the SQLTRACE_LOCK and
IO_COMPLETION wait types
Trang 9 The rowset provider is not designed for data loss guarantee
If data is not being consumed quickly
enough and its internal buffers fill it waits
up to 20 seconds before it begins dropping events in order to free buffers for the sake
of getting things moving
SQL Server Profiler will then send an
information message informing that some events had been lost and had not been
captured
You can find out if you're headed in that direction by monitoring SQL Server's
TRACEWRITE wait type which is
incremented as threads are waiting for
buffers to free up
Trang 10thread is started whenever at least one trace is active on the server
– SELECT * FROM sysprocesses
WHERE cmd = N'TRACE QUEUE TASK' ;
responsible for:
– Flushing file provider buffers (done every 4 seconds)
– Can be seen as a wait_type of
SQLTRACE_BUFFER_FLUSH
– Closing rowset-based traces that are considered to be expired (dropping events for more than 10 minutes)
trace data directly to a table
Trang 11Security and Permissions
Trang 12permission called ALTER TRACE This is
a server-level permission granted to a login principal and allows access to
start, stop, or modify a trace (in
addition to being able to generate defined events)
<LoginName>;
security features to keep passwords
secured
–SQL Trace will automatically omit data if an event
contains a call to a password-related stored procedure or statement For example, a call to CREATE LOGIN including the WITH PASSWORD option will be blanked out by SQL Trace
–SQL Trace will not return statement text or query plans
generated within an encrypted stored procedure,
user-defined function, or view
Trang 13Profiler
Trang 14 The General tab allows you to control how the trace will be processed by the consumer
The default setting is to use the rowset provider
displaying the events in real time in the SQL Server
Profiler tool window
Other available options are
When saving to a table Profiler uses the rowset
provider and routes the data back into a table
When saving to a server side file Profiler actually
starts two equivalent traces—one using the rowset
provider and the other using the file provider
Saving to a client-side file does not use the file
provider at all Rather, the data is routed to the Profiler tool via the rowset provider and then saved from there
to a file.
offered by the file provider
Trang 15 The Events Selection tab allows you to select events that you'd like to trace along with their associated data columns
In order to narrow your scope and help ensure that tracing does not cause performance issues SQL Trace offers the ability to filter the events based on various criteria
In SQL Server Profiler filtration is exposed via the
“Column Filters” tab
Remember to check the “Show All Columns”
checkbox in order to see the complete list of columns
Once you click the Run button data will begin
streaming immediately and be displayed at the Profiler window (this is because Profiler uses the rowset
provider)
it you may disable scrolling using the Auto Scroll icon on the SQL Server Profiler toolbar
filtered you may be in a situation that the icons in the Profiler tool bar are seen as if they were inactive thus disallowing you to stop the trace The only way out here is to stop the trace using sp_trace_setstatus
Trang 16Saving and Replaying
Traces
Trang 17 Profiler ships with eight predefined templates
settings that you can save to create a reusable trace definitions
TSQL_Replay template selects a variety of columns for 15 events that are all required for Profiler to be able to play back (Replay) a collected trace at a later time
experienced on a production system by replaying events collected on the
production system at a test environment
Collected trace data has to be saved and then reopened
before a replay can begin
SQL Server Profiler offers the following options for saving trace data available from the File menu:
proprietary binary format This is generally the fastest way to save the data, and also the smallest in terms of bytes on disk
table in a database This option is useful if you need to manipulate or report on the data using T-SQL
only those events and columns needed for replay functionality are saved
Trang 18 Once the data has been saved to a file or table the
original trace window can be closed and the file or
table reopened via SQL Server Profiler's File menu
A trace reopened in this way will have a Replay menu
on the Profiler tool bar allowing you to start replaying the trace, stop the replay, or set a breakpoint
During the course of the replay, the same events
used to produce the trace being replayed will be traced from the server on which you replay
client-side save No server-client-side option exists for saving playback results
The trace will be replayed on multiple threads (2005 only), corresponding to at most the “Number Of Replay Threads” specified
that all events will be played back in exactly the order in which they
occurred as based upon the EventSequence column Multiple threads will still be used to simulate multiple spids
Profiler to reorder the order in which each spid starts to execute events,
in order to enhance playback performance However, the order of events will remain consistent with the EventSequence within a given spid
Trang 19Server-Side Tracing
Trang 20add/remove event/column combinations
to traces based on the TraceId
sp_trace_setfilter is used to define
event filters based on trace columns
sp_trace_setstatus is called to start,
stop and and delete a trace
–Traces can be started and stopped multiple times over
their lifespan
Trang 22Querying Server-Side Trace
Metadata
Trang 23SELECT *
FROM sys.traces WHERE [status] = 1;
This query returns the trace status, which will
be 1 (started) or 0 (stopped); the server-side path to the trace file (or NULL if the trace is using the rowset provider); the maximum file size (or again, NULL in the case of the rowset provider); information about how many
buffers of what size are in use for processing the I/O; the number of events captured; and the number of dropped events (NULL if your trace is using the file provider(loseless
guarantee) ).
Trang 24SELECT
e.name AS [event], c.name AS [column]
FROM
fn_trace_geteventinfo(@TraceId) ei
INNER JOIN sys.trace_events e
ON ei.eventid = e.trace_event_id INNER JOIN sys.trace_columns c
ON ei.columnid = c.trace_column_id;
Trang 25SELECT
columnid ,c.name AS [column]
,logical_operator ,comparison_operator ,value
FROM fn_trace_getfilterinfo(TraceId ) ei
INNER JOIN sys.trace_columns c
ON ei.columnid = c.trace_column_id;
Trang 26ON c.trace_column_id =
b.trace_column_id
ORDER BY e.name;
Trang 27INNER JOIN sys.trace_subclass_values s
ON c.trace_column_id =
s.trace_column_id
INNER JOIN sys.trace_events e
ON e.trace_event_id = s.trace_event_id WHERE e.name LIKE 'Audit Login';`
Trang 28 Reading trace files
SELECT * FROM
fn_trace_gettable(‘B:\Traces\TraceErrors.trc', 1);
the number of trace files to read If the second parameter is DEFAULT then all available trace files will be read
SELECT * INTO ProfilerErrors
FROM
fn_trace_gettable(‘B:\Traces\TraceErrors.trc', DEFAULT);
from the fact that the SELCT INTO command is a BULK operation where as just performing queries against the trace loads sql server’s memory and if it
is a large set of data spilling begins thus incurring additional I/O
SELECT IDENTITY(int, 1, 1) AS RowNumber, * INTO dbo.ProfilerErrors
FROM
fn_trace_gettable(‘B:\Traces\TraceErrors.trc', DEFAULT);
Trang 29 Controlling trace state
EXEC sp_trace_setstatus @TraceId, 1; @TraceId, 1; start start
EXEC sp_trace_setstatus @TraceId, 0; @TraceId, 0; stop stop
EXEC sp_trace_setstatus @TraceId, 2; @TraceId, 2; –remove –remove
A trace in a stopped state can be started without the need to recreate the trace This allows to
modify the Events/Columns selection and the
selected Filters
A restart of the SQL Server service removes all trace definitions
–Zeros the EventSequence column
–If you need your trace always up and running you can launch it from a start up stored procedure
Trang 31?
?
Trang 32Inside Microsoft SQL Server 2005
http://www.insidesqlserver.com/thebooks.html
Trang 33Trace Errors
http://blogs.microsoft.co.il/blogs/yaniv_etrogi/TraceErrors.zip