How to identify tables which are changing very often

Sam 1,476 Reputation points
2023-03-25T14:46:21.3166667+00:00

Hi All,

Is there any query using which we can tell what is the table which is changing the most or more frequently? The idea, is to identify those tables and run update statistics with full scan on daily basis instead of weekly basis.

Regards,

Sam

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,681 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sedat SALMAN 13,740 Reputation points
    2023-03-25T15:06:03.7766667+00:00

    In SQL Server, you can use the sys.dm_db_index_usage_stats dynamic management view to gather information about index usage and changes. This can help you identify tables with frequent updates, inserts, and deletes. Please note that these statistics are reset when the SQL Server instance is restarted.

    
    SELECT 
        OBJECT_NAME(ius.object_id) AS TableName,
        SUM(ius.user_updates) AS TotalUpdates,
        SUM(ius.user_seeks + ius.user_scans + ius.user_lookups) AS TotalReads
    FROM 
        sys.dm_db_index_usage_stats ius
    JOIN 
        sys.indexes idx ON ius.object_id = idx.object_id AND ius.index_id = idx.index_id
    WHERE 
        ius.database_id = DB_ID() -- Current database
        AND idx.type_desc <> 'HEAP'
    GROUP BY 
        ius.object_id
    ORDER BY 
        TotalUpdates DESC;
    
    

2 additional answers

Sort by: Most helpful
  1. RahulRandive 9,581 Reputation points
    2023-03-26T12:13:11.37+00:00

    Thanks for your question.

    Here is a query to find out when the table was last updated.

    If WHERE condition is removed, it will provide details of the entire database. (Need to run in specific database)

    SELECT OBJECT_NAME(OBJECT_ID) AS TableName,
    
     last_user_update,*
    
    FROM sys.dm_db_index_usage_stats
    
    WHERE database_id = DB_ID( 'database name')
    
    AND OBJECT_ID=OBJECT_ID('table name')
    
    

    Let us know if this help!


  2. LiHongMSFT-4306 26,706 Reputation points
    2023-03-27T06:19:44.35+00:00

    Hi @Samantha r

    Here is a workaround:

    Step 1: Enable Change Tracking for a Database;

    ALTER DATABASE AdventureWorks2019   
    SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)  
    

    Step 2: Enable Change Tracking for a Table;

    Step 3: Use the change tracking functions to obtain changes.

    Refer to this article for more details and examples: Work with change tracking (SQL Server).

    Best regards,

    Cosmog Hong


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.