View sql server last index rebuild time

sakuraime 2,341 Reputation points
2020-11-12T15:37:32.88+00:00

How to check last index rebuild time ( instead of checking last update stats time)
?

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. m 4,276 Reputation points
    2020-11-13T02:28:11.387+00:00

    Hi @sakuraime ,

    How to check last index rebuild time ( instead of checking last update stats time)?

    If my understanding is right, you want to know the last update time of executing the t_sql of rebuiding the index.
    Actually,you can find all the operations for changing the table in your transaction log, run code as next or use tool as Apex SQL,it will show clearly what you have done of the data, and it is more easier to understand than reading log information by using dbcc log:

    DBCC LOG('Adventureworks2017',3)  
    

    However, you need to know what the information means of the record , there is one description column, you can check the details of the transaction:
    39508-20201113getdate1.jpg
    39488-20201113allopterations.jpg
    39516-20201113description.jpg

    Note: if you want to know last update stats time,you can try code as next, though I guess you have already knew the code.
    Try code as next:

    DECLARE @table_name NVARCHAR(32);  
    SET @table_name='Person.Person'  
    SELECT sch.name + '.' + so.name AS table_name  
          , so.object_id  
          , ss.name  AS stat_name  
          , ds.stats_id  
          , ds.last_updated  
          , ds.rows  
          , ds.rows_sampled  
          , ds.rows_sampled*1.0/ds.rows *100 AS sample_rate  
          , ds.steps  
          , ds.unfiltered_rows  
          --, ds.persisted_sample_percent  
          , ds.modification_counter   
          , 'UPDATE STATISTICS ' + QUOTENAME(DB_NAME()) + '.' + QUOTENAME(sch.name) + '.' + QUOTENAME( so.name) + ' "' +  RTRIM(LTRIM(ss.name)) + '" WITH SAMPLE 80 PERCENT;'  
            AS update_stat_script  
    FROM sys.stats ss  
    JOIN sys.objects so ON ss.object_id = so.object_id  
    JOIN sys.schemas sch ON so.schema_id = sch.schema_id  
    CROSS APPLY sys.dm_db_stats_properties(ss.object_id,ss.stats_id) ds  
    WHERE  so.is_ms_shipped = 0   
            AND so.object_id NOT IN (  
            SELECT  major_id  
            FROM    sys.extended_properties (NOLOCK)  
            WHERE   name = N'microsoft_database_tools_support' )  
            AND so.object_id =OBJECT_ID(@table_name)  
    

    BR,
    Mia


    If the answer is helpful, please click "Accept Answer" and upvote it.


1 additional answer

Sort by: Most helpful
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2020-11-12T22:59:44.527+00:00

    I don't think that is logged per se, but sys.objects.modify_date is updated on index rebuilds.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.