Find out if a database is being used and how much

  • Hi all,

    Do you think it is possible to use DMVs (or DMFs) to find out if a database is being accessed by users or applications? For example, I wondered if I could use sys.dm_db_index_usage_stats to work out if a database has been used lately at all. The results are possibly getting distorted by system processes, because I am getting last_user_seek or last_user_scan, etc. populated in some (non-system) tables very recently although I know that no users connections have been in the database. Perhaps the plan is flawed from the start, since reindexing and other maintenance jobs would presumably increment the scan counters, etc.

    Has anyone else tried anything like this? Or does anyone know of a useful DMV for this purpose? Maybe a DMV that consolidates login info?

    Basically, the problem I am trying to solve is I am being asked "is anyone accessing databases x,y,z and, if not, you may delete them!". Other than running profiler for a few days, I love to know if SS 2005 consolidates this info somewhere.

    Many thanks.

    James

  • James

    If you suspect the databases aren't being used at all, then run a Profiler trace against them for a week or a month or however long you need to. If you see no activity, set them to restricted user and leave them like that for the same period of time. If nobody squeals, take a final backup and remove the databases.

    John

  • I don't think there is one that tracks connections historically. If you really don't want to run Profiler, you might try running queries against sys.dm_exec_query_stats and see if any queries have been run against the database in question. That's about the best that I know of.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks, these seem sensible suggestions for me to try. It's also useful to know there's no really obvious script possible so i can stop trying!

    Cheers.

    James

  • I'd say use the dm_exec_query_stats. I believe there is a DBID in there. The plan cache is transcient, so commands may get removed from the cache and not all statements are cached at all.

    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
  • Oh yes, thanks. If you CROSS JOIN the sys.dm_exec_query_plan DMF to sys.dm_exec_query_stats you get a dbid. A bit of experimenting reveals that ad hoc plans don't seem to expose a dbid in sys.dm_exec_query_plan if the query is ad hoc as opposed to a stored procedure, but the stored proc query plans do give you a dbid, so this is something to work with.

    So, so far I have the following if anyone is interested:

    WITH last_query_by_db (dbid, Last_query) AS (

    select

    dbid, max(last_execution_time) 'Last_query'

    from

    sys.dm_exec_query_stats

    cross apply

    sys.dm_exec_sql_text(plan_handle)

    group by

    dbid

    )

    select

    d.name, Last_query

    from

    sys.databases d

    left outer join

    last_query_by_db q on q.dbid = d.database_id

    where

    d.name not in ('master','msdb','model','tempdb')

    order by

    1

    I think - but could be wrong - a database that has only ad hoc queries executed against it would appear (incorrectly) from these results to not be used, but i don't suppose this includes too many production systems.

    Bit of a work in progress at the mo!

    Many thanks.

    James

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply