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 12»»

Looking to select last time tables were queried Expand / Collapse
Author
Message
Posted Friday, February 22, 2013 1:58 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
I need a query to show all tables in a database sorted by last datetime queried ordered oldest first. Can someone help me with this?
Post #1423256
Posted Friday, February 22, 2013 2:04 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,110, Visits: 11,940
dndaughtery (2/22/2013)
I need a query to show all tables in a database sorted by last datetime queried ordered oldest first. Can someone help me with this?


There is nothing by default in sql that tracks when tables are accessed.

Are you trying to find tables are likely to be no longer needed? This kind of thing can be really tricky because there are usually processes that run only once a year but are vital.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1423261
Posted Friday, February 22, 2013 2:07 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
Yes Sean thats exactly what m trying to do. Trying to find out which table can be dropped.
Post #1423262
Posted Friday, February 22, 2013 2:10 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
But Im needing to do it on a weekly basis
Post #1423263
Posted Friday, February 22, 2013 2:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,110, Visits: 11,940
How are you going to handle those once a year type of things? I can understand wanting to do this periodically but weekly? Are you really going to drop tables if they haven't been accessed within the last week? What about times when overall activity is low, like the holidays? This should not be an automated type of process. It requires manual intervention or you will end up dropping something that is needed.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1423266
Posted Friday, February 22, 2013 2:27 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
This is for an adhoc enviroment for report development.
Post #1423269
Posted Friday, February 22, 2013 2:55 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
Found it!


USE MyDB
GO
SET ANSI_WARNINGS OFF;
SET NOCOUNT ON;
GO

WITH agg AS
(
SELECT
[object_id],
last_user_seek,
last_user_scan,
last_user_lookup,
last_user_update
FROM
sys.dm_db_index_usage_stats
WHERE
database_id = DB_ID()
)
SELECT
[Schema] = OBJECT_SCHEMA_NAME([object_id]),
[Table_Or_View] = OBJECT_NAME([object_id]),
last_read = MAX(last_read),
last_write = MAX(last_write)
FROM
(
SELECT [object_id], last_user_seek, NULL FROM agg
UNION ALL
SELECT [object_id], last_user_scan, NULL FROM agg
UNION ALL
SELECT [object_id], last_user_lookup, NULL FROM agg
UNION ALL
SELECT [object_id], NULL, last_user_update FROM agg
) AS x ([object_id], last_read, last_write)
where OBJECT_SCHEMA_NAME([object_id]) = 's012sod'
GROUP BY
OBJECT_SCHEMA_NAME([object_id]),
OBJECT_NAME([object_id])
ORDER BY last_read asc;

Post #1423274
Posted Monday, February 25, 2013 7:10 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:47 AM
Points: 13,110, Visits: 11,940
dndaughtery (2/22/2013)
Found it!


Be careful now. That only shows index usage. If your queries are not sargable or you have a heap your results may not be exactly what you are looking for.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1423602
Posted Monday, February 25, 2013 8:01 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, July 18, 2014 1:57 PM
Points: 313, Visits: 715
How can a correct that?
Post #1423623
Posted Monday, February 25, 2013 8:07 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 11:19 AM
Points: 42,466, Visits: 35,529
This kind of thing is incredibly difficult to do.

Probably best thing you can do is set up an extended events session for Object accessed (I think it's in the 2008 XE), and put that to a event bucketizer target (bucketize on the object id). Watch the overhead though, that's a frequent event

Now that won't tell you what tables aren't used, it will tell you which tables are known to be used. Run that for long enough (where long enough covers an entire business cycle) and you can use that as a place to start for deciding which tables are not known to be used. Doesn't mean they aren't, just means they might not be,



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 #1423628
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse