1

我想知道被访问的前 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

现在我需要这个用于桌子。有什么想法或链接吗?

4

1 回答 1

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替换:

于 2012-08-23T17:56:21.363 回答