How to find Outdated Index Statistics in SQL

Bala Narasimha Challa 466 Reputation points
2021-08-19T11:01:59.907+00:00

Hi Team,

How to find Outdated Index Statistics in SQL server

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. AmeliaGu-MSFT 14,006 Reputation points Microsoft External Staff
    2021-08-20T03:31:41.243+00:00

    Hi balanarasimhac-1495,

    We can use sys.dm_db_stats_properties and sys.stats to find the outdated statistics. Please refer to this query which might help:

    SELECT   
      st.object_id                          AS [Table ID]  
    , OBJECT_NAME(st.object_id)             AS [Table Name]  
    , st.name                               AS [Index Name]  
    , STATS_DATE(st.object_id, st.stats_id) AS [LastUpdated]  
    , modification_counter                  AS [Rows Modified]  
    FROM  
    sys.stats st   
    CROSS APPLY  
    sys.dm_db_stats_properties(st.object_id, st.stats_id) AS sp   
    WHERE  
    STATS_DATE(st.object_id, st.stats_id)<=DATEADD(DAY,-1,GETDATE())    
    AND modification_counter > 0   
    AND OBJECTPROPERTY(st.object_id,'IsUserTable')=1  
    GO  
    

    Best Regards,
    Amelia


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2021-08-19T11:34:55.517+00:00
    1 person found this answer helpful.
    0 comments No comments

  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-08-19T21:58:44.803+00:00

    That's non-trivial. Because when is a statistics "outdated"? We probably mean that it is outdated when the optimizer produces a bad plan. But that depends a lot on the query. So there may be tables where statistics have not been updated for ages, but we will get good plans. And there are tables where you may need to update statistics more than once a day.

    An experienced database professional recognises an outdated statistics when he or she encounters them when battling a query. But in the general case?

    As Olaf says, best is to run index maintenance in advance to prevent it from happening.

    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.