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


Find tables unused since last SQL Server restart


Find tables unused since last SQL Server restart

Author
Message
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87227 Visits: 45272
There are a number of things that can cause a stored proc's plan to be dropped from cache. Stats updates, index rebuilds, table alterations, etc. Be very careful relying on what's in sys.dm_exec_cached_plans
http://sqlinthewild.co.za/index.php/2009/07/10/why-the-dmvs-are-not-a-replacement-for-sql-trace/

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


Chris Holding
Chris Holding
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 70
This works for SQL 2005 and above but I'm having problems with SQL 2000; can anyone rewrite for SQL 2000?

Is there and equivalent for sys.dm_db_index_usage_stats etc in SQL 2000?

Thanks in advance
Mo-455357
Mo-455357
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 10
Is there to find unused functions and procedures as well? Or time since last call to these objects?

Thanks
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87227 Visits: 45272
Chris Holding (11/4/2010)
This works for SQL 2005 and above but I'm having problems with SQL 2000; can anyone rewrite for SQL 2000?


No, because the information is not there in SQL 2000.
If you want this kind of info in SQL 2000, you'll need a trace (SQL Trace)

Is there and equivalent for sys.dm_db_index_usage_stats etc in SQL 2000?

No

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


bgrover
bgrover
Old Hand
Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)

Group: General Forum Members
Points: 362 Visits: 61
So I added a bit to it and made it so it will run aginst every database on the server. Hope this helps someone.


CREATE TABLE #LastAccess (DatabaseName sysname,
TableName sysname,
LastSelect datetime)

DECLARE @SQL varchar(max)
SET @SQL = 'USE ?; '
+ 'WITH LastActivity (ObjectID, LastAction) AS '
+ '( '
+ 'SELECT object_id, '
+ 'last_user_seek as LastAction '
+ 'FROM sys.dm_db_index_usage_stats '
+ 'WHERE database_id = db_id(db_name()) '
+ 'UNION SELECT object_id, '
+ 'last_user_scan as LastAction '
+ 'FROM sys.dm_db_index_usage_stats '
+ 'WHERE database_id = db_id(db_name()) '
+ 'UNION SELECT object_id, '
+ 'last_user_lookup as LastAction '
+ 'FROM sys.dm_db_index_usage_stats '
+ 'WHERE database_id = db_id(db_name()) '
+ ') '
+ 'INSERT INTO #LastAccess '
+ 'SELECT DB_NAME() AS DatabaseName, '
+ '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) '

EXEC sp_MSforeachdb @SQL

SELECT * FROM #LastAccess





pranav_parashar
pranav_parashar
Forum Newbie
Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)Forum Newbie (8 reputation)

Group: General Forum Members
Points: 8 Visits: 19
Many thanks for such useful script !!!!
RedCAS
RedCAS
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 16
Very helpful. Thanks for the script :-D
Ninja's_RGR'us
Ninja's_RGR'us
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28847 Visits: 9671
You need to wrap the first ? with [?] so that non-standard db names don't fail.
bgrover
bgrover
Old Hand
Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)Old Hand (362 reputation)

Group: General Forum Members
Points: 362 Visits: 61
Yes, thanks good catch!



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