Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a database table is being accessed anymore? Want something like a "SELECT trigger"

I have a very large database with hundreds of tables, and after many, many product upgrades, I'm sure half of them aren't being used anymore. How can I tell if a table is is actively being selected from? I can't just use Profiler - not only do I want to watch for more than a few days, but there are thousands of stored procedures as well, and profiler won't translate the SP calls into table access calls.

The only thing I can think of is to create a clustered index on the tables of interest, and then monitor the sys.dm_db_index_usage_stats to see if there are any seeks or scans on the clustered index, meaning that data from the table was loaded. However, adding a clustered index on every table is a bad idea (for any number of reasons), as isn't really feasible.

Are there other options I have? I've always wanted a feature like a "SELECT trigger", but there are probably other reasons why SQL Server doesn't have that feature either.

SOLUTION:

Thanks, Remus, for pointing me in the right direction. Using those columns, I've created the following SELECT, which does exactly what I want.

  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) 
like image 713
SqlRyan Avatar asked Jan 28 '10 15:01

SqlRyan


People also ask

How do you know if a table has triggers?

Just go to your table name and expand the Triggers node to view a list of triggers associated with that table.

How do you tell if a database is being used?

Another way to see if your database is in use is to look and see if the indexes are being used. Information on index usage is held in the sys. dm_db_index_usage_stats table since the last server reboot, and can be queried using this statement which can be tailored to select the data you need.

How can I tell when a SQL database was last accessed?

To get the last time when table was accessed in SQL Server, you can use SQL Server dynamic management view sys. dm_db_index_usage_stats, which returns counts of different types of index operations and the time each type of operation was last performed.

How can I tell if someone has access to my database?

HAS_DBACCESS returns 1 if the user has access to the database, 0 if the user has no access to the database, and NULL if the database name is not valid. HAS_DBACCESS returns 0 if the database is offline or suspect. HAS_DBACCESS returns 0 if the database is in single-user mode and the database is in use by another user.


1 Answers

Look in sys.dm_db_index_usage_stats. The columns last_user_xxx will contain the last time the table was accessed from user requests. This table resets its tracking after a server restart, so you must leave it running for a while before relying on its data.

like image 101
Remus Rusanu Avatar answered Oct 03 '22 09:10

Remus Rusanu