![]() |
|
|
|
By: David Bird | Read Comments (4) | Related Tips: More > Profiler and Trace |
You want to schedule a trace, but SQL Profiler does not have a built-in scheduling option. So how do you use SQL Agent to schedule a SQL Profiler trace?
The most efficient means to define the SQL commands used for constructing a profiler trace is to use SQL Profiler.
Next, take the exported trace definition and use it to create a stored procedure.
/* use Admin go */ CREATE procedure trc_Template @Folder nvarchar(200) as /* Start a 60 minute profiler trace storing the captured output in provider folder. The folder must exist. A subfolder will be created using the start date and time to allow for repeated running of this profile without replacing the previuos captured trace files. On SQL Server 2005, XP_CMDSHELL needs to be enable to create the subfolder. You might want to disable it when you are done running your scheduled trace. Sample Command: exec trc_Template @Folder = 'C:\Output\ProfilerTrace\Template' */ set nocount on -- To change the traces duration, modify the following statement declare @StopTime datetime ; set @StopTime = dateadd(mi,60,getdate()) declare @StartDatetime varchar(13) ; set @StartDatetime = convert(char(8),getdate(),112) + '_' + cast(replace(convert(varchar(5),getdate(),108),':','') as char(4)) --['YYYYMMDD_HHMM'] declare @rc int declare @TraceID int declare @TraceFile nvarchar(100) declare @MaxFileSize bigint ; set @MaxFileSize = 50 -- The maximum trace file in megabytes declare @cmd nvarchar(2000) declare @msg nvarchar(200) If right(@Folder,1)<>'\' set @Folder = @Folder + '\' -- Check if Folder exists set @cmd = 'dir ' +@Folder exec @rc = master..xp_cmdshell @cmd,no_output if (@rc != 0) begin set @msg = 'The specified folder ' + @Folder + ' does not exist, Please specify an existing drive:\folder '+ cast(@rc as varchar(10)) raiserror(@msg,10,1) return(-1) end --Create new trace file folder set @cmd = 'mkdir ' +@Folder+@StartDatetime exec @rc = master..xp_cmdshell @cmd,no_output if (@rc != 0) begin set @msg = 'Error creating trace folder : ' + cast(@rc as varchar(10)) set @msg = @msg + 'SQL Server 2005 or later instance require OLE Automation to been enabled' raiserror(@msg,10,1) return(-1) end set @TraceFile = @Folder+@StartDatetime+'\trace' exec @rc = sp_trace_create @TraceID output, 2, @TraceFile, @MaxFileSize, @StopTime if (@rc != 0) begin set @msg = 'Error creating trace : ' + cast(@rc as varchar(10)) raiserror(@msg,10,1) return(-1) end --> Using your saved trace file, add the '-- Set the events' section below <-- --> Using your saved trace file, add the '-- Set the Filters' section below <-- --> Customization is now completed <-- ----------------------------------------------------------------------------- -- This filter is added to exclude all profiler traces. exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Profiler%' -- Set the trace status to start exec sp_trace_setstatus @TraceID, 1 -- start trace select 'Trace id = ', @TraceID, 'Path=', @Folder+@StartDatetime+'\' select 'To Stop this trace sooner, execute these two commands' select ' EXEC sp_trace_setstatus @traceid = ' , @TraceID , ', @status = 0; -- Stop/pause Trace' select ' EXEC sp_trace_setstatus @traceid = ' , @TraceID , ', @status = 2; -- Close trace and delete it from the server' return go


-- Enable xp_cmdshell EXEC sp_configure 'show advanced options', 1 -- To update the currently configured value for advanced options. RECONFIGURE -- To enable the feature. EXEC sp_configure 'xp_cmdshell', 1 -- To update the currently configured value for this feature. RECONFIGURE -- Start profiler trace EXEC Admin.dbo.trc_PerformanceTuning @Folder = 'e:\Output\ProfilerTrace\PerformanceTuning' -- Disable xp_cmdshell EXEC sp_configure 'xp_cmdshell', 0 -- To update the currently configured value for this feature. RECONFIGURE EXEC sp_configure 'show advanced options', 0 -- To update the currently configured value for advanced options. RECONFIGURE

Conclusion
This method of scheduling a SQL Profiler trace has been tested on SQL Server 2000, 2005, and 2008. At first creating these traces might seem a little complex, but after time it will get to become second nature. Having a ready-made trace you can run whenever an issue occurs will save time in reacting to and diagnosing a problem.
| Wednesday, July 08, 2009 - 11:44:02 AM - cferdig | Read The Tip |
|
Anyway to set the server you want to profile or do you have to run the proc on the profiled server? Like if I open profiler on machine B i can select machine A as the one I want to trace, but I cannot save this connection information. |
|
| Monday, January 24, 2011 - 11:04:34 PM - Fernando Jacinto | Read The Tip |
|
How can I save results to a table in a database and not to a trc file? and leave running as a Startup job |
|
| Thursday, March 24, 2011 - 6:15:04 PM - subhash Lahoti | Read The Tip | ||
|
The artical is great.
|
|||
| Tuesday, March 29, 2011 - 7:37:37 PM - David Bird | Read The Tip |
|
When generating the trace, select save in table. I have never tried it because trace files work better for me. I have read about setting up stored procedures that are executed at the startup. A stored procedure can be used to start a sql agent job which starts a SQL profile trace. I would not recommend running profile traces 24x7. Your question on working on blocking and cpu intensive SQLs is an article in itself. You might try searching for such articles or possible book on the subject. I often use the DMV's to find the most CPU intenstive queries and use profile traces to collect details about it.
|
|
|
privacy | disclaimer | copyright | advertise | about authors | contribute | feedback | giveaways | user groups Some names and products listed are the registered trademarks of their respective owners. Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com |