SQL Server Gets the index creation time & time & rebuild reorganization time

Prior wrote a blog " ? SQL Server whether you can get an accurate reconstruction of the last time the index ", which focuses on three questions: Can we find time to create an index? The last time the index rebuild (Index Rebuild) time? The last time the index reorganization (INDEX REORGANIZE) time? Was concluded, the answer is we can not accurately find the time to create the index, the index reorganization last time, the last time the index rebuild. But recently I saw a blog " SQL Server - the Get a Date Index Creation " , and then study a little, even if temporarily do not have a SQL Server system tables or views DMV have time to save index creation, the index rebuild time and index reorganization time. But we can get through the file system to track their value, of course, there are limitations , not all of these can be found in the index value . See below for a detailed explanation:

 

 

 

Index creation time

 

Index creation time, you can use the following SQL get, but we know that it is possible to stop or disable tracking; trace file may be overwritten. So this method can only check the most recent period. It has a strong timeliness. So this method can not be universal. A method which is destined only as a reference, and can not be universal.

 

 

DECLARE @filename VARCHAR(500) 
SELECT @filename = CAST(value AS VARCHAR(500)) 
FROM fn_trace_getinfo(DEFAULT) 
WHERE property = 2 
  AND  value  IS  NOT  NULL 
 
-- Go back 4 files since default trace only keeps the last 5 and start from there.
SELECT @filename = substring(@filename, 0, charindex('_', @filename)+1) + convert(varchar, (convert(int, substring(left(@filename, len(@filename)-4), charindex('_', @filename)+1, len(@filename)))-4)) + '.trc'
 
SELECT 
       gt.EventClass, 
       gt.EventSubClass,
       te.Name AS EventName,
       gt.HostName, 
       gt.StartTime, 
       gt.DatabaseName,
       gt.ObjectName,
       gt.IndexID
FROM fn_trace_gettable(@fileName, DEFAULT) gt 
JOIN sys.trace_events te ON gt.EventClass = te.trace_event_id 
WHERE EventClass = 46
  and ObjectType = 22601
  and gt.DatabaseName <> 'tempdb'
ORDER BY StartTime desc; 

 

 

 

Recombinant time to rebuild the index & index

 

 

As shown below, Object: Altered the trace_event_id 164, where we can not distinguish ALTER INDEX ... REBUILD and   ALETER INDEX ... REORGANIZE to rebuild the index, the index recombination, the fn_trace_gettable TextData is Null value returned, and no judgment. So here to record the exact time, but can not distinguish between an index rebuild and index reorganization.

 

 

clip_image001

 

 

DECLARE @filename VARCHAR(500) 
SELECT @filename = CAST(value AS VARCHAR(500)) 
FROM fn_trace_getinfo(DEFAULT) 
WHERE property = 2 
  AND value IS NOT NULL 
 
-- Go back 4 files since default trace only keeps the last 5 and start from there.
SELECT @filename = substring(@filename, 0, charindex('_', @filename)+1) + convert(varchar, (convert(int, substring(left(@filename, len(@filename)-4), charindex('_', @filename)+1, len(@filename)))-4)) + '.trc'
 
SELECT 
       gt.EventClass, 
       gt.EventSubClass,
       te.Name AS EventName,
       gt.HostName, 
       gt.StartTime, 
       gt.DatabaseName,
       gt.ObjectName,
       gt.IndexID
FROM fn_trace_gettable(@fileName, DEFAULT) gt 
JOIN sys.trace_events te ON gt.EventClass = te.trace_event_id 
WHERE EventClass = 164
  and ObjectType = 22601
  and gt.DatabaseName <> 'tempdb'
ORDER BY StartTime desc; 
 

 

 

测试验证如下所示:

 

USE YourSQLDba;
GO
ALTER INDEX Pk_HistMaintTrav ON [Maint].[JobHistory] REBUILD;
 
ALTER INDEX PK_DataBaseSizeDtl_Day ON [Maint].[DataBaseSizeDtl_Day] REORGANIZE;
 
CREATE INDEX IX_DataBaseSizeDtl_Day_N1 ON [Maint].[DataBaseSizeDtl_Day](DataBaseName);

 

clip_image002

 

clip_image003

 

 

注意:上面脚本在有些环境可能会出错,主要是因为trac文件的路径,例如C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Log\log_603.trc 就会遇到下面错误,需要根据实际情况修改脚本。

 

Msg 245, Level 16, State 1, Line 8

Conversion failed when converting the varchar value '50.MSSQLSERVER\MSSQL\Log\log_603' to data type int.

 

 

 

参考资料

 

https://sqlconjuror.com/sql-server-get-index-creation-date/

Guess you like

Origin www.cnblogs.com/kerrycode/p/11720937.html