在SQL Server 2005中,您可以轻松确定某人查询数据库的最后时间.
SELECT last_user_seek = MAX(last_user_seek), last_user_scan = MAX(last_user_scan), last_user_lookup = MAX(last_user_lookup), last_user_update = MAX(last_user_update) FROM sys.dm_db_index_usage_stats WHERE [database_id] = DB_ID()
使用此方法的一个警告是,每当您重新启动SQL Server时,DMV中的信息都将被清除并清空.
扩展James Allen的答案:
SELECT d.name, last_user_seek = MAX(last_user_seek), last_user_scan = MAX(last_user_scan), last_user_lookup = MAX(last_user_lookup), last_user_update = MAX(last_user_update) FROM sys.dm_db_index_usage_stats AS i JOIN sys.databases AS d ON i.database_id=d.database_id GROUP BY d.name
如果您不希望每个数据库上下文生成结果并希望在结果集的开头包含数据库名称,请使用此修改版本.
在MySQLtips找到了这个-对我有用。
select d.name, x1 = (select X1= max(bb.xx) from ( select xx = max(last_user_seek) where max(last_user_seek) is not null union all select xx = max(last_user_scan) where max(last_user_scan) is not null union all select xx = max(last_user_lookup) where max(last_user_lookup) is not null union all select xx = max(last_user_update) where max(last_user_update) is not null) bb) FROM master.dbo.sysdatabases d left outer join sys.dm_db_index_usage_stats s on d.dbid= s.database_id group by d.name