我想知道访问过的前N个表。我只获得了数据库和谷歌地址中的大多数链接,可以访问特定表的最后日期。
SELECT DatabaseName, MAX(LastAccessDate) LastAccessDate
FROM
(SELECT
DB_NAME(database_id) DatabaseName
, last_user_seek
, last_user_scan
, last_user_lookup
, last_user_update
FROM sys.dm_db_index_usage_stats) AS PivotTable
UNPIVOT
(LastAccessDate FOR last_user_access IN
(last_user_seek
, last_user_scan
, last_user_lookup
, last_user_update)
) AS UnpivotTable
GROUP BY DatabaseName
HAVING DatabaseName NOT IN ('master', 'tempdb', 'model', 'msdb')
ORDER BY 2
现在我需要这个表格。请问任何想法或链接?
答案 0 :(得分:4)
您可以一次轻松地对一个数据库执行此操作,只需稍微调整一下代码:
USE Your_Database;
GO
SELECT [schema] = OBJECT_SCHEMA_NAME([object_id]),
[object] = OBJECT_NAME([object_id]),
LastAccessDate = MAX(LastAccessDate)
FROM
(SELECT
[object_id]
, last_user_seek
, last_user_scan
, last_user_lookup
, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID()) AS PivotTable
UNPIVOT
(LastAccessDate FOR last_user_access IN
(last_user_seek
, last_user_scan
, last_user_lookup
, last_user_update)
) AS UnpivotTable
GROUP BY [object_id]
ORDER BY LastAccessDate;
当然,这只是自上次服务重启或重新配置以来的好处。
如果您想对每个数据库执行此操作,则可以使用我的自定义sp_MSforeachdb
替换: