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

Loading All Trace files Expand / Collapse
Author
Message
Posted Tuesday, February 5, 2013 7:40 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 311, Visits: 538
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);
Post #1415841
Posted Tuesday, February 5, 2013 10:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:57 AM
Points: 7,081, Visits: 12,575
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
Post #1416001
Posted Tuesday, February 5, 2013 2:29 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 311, Visits: 538
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.

Post #1416108
Posted Tuesday, February 5, 2013 2:50 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:57 AM
Points: 7,081, Visits: 12,575
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
Post #1416114
Posted Tuesday, February 5, 2013 2:52 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 8:57 AM
Points: 7,081, Visits: 12,575
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
Post #1416115
Posted Wednesday, February 6, 2013 7:49 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Yesterday @ 7:27 AM
Points: 311, Visits: 538
Two very good idea's. Since I have very limited powershell skills, I think the second is what I will do.

Thanks
Post #1416512
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse