Your client come up to you and says , “Hey, I wanna list of all of our databases and when they were last accessed”.
You might be okay if you have auditing enabled or if there is a trace or xevent capturing this kind of data, but I'm guessing none of them exist. That's great, I don't usually monitor things like that either.
Is there a method for you to obtain that information if you're not keeping an eye on it? Nothing particularly clear comes from SQL; there is no magic in the "last_access_date" column in sys.databases or anywhere else for that matter.
It's possible to work around this issue; have a look at sys.dm_db_index_usage_stats. For every index in a database, you will observe the following intriguing columns: last_user_seek, last_user_scan, last_user_lookup, and last_user_update.
You may find out when the database was last visited by using the code below, which will provide the most recent date that any index in the database was modified.
SELECT db_name(databases.database_id) AS DBName, (SELECT MAX(last_user_access) FROM (VALUES (MAX(last_user_seek)),(MAX(last_user_scan)) ,(MAX(last_user_lookup))) AS value(last_user_access)) AS last_user_access FROM sys.dm_db_index_usage_stats indexstats RIGHT OUTER JOIN sys.databases databases ON indexstats.database_id = databases.database_id GROUP BY databases.database_id ORDER BY db_name(databases.database_id)Note: sys.dm_db_index_usage_stats like many DMVs is cleared out on a SQL restart.
No comments:
Post a Comment