|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
|
|
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);
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
|
|
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.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:26 PM
Points: 6,696,
Visits: 11,715
|
|
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
Believe you can and you're halfway there. --Theodore Roosevelt
Everything Should Be Made as Simple as Possible, But Not Simpler --Albert Einstein
The significant problems we face cannot be solved at the same level of thinking we were at when we created them. --Albert Einstein
1 apple is not exactly 1/8 of 8 apples. Because there are no absolutely identical apples. --Giordy
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 9:52 AM
Points: 187,
Visits: 332
|
|
Two very good idea's. Since I have very limited powershell skills, I think the second is what I will do.
Thanks
|
|
|
|