SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Find out if a database is being used and how much


Find out if a database is being used and how much

Author
Message
Jimmy M
Jimmy M
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 568
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
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13892 Visits: 15926
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
Grant Fritchey
Grant Fritchey
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39039 Visits: 32616
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 and SQL Server Execution Plans
Product Evangelist for Red Gate Software
Jimmy M
Jimmy M
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 568
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86148 Visits: 45229
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, MVP, M.Sc (Comp Sci)
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


Jimmy M
Jimmy M
SSC-Enthusiastic
SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)SSC-Enthusiastic (113 reputation)

Group: General Forum Members
Points: 113 Visits: 568
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search