March 30, 2009 at 7:12 am
Hi,
How do i find the unused databases or last accessed datetime of databases. I want to find it and drop all the databases which are not accessed for past 3 months.
Please help me.
March 30, 2009 at 8:03 am
I assume you are talking about a wild, non-secure development box.
Shutdown the suspected databases, wait three month, if nobody comes crying foul take a full backup and drop it.
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.March 30, 2009 at 8:53 am
😎
My Manager asked me to check and drop the databases which are not used for past 3 months.
How do i do it?
March 30, 2009 at 5:52 pm
Set up a profiler trace and filter it for the databases you need to drop and check whether you see any connections for these databases? Do this activity for good amount of time and then decide accordingly.
MJ
March 30, 2009 at 11:07 pm
What I do on servers that I suspect the databases/logins are not being used is setup a DBManagement database. That gets populated with connection information every min by SQL Agent jobs so I can later run queries against the tables in DBManagement to see what is in use and what is not.
Not recommend on heavy used servers; as the job running every min would be too much performance impact; question is how often you think is enough?
Thanks.
Mohit.
Mohit K. Gupta, MCITP: Database Administrator (2005), My Blog, Twitter: @SQLCAN[/url].
Microsoft FTE - SQL Server PFE
* Some time its the search that counts, not the finding...
* I didn't think so, but if I was wrong, I was wrong. I'd rather do something, and make a mistake than be frightened and be doing nothing. :smooooth:[/font]
March 31, 2009 at 6:31 am
In sql 2005/2008 and the service us running for a long time
you can run this script, and you can get an idia of wht is hapenning:
--time sql service started
select last_batch from sys.sysprocesses where spid=1
---
SELECT db_name(d.database_id),MAX(last_user_SCAN),MAX(last_user_Seek),MAX(last_user_lookup),max(last_user_update)
from sys.databases d
left join sys.dm_db_index_usage_stats i on d.database_id=i.database_id
group by db_name(d.database_id)
March 31, 2009 at 12:01 pm
A somewhat non-intrusive method is to set the databases to 'AUTO CLOSE'.
Then all you have to do is scan the errorlog to see which ones are used !
RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."
March 31, 2009 at 6:59 pm
In SQL 2008 u could log/audit any statements run across the suspected DB's.
Or just.
You could use a trigger on the connection event.
But the auto-close idea could be a Q and D fix.
Carlton..
June 22, 2010 at 8:18 am
yehuda klein (3/31/2009)
SELECT db_name(d.database_id),MAX(last_user_SCAN),MAX(last_user_Seek),MAX(last_user_lookup),max(last_user_update)from sys.databases d
left join sys.dm_db_index_usage_stats i on d.database_id=i.database_id
group by db_name(d.database_id)
Thanks for this script yehuda; it gives me what I need. 🙂
June 22, 2010 at 8:33 am
rudy komacsar - Doctor "X" (3/31/2009)
A somewhat non-intrusive method is to set the databases to 'AUTO CLOSE'.Then all you have to do is scan the errorlog to see which ones are used !
Best solution I've ever read on this forum.
However it assumes you don't need the hd space right away. In that case backup and send to dvds or where you can for quick recovery then drop from server. Just make sure you tell your boss that recovery time will be in the hours range rather than seconds (or ms with Rudy's solution).
June 22, 2010 at 9:46 am
yehuda klein (3/31/2009)
SELECT db_name(d.database_id),MAX(last_user_SCAN),MAX(last_user_Seek),MAX(last_user_lookup),max(last_user_update)from sys.databases d
left join sys.dm_db_index_usage_stats i on d.database_id=i.database_id
group by db_name(d.database_id)
NB. That DMV is flushed when the DB is started. Offline/online, detach/attach, close/open, restore, restart of SQL all will set the seeks and scans dates to null.
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
May 30, 2021 at 8:59 pm
If the last_access column is null then no reads or writes have occurred:
WITH cte AS (
SELECT database_id, dt, op
FROM sys.dm_db_index_usage_stats
UNPIVOT (dt for op in (last_user_lookup, last_user_scan, last_user_seek, last_user_update)) b)
SELECT d.name DB, MAX(c.dt) last_access, MAX(i.sqlserver_start_time) sqlserver_start_time, GETUTCDATE() captured
FROM sys.databases d
LEFT JOIN cte c ON d.database_id=c.database_id
CROSS JOIN sys.dm_os_sys_info i
WHERE d.database_id>4
GROUP BY d.name
ORDER BY d.name;
May 30, 2021 at 11:54 pm
If the last_access column is null then no reads or writes have occurred:
WITH cte AS (
SELECT database_id, dt, op
FROM sys.dm_db_index_usage_stats
UNPIVOT (dt for op in (last_user_lookup, last_user_scan, last_user_seek, last_user_update)) b)
SELECT d.name DB, MAX(c.dt) last_access, MAX(i.sqlserver_start_time) sqlserver_start_time, GETUTCDATE() captured
FROM sys.databases d
LEFT JOIN cte c ON d.database_id=c.database_id
CROSS JOIN sys.dm_os_sys_info i
WHERE d.database_id>4
GROUP BY d.name
ORDER BY d.name;
Heh... especially in the first several milli-seconds or maybe even days after you've done a restart of the SQL Server Service or the box it's on. For that matter, you could have important databases that are used only once per month or even less.
I strongly recommend that if you use this method to determine if a database is still used or not that 1) you make damned sure that you have a viable, quickly restore-able backup that you've tested for restore-ability and 2) that your resume is up to date and 3) that you stand a chance of actually getting a job after you use this method to drop supposedly unused databases. 😉
Also... while I always appreciate someone adding to a more than a decade old thread with additional information, you might not be aware of the fact that this thread was last active more than a decade ago prior to your post on it.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply