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

DB Last Used Date Expand / Collapse
Author
Message
Posted Monday, September 20, 2010 2:53 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 10, 2012 12:51 PM
Points: 187, Visits: 144
Hi Forum,

As we are on the way to cleaning Production server, we need to find out the last usage date of each database. sothat we can clean the databases which are not used for a long time. Is there any way to find the last usage date of the database.

thanks for the help.


KK
Post #989178
Posted Monday, September 20, 2010 3:57 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 6:59 AM
Points: 1,400, Visits: 6,894
Not exactly what you are looking for, but the following code can be used as a starting point.
I came across this code in SSC some time ago - it shows the date each table of a database was last accessed, since the server/database was restarted:

WITH LastActivity (ObjectID, LastAction) AS
(
SELECT OBJECT_ID AS TableName,
last_user_seek AS LastAction
FROM sys.dm_db_index_usage_stats u
WHERE database_id = DB_ID(DB_NAME())
UNION
SELECT OBJECT_ID AS TableName,
last_user_scan AS LastAction
FROM sys.dm_db_index_usage_stats u
WHERE database_id = DB_ID(DB_NAME())
UNION
SELECT OBJECT_ID AS TableName,
last_user_lookup AS LastAction
FROM sys.dm_db_index_usage_stats u
WHERE database_id = DB_ID(DB_NAME())
)
SELECT OBJECT_NAME(so.OBJECT_ID) AS TableName,
MAX(la.LastAction) AS LastSelect
FROM sys.objects so
LEFT
JOIN LastActivity la
ON so.OBJECT_ID = la.ObjectID
WHERE so.TYPE = 'U'
AND so.OBJECT_ID > 100
GROUP BY OBJECT_NAME(so.OBJECT_ID)
ORDER BY OBJECT_NAME(so.OBJECT_ID)

Edit: since server/database restart.


BrainDonor
Linkedin
Post #989206
Posted Monday, September 20, 2010 1:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, May 17, 2013 12:22 PM
Points: 10,571, Visits: 11,871
PavanKK (9/20/2010)
Hi Forum,

As we are on the way to cleaning Production server, we need to find out the last usage date of each database. sothat we can clean the databases which are not used for a long time. Is there any way to find the last usage date of the database.

thanks for the help.


KK


You need to be careful, what if there is a a database that is only used for reporting on a yearly basis? I have not tested BrainDonor's code, but I'm going to guess that if you have any kind of index maintenance going on then it will show up in that query.




Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #989730
Posted Monday, September 24, 2012 9:19 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, September 24, 2012 9:15 AM
Points: 1, Visits: 6
Hello ,

What happen if some tables don't have any index?

Will this code works in that case?

I have some doubt.

Can you confirm this code will work in that case?

Post #1363562
Posted Monday, September 24, 2012 9:34 AM


SSC-Dedicated

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

Group: General Forum Members
Last Login: Today @ 3:28 PM
Points: 37,730, Visits: 29,996
It will.


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 #1363568
Posted Tuesday, October 02, 2012 3:10 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, March 25, 2013 4:04 AM
Points: 4, Visits: 155
BrainDonor,

Do you have a similar code for SQL 2000 ?

Thank you.
Post #1366915
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse