table not used or accesses in last 60 days

table not used or accesses in last 60 days

 

Query

 

SELECT
    t.name AS TableName,
    t.create_date,
    COALESCE(i.last_user_seek, i.last_user_scan, i.last_user_lookup, i.last_user_update) AS LastAccess
FROM
    sys.tables t
LEFT JOIN
    sys.dm_db_index_usage_stats i ON t.object_id = i.object_id
WHERE
 
        i.last_user_seek IS NULL
        AND i.last_user_scan IS NULL
        AND i.last_user_lookup IS NULL
        AND i.last_user_update IS NULL
        OR DATEDIFF(DAY, COALESCE(i.last_user_seek, i.last_user_scan, i.last_user_lookup, i.last_user_update), GETDATE()) > 30 -- 60 days
    AND t.is_ms_shipped = 0 -- Exclude system tables
ORDER BY
    t.create_date;

Share This with your friend by choosing any social account


Upcoming Articles
You may also read following recent Post
Copyright Future Minutes © 2015- 2024 All Rights Reserved.   Terms of Service  |   Privacy Policy |  Contact US|  Pages|  Whats new?
Update on: Dec 20 2023 05:10 PM