Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Performance Trace Helpers Expand / Collapse
Author
Message
Posted Friday, February 4, 2011 5:11 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 5:29 PM
Points: 1,787, Visits: 5,693
I have created these SPs as helpers for me when performance tuning/ measuring.

Sample usage:


EXEC helpers.dbo.StartPerfTrace;
GO
DECLARE @bb int;
SELECT top 100000 @bb=ROW_NUMBER() OVER(ORDER BY @@SPID)
FROM syscolumns,syscolumns a
GO 10
EXEC helpers.dbo.StopPerfTrace;



Sample output:


I find these very useful, but wondered if anyone had any good suggestions on other useful information I could include or just general critique of the techniques used.

If you can find time to take a look and maybe have a play I would appreciate it, thanks


USE [helpers]
GO
/****** Object: StoredProcedure [dbo].[StartPerfTrace] Script Date: 02/04/2011 11:59:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROC [dbo].[StartPerfTrace]
AS
DECLARE @TraceID int
DECLARE @RC int, @on BIT
DECLARE @Name nvarchar(245)
SELECT
@Name = 'D:\Dump\' + REPLACE (REPLACE ('PerfTrace' + convert (VARCHAR(10), @@spid) + ' ' + convert (VARCHAR(30), getdate (), 6) + ' ' + convert (VARCHAR(30), getdate (), 8), ' ', ''), ':', '')

EXEC @rc = sp_trace_create @TraceID output, 0, @Name

-- Save the trace ID for later - used in the StopPerfTrace Proc.
IF OBJECT_ID('helpers.dbo.traces') IS NULL
EXEC('USE helpers; CREATE TABLE traces (spid int not null,traceid int not null);');

INSERT
helpers.dbo.traces (spid,
traceid)
VALUES
(@@SPID,
@TraceID);

-- Set the events and data columns you need to capture.
SELECT
@on = 1;

-- 41 is SQL:StmtCompleted event. 1 is TextData column.
EXEC sp_trace_setevent @TraceID, 41, 1, @on; -- TextData
EXEC sp_trace_setevent @TraceID, 41, 13, @on; -- Duration
EXEC sp_trace_setevent @TraceID, 41, 16, @on; -- Reads
EXEC sp_trace_setevent @TraceID, 41, 17, @on; -- Writes
EXEC sp_trace_setevent @TraceID, 41, 18, @on; -- Cpu

EXEC sp_trace_setevent @TraceID, 162, 1, @on; -- User Errors - Can then use RAISERROR to put messages in the trace

DECLARE @pid INT = CONVERT(INT,@@SPID);

-- Set filter to include the current SPID
EXEC sp_trace_setfilter @TraceID, 12, 0, 0, @pid;
-- Set filter to exclude RAISERROR statements
-- I use these to add messages to the trace so don't want to see the command being called
EXEC sp_trace_setfilter @TraceID, 1, 0, 7, N'RAISERROR%';

-- Start Trace (status 1 = start)
EXEC @RC = sp_trace_setstatus @TraceID, 1;



USE [helpers]
GO
/****** Object: StoredProcedure [dbo].[StopPerfTrace] Script Date: 02/04/2011 12:07:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROC [dbo].[StopPerfTrace]
AS
DECLARE @TraceID int;

SELECT
@TraceID = traceid
FROM
helpers.dbo.traces
WHERE
spid = @@SPID;

DELETE helpers.dbo.traces
WHERE
spid = @@SPID;

DECLARE @file nvarchar(247)

-- Populate a variable with the trace_id of the current trace
SELECT
@file = [path]
FROM
sys.traces
WHERE
id = @TraceID

-- First stop the trace.
EXEC sp_trace_setstatus @TraceID, 0

-- Close and then delete its definition from SQL Server.
EXEC sp_trace_setstatus @TraceID, 2

-- Save to #tracestats table
SELECT
row_number () OVER (ORDER BY @@spid) AS RowNumber,
TextData,
convert (NUMERIC(14, 3), Duration / 1000) [Duration(millisec)],
Cpu,
Reads,
Writes,
avg (convert (NUMERIC(14, 3), Duration / 1000)) OVER (PARTITION BY convert (VARCHAR(MAX), TextData)) AS Duration_Avg,
avg (Cpu) OVER (PARTITION BY convert (VARCHAR(MAX), TextData)) AS Cpu_Avg,
avg (Reads) OVER (PARTITION BY convert (VARCHAR(MAX), TextData)) AS Reads_Avg,
avg (Writes) OVER (PARTITION BY convert (VARCHAR(MAX), TextData)) AS Writes_Avg
FROM
fn_trace_gettable (@file, DEFAULT)
WHERE
TextData IS NOT NULL;




MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw



  •   Post Attachments 
    Perftrace.PNG (55 views, 45.14 KB)
    Post #1058636
    Posted Friday, February 4, 2011 7:06 AM


    SSCoach

    SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

    Group: General Forum Members
    Last Login: Today @ 5:48 AM
    Points: 15,628, Visits: 28,010
    I see the trace is outputting to a table, is that right? If so, is it a table on the system that you're doing your testing on? That could be an issue because then the trace could be adding resource contention to whatever you're testing, invalidating the test. That's one of the reasons I like to output to file.

    ----------------------------------------------------
    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
    The Scary DBA
    Author of: SQL Server 2012 Query Performance Tuning
    SQL Server 2008 Query Performance Tuning Distilled
    and
    SQL Server Execution Plans

    Product Evangelist for Red Gate Software
    Post #1058714
    Posted Friday, February 4, 2011 7:24 AM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Yesterday @ 5:29 PM
    Points: 1,787, Visits: 5,693
    Hi Grant,

    Thanks for that comment, but the trace is actually writing to a file on a separate physical disk, the the StopPerfTrace SP reads it in using fn_trace_gettable.

    I am pretty sure that will be fine for my purposes, but am open to suggestions.

    I didn't think you could output the trace directly to a table - is that possible?



    MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1058737
    Posted Friday, February 4, 2011 7:29 AM


    SSCoach

    SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

    Group: General Forum Members
    Last Login: Today @ 5:48 AM
    Points: 15,628, Visits: 28,010
    mister.magoo (2/4/2011)
    Hi Grant,

    Thanks for that comment, but the trace is actually writing to a file on a separate physical disk, the the StopPerfTrace SP reads it in using fn_trace_gettable.

    I am pretty sure that will be fine for my purposes, but am open to suggestions.

    I didn't think you could output the trace directly to a table - is that possible?



    Yeah, you can. I don't recommend it, but it's possible.

    Other than, looks good. sorry I missed where it was storing the data.


    ----------------------------------------------------
    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
    The Scary DBA
    Author of: SQL Server 2012 Query Performance Tuning
    SQL Server 2008 Query Performance Tuning Distilled
    and
    SQL Server Execution Plans

    Product Evangelist for Red Gate Software
    Post #1058745
    Posted Friday, February 4, 2011 7:32 AM


    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Yesterday @ 5:29 PM
    Points: 1,787, Visits: 5,693
    Ok, thanks for your time.

    MM


  • MMGrid Addin
  • MMNose Addin


  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Post #1058748
    « Prev Topic | Next Topic »

    Add to briefcase

    Permissions Expand / Collapse