Loading All Trace files

  • I was wondering if someone had already put the SQL together to load all trace files in a folder, into a SQL table. I have the following code.. that I modify for each trace file created during the previous day.

    insert into zt_trace_data

    SELECT cast(TextData as varchar(4000)) as TextData, BinaryData, DatabaseID, TransactionID, LineNumber, NTUserName, NTDomainName, HostName, ClientProcessID, ApplicationName,

    LoginName, SPID, Duration, StartTime, EndTime, Reads, Writes, CPU, Permissions, Severity, EventSubClass, ObjectID, Success, IndexID, IntegerData,

    ServerName, EventClass, ObjectType, NestLevel, State, Error, Mode, Handle, ObjectName, DatabaseName, FileName, OwnerName, RoleName,

    TargetUserName, DBUserName, LoginSid, TargetLoginName, TargetLoginSid, ColumnPermissions, LinkedServerName, ProviderName, MethodName,

    RowCounts, RequestID, XactSequence, EventSequence, BigintData1, BigintData2, GUID, IntegerData2, ObjectID2, Type, OwnerID, ParentName,

    IsSystem, Offset, SourceDatabaseID, SqlHandle, SessionLoginName, PlanHandle

    FROM fn_trace_gettable(N'C:\TraceFiles\SQL_Performance_20130201040000.trc',DEFAULT);

  • Borrowed from here and tweaked slightly:

    DECLARE @trace_id INT = 1,

    @path NVARCHAR(260);

    SELECT @path = REVERSE(SUBSTRING(REVERSE([path]), CHARINDEX('\', REVERSE([path])), 260)) + N'log.trc'

    FROM sys.traces

    -- set this id each time up above or come up with another identifier you can count on to get a specific trace id from sys.traces, e.g. path like '%something%'

    WHERE id = @trace_id;

    SELECT DatabaseName,

    [FileName],

    SPID,

    Duration,

    StartTime,

    EndTime,

    FileType = CASE EventClass

    WHEN 92 THEN 'Data'

    WHEN 93 THEN 'Log'

    END

    FROM sys.fn_trace_gettable(@path, DEFAULT)

    WHERE EventClass IN ( 92, 93 )

    ORDER BY StartTime DESC;

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Thanks, I just noticed that I left one key fact out.

    The trace runs for a set amount of time then stops, and SQL server removes it from sys.traces. So I have a job that re-creates the trace, at various times through out the day, when our performance is known to suffer. So the script you posted will not work, since once the hour is up.. sql server stops AND deletes the trace from sys.traces.

  • In that case if you want to do everything in T-SQL then you will need to gain directory access. Some options are xp_cmdshell or a CLR object, there are others. I would go a different route though. Create a simple PowerShell script to iterate over any directory you need and call Invoke-Sqlcmd once for each file encountered to load the trace file into a table. When the file is loaded you can do any number of things including deleting the trace file or moving it to an archive directory, your option are wide open at that point.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Another option is to have the job that starts the trace add an entry into a control table after the trace is started so you can later use that to bring the trace files in.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Two very good idea's. Since I have very limited powershell skills, I think the second is what I will do.

    Thanks

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply