Sunday, September 1, 2019

SQL Script to find Last Statistics update date for a Table


You can find below query useful when you are trying to see the last stats update date of few selected tables in SQL Server.

Query:

SELECT
    sch.name + '.' + so.name AS 'Table',
    ss.name AS 'Statistic',
      CASE
            WHEN ss.auto_Created = 0 AND ss.user_created = 0 THEN 'Index Statistic'
            WHEN ss.auto_created = 0 AND ss.user_created = 1 THEN 'User Created'
            WHEN ss.auto_created = 1 AND ss.user_created = 0 THEN 'Auto Created'
            WHEN ss.AUTO_created = 1 AND ss.user_created = 1 THEN 'Not Possible?'
      END AS
'Statistic Type',
    CASE
            WHEN ss.has_filter = 1 THEN 'Filtered Index'
            WHEN ss.has_filter = 0 THEN 'No Filter'
      END AS
'Filtered?' ,
    sp.last_updated AS 'Stats Last Updated',     sp.rows AS 'Rows',
    sp.rows_sampled AS
'Rows Sampled',
    sp.unfiltered_rows AS
'Unfiltered Rows',
      sp.modification_counter AS
'Row Modifications',
      sp.steps AS
'Histogram Steps'
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
OUTER APPLY sys.dm_db_stats_properties(so.object_id, ss.stats_id) AS sp WHERE so.TYPE = 'U' 
and so.name in('Your_Table_Name') --Enter Your table name here

ORDER BY sp.last_updated DESC;


No comments:

Post a Comment