views:

344

answers:

5

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)
+2  A: 

For SQL Server 2008 you should take a look at SQL Auditing. This allows you to audit many things including selects on a table and reports to a file or Events Log.

dr
+3  A: 

Re: Profiler, if you monitor for SP:StmtCompleted, that will capture all statements executing within a stored procedure, so that will catch table accesses within a sproc. If not everything goes through stored procedures, you may also need the SQL:StmtCompleted event.

There will be a large number of events so it's probably still not practical to trace over a long time due to the size of trace. However, you could apply a filter - e.g. where TextData contains the name of your table you want to check for. You could give a list of table names to filter on at any one time and work through them gradually. So you should not get any trace events if none of those tables have been accessed.

Even if you feel it's not a suitable/viable approach for you, I thought it was worth expanding on.

Another solution would be to do a global search of your source code to find references to the tables. You can query the stored procedure definitions to check for matches for a given table, or just generate a complete database script and do a Find on that for table names.

AdaTheDev
+1 for source code search. Some applications have very infrequently-used features that might never be caught by profiling/auditing. The only way to be sure is to actually check the programs that use the database!
Aaronaught
Yep. I would trust a source code search a bit more (as long as you check ALL source code!) as that doesn't involve monitoring for actual hits on the table which could potentially be rare/require a long time to flag up for (how long do you monitor for?!).
AdaTheDev
The application that goes against this database does enough ad-hoc SQL that a stored proc code search wouldn't catch everything - the procs are mostly there for reporting and scheduled stuff. I like this time, though - I never realized the difference between the SP and SQL versions of "StmtCompleted"
rwmnau
@rwmnau: If it uses a lot of ad-hoc SQL then it should be even easier to find table references in the source code, no?
Aaronaught
Yes and no - we have the code for the SPs and reports, but the application is proprietary, and I don't have access to the source code. We've had it long enough that we know pretty well how it works, but I can't check the code to know for sure. My point was that I can't just do a syscomments search on the SP code to find table names, since the app does plenty of ad hoc that wouldn't show up in an SP.
rwmnau
A: 

A side-note : if your intent is to drop those tables, you might have to consider legal obligations that impose it on you to keep the data involved anyway for x years.

Erwin Smout
Thanks for the note - you're correct. I think this note is more appropriate as a comment on the question though, since it's not an answer. Welcome to SO!
rwmnau
A: 

Hello,

I had in mind to play with user permissions for different tables, but then I remembered you can turn on trace with an ON LOGON trigger you might benefit from this:

CREATE OR REPLACE TRIGGER SYS.ON_LOGON_ALL

AFTER LOGON ON DATABASE
WHEN (

USER 'MAX'

)
BEGIN

EXECUTE IMMEDIATE 'ALTER SESSION SET SQL_TRACE TRUE';

--EXECUTE IMMEDIATE 'alter session set events ''10046 trace name context forever level 12''';

EXCEPTION

WHEN OTHERS THEN

NULL;

END;

/

Then you can check your trace files.

Pentium10
+5  A: 

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.

Remus Rusanu
Wait - is this as awesome as I think it is? I'll do some testing, but it appears that I can monitor the "HEAP" virtual index on the table for any seeks/scans/lookups, and that tells me exactly what I want to know. You, sir, are my hero if this is the case.
rwmnau
It does what you think it does and more. You can differentiate between tables that are not used (0 heap or 1 clustered index) and non-clustered indexes never used so you can discard unnecessary non-clustered indexes. Just remeber that it resets when the server restarts (actually when a database comes online it resets for that db, details...)
Remus Rusanu
Outstanding. I'll update the question with the magic query I developed using your tip, but it's spectacular.
rwmnau
Just remember that no usage for a while may not mean no usage. Annual reports may only be run once a year. Tables to support some of these infrequent tasks may not see a lot of action (or any) the rest of the time.
HLGEM
Good point. It turns out that the database wasn't nearly as "ununsed" as I expected, and since our server has been up since October, we have a pretty solid snapshot of activity, including through year end. Thanks again for all the help!
rwmnau
Very handy query, +1!
edosoft