Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Find out if a database is being used and how much Expand / Collapse
Author
Message
Posted Wednesday, April 23, 2008 3:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 3:45 AM
Points: 41, Visits: 459
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
Post #489149
Posted Wednesday, April 23, 2008 4:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:56 AM
Points: 5,383, Visits: 9,951
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
Post #489156
Posted Wednesday, April 23, 2008 6:19 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 9:27 PM
Points: 13,776, Visits: 28,178
I don't think there is one that tracks connections historically. If you really don't want to run Profiler, you might try running queries against sys.dm_exec_query_stats and see if any queries have been run against the database in question. That's about the best that I know of.

----------------------------------------------------
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood..." Theodore Roosevelt
The Scary DBA
Author of: SQL Server Query Performance Tuning
SQL Server 2012 Query Performance Tuning
SQL Server 2008 Query Performance Tuning Distilled
and
SQL Server Execution Plans

Product Evangelist for Red Gate Software
Post #489207
Posted Wednesday, April 23, 2008 9:57 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 3:45 AM
Points: 41, Visits: 459
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
Post #489394
Posted Wednesday, April 23, 2008 12:27 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 5:09 PM
Points: 39,968, Visits: 36,327
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

Post #489493
Posted Thursday, April 24, 2008 5:10 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, August 20, 2014 3:45 AM
Points: 41, Visits: 459
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
Post #489800
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse