0% found this document useful (0 votes)
12 views

7 TraceCheck Generic

This document provides instructions for analyzing a SQL Server trace file to identify performance issues. It loads trace data into a temporary table, calculates metrics like average and maximum reads, CPU, and duration for each query, and identifies the most expensive queries based on total read cost. It then calculates the latency between subsequent query events to identify blocking or delays. Finally, it returns detailed trace data ordered by event sequence for further analysis.

Uploaded by

rahul verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

7 TraceCheck Generic

This document provides instructions for analyzing a SQL Server trace file to identify performance issues. It loads trace data into a temporary table, calculates metrics like average and maximum reads, CPU, and duration for each query, and identifies the most expensive queries based on total read cost. It then calculates the latency between subsequent query events to identify blocking or delays. Finally, it returns detailed trace data ordered by event sequence for further analysis.

Uploaded by

rahul verma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*********************************************************/

/*** RV ***/
/*** Performance Optimization & Troubleshooting ***/
/*********************************************************/

USE [master]
GO

SET STATISTICS IO OFF


SET NOCOUNT ON
GO

-- Load Trace Data into table


DECLARE @ProfilerTrace NVARCHAR(250)
SET @ProfilerTrace = '<Path\Filename>.trc' -- assign Profiler Trace file here
SELECT * INTO #tmp_Trace FROM ::fn_trace_gettable(@ProfilerTrace, default)
WHERE [EventClass] IN (12, 45)
GO

ALTER TABLE dbo.#tmp_Trace ADD [id] BIGINT IDENTITY NOT NULL;


GO
ALTER TABLE #tmp_Trace ADD CONSTRAINT [pk_tmp_Trace] PRIMARY KEY NONCLUSTERED
([id])
GO

-----------------------------------------------------------------------------------
-------

/* ANALYSIS */

SELECT
isnull(DatabaseName, '') as [DbName],
isnull(convert(varchar(max), [TextData]), '') as [Query],
avg(Reads) as [Avg_Reads],
max(Reads) as [Max_Reads],
avg(CPU) as [Avg_CPU],
max(CPU) as [Max_CPU],
avg(Duration/1000) as [Avg_Duration],
max(Duration/1000) as [Max_Duration],
count(*) as [Occurrence],
convert(bigint, avg(Reads) * count(*)) as [Total_Cost_Reads],
convert(bigint,avg(CPU) * count(*)) as [Total_Cost_CPU],
convert(bigint,avg(Duration/1000) * count(*)) as [Total_Cost_Duration]
FROM #tmp_Trace WITH (READUNCOMMITTED)
WHERE Reads is not null and Writes is not null and CPU is not null
-- and DatabaseName = 'DbName'
GROUP BY DatabaseName, convert(varchar(max), [TextData])
--HAVING count(RowNumber) >= 100
ORDER BY [Total_Cost_Reads] DESC
GO

----------

ALTER TABLE #tmp_Trace ADD [latency] bigint


GO

DECLARE @start_1 datetime, @end_1 datetime, @start_2 datetime, @end_2 datetime


DECLARE temp_cur CURSOR FOR SELECT [StartTime], [EndTime] FROM #tmp_Trace ORDER BY
[EventSequence] FOR UPDATE OF [latency]
OPEN temp_cur
FETCH NEXT FROM temp_cur INTO @start_1, @end_1
FETCH NEXT FROM temp_cur INTO @start_2, @end_2
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE #tmp_Trace SET [latency] = DATEDIFF(ms, @start_1, @end_2) WHERE
CURRENT OF temp_cur

SET @start_1 = @start_2


SET @end_1 = @end_2

FETCH NEXT FROM temp_cur INTO @start_2, @end_2


END;
CLOSE temp_cur
DEALLOCATE temp_cur
GO

----------

SELECT [SPID], [TextData], [Reads], [Writes], [CPU], [Duration]/1000 as [Duration],


[StartTime], [EndTime], [latency], [LoginName], [HostName], [ApplicationName],
[DatabaseName], [EventSequence], [EventClass]
FROM #tmp_Trace
ORDER BY [EventSequence];
GO

----------

DROP TABLE #tmp_Trace


GO

You might also like