|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 3:20 AM
Points: 41,
Visits: 455
|
|
Hi all,
Do you think it is possible to use DMVs (or DMFs) to find out if a database is being accessed by users or applications? For example, I wondered if I could use sys.dm_db_index_usage_stats to work out if a database has been used lately at all. The results are possibly getting distorted by system processes, because I am getting last_user_seek or last_user_scan, etc. populated in some (non-system) tables very recently although I know that no users connections have been in the database. Perhaps the plan is flawed from the start, since reindexing and other maintenance jobs would presumably increment the scan counters, etc.
Has anyone else tried anything like this? Or does anyone know of a useful DMV for this purpose? Maybe a DMV that consolidates login info?
Basically, the problem I am trying to solve is I am being asked "is anyone accessing databases x,y,z and, if not, you may delete them!". Other than running profiler for a few days, I love to know if SS 2005 consolidates this info somewhere.
Many thanks. James
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Tuesday, June 04, 2013 7:03 AM
Points: 4,443,
Visits: 7,249
|
|
James
If you suspect the databases aren't being used at all, then run a Profiler trace against them for a week or a month or however long you need to. If you see no activity, set them to restricted user and leave them like that for the same period of time. If nobody squeals, take a final backup and remove the databases.
John
|
|
|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Yesterday @ 9:49 AM
Points: 13,436,
Visits: 25,281
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 3:20 AM
Points: 41,
Visits: 455
|
|
Thanks, these seem sensible suggestions for me to try. It's also useful to know there's no really obvious script possible so i can stop trying!
Cheers. James
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 3:13 PM
Points: 38,096,
Visits: 30,390
|
|
I'd say use the dm_exec_query_stats. I believe there is a DBID in there. The plan cache is transcient, so commands may get removed from the cache and not all statements are cached at all.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, January 17, 2013 3:20 AM
Points: 41,
Visits: 455
|
|
Oh yes, thanks. If you CROSS JOIN the sys.dm_exec_query_plan DMF to sys.dm_exec_query_stats you get a dbid. A bit of experimenting reveals that ad hoc plans don't seem to expose a dbid in sys.dm_exec_query_plan if the query is ad hoc as opposed to a stored procedure, but the stored proc query plans do give you a dbid, so this is something to work with.
So, so far I have the following if anyone is interested:
WITH last_query_by_db (dbid, Last_query) AS ( select dbid, max(last_execution_time) 'Last_query' from sys.dm_exec_query_stats cross apply sys.dm_exec_sql_text(plan_handle) group by dbid ) select d.name, Last_query from sys.databases d left outer join last_query_by_db q on q.dbid = d.database_id where d.name not in ('master','msdb','model','tempdb') order by 1
I think - but could be wrong - a database that has only ad hoc queries executed against it would appear (incorrectly) from these results to not be used, but i don't suppose this includes too many production systems.
Bit of a work in progress at the mo!
Many thanks. James
|
|
|
|