Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This next year, I am helping an effort to clean several SQL Server environments.

We have about 10,000 stored procedures and estimate that only about 1000 of them are used on a regular basis, and another 200 or so are used on a rare occasion, meaning we have a lot of work to do.

Since we have multiple departments and teams that can access these databases and procedures, we are not always the ones calling the procedures, meaning that we must determine what procedures are being called. On top of that, we want to determine this over a few months, not in a few days (which eliminates some possibilities).

One approach to this is to use the SQL Server Profiler and track what procedures are being called and compare them to the list of what procedures we have, while marking whether the procedures are used or not. From then, we could move the procedures to a different schema in case a department comes screaming.

Is using the Profiler the most effective approach here? And/Or have any of you done something similar and found another way/better way to do this?

share|improve this question
up vote 22 down vote accepted

You can use server side trace (different from using Profiler GUI that incurs more resources) during your testing or your business cycle and capture only stuff related to SP's. Then you can load that in a table or excel for further analysis.

Second approach, is to use DMV sys.dm_exec_procedure_stats (with limitation that if sql server is restarted, then the data is flushed).

You can even schedule a job to capture DMV data to a table to keep it persisted.

 -- Get list of possibly unused SPs (SQL 2008 only)
    SELECT AS 'SP Name'        -- Get list of all SPs in the current database
    FROM sys.procedures AS p
    WHERE p.is_ms_shipped = 0


    SELECT AS 'SP Name'        -- Get list of all SPs from the current database 
    FROM sys.procedures AS p          -- that are in the procedure cache
    INNER JOIN sys.dm_exec_procedure_stats AS qs
    ON p.object_id = qs.object_id
    WHERE p.is_ms_shipped = 0;

Refer to :

share|improve this answer
Also see… and… (ignoring that on the latter the link to SQLServerPedia is now dead). – Aaron Bertrand Jul 25 '13 at 21:58
Make sure you check the DMV periodically over the course of weeks or even months as there may be SPs that only get run on a monthly or even quarterly basis. DMVs are cleared out when the instance is restarted, cleared manually, or even just over time. – Kenneth Fisher Jul 25 '13 at 22:22
@KennethFisher Thats why I recommended to schedule a job to capture DMV data to a table. Thanks for mentioning though ! – Kin Jul 25 '13 at 23:56

If you are on SQL Server 2008+ you can also use extended events with a histogram target. Possibly this would be more light weight than a trace.

AFAIK you would need to create a different session for each database of interest though as I couldn't see any indication that bucketizing on multiple columns was possible. The quick example below filters on database_id=10

CREATE EVENT SESSION [count_module_start_database_10]
ADD EVENT sqlserver.module_start
        WHERE (source_database_id=10) 
ADD TARGET package0.asynchronous_bucketizer
(     SET  filtering_event_name='sqlserver.module_start', 
            slots = 10000
ALTER EVENT SESSION [count_module_start_database_10]

And then after running some stored procedures in that DB a few times and retrieving the data with

SELECT CAST(target_data as XML) target_data
FROM sys.dm_xe_sessions AS s 
JOIN sys.dm_xe_session_targets t
    ON s.address = t.event_session_address
WHERE = 'count_module_start_database_10'

The output is

<HistogramTarget truncated="0" buckets="16384">
  <Slot count="36">
  <Slot count="3">
  <Slot count="2">

Showing that the procedure with object_id of 1287675635 was executed 36 times for example. The asynchronous_bucketizer is memory only so it would be best to set up something that polls this every so often and saves to persistent storage.

share|improve this answer
It is true, you need one session per database. Would be great to say WHERE (source_database_id IN (10,15,20)) but alas this is not supported. – Aaron Bertrand Jul 26 '13 at 0:10
@AaronBertrand - And even if it was supported you would still need to count procedure calls for objects with the same object_id (or same object_name) in different databases separately and I don't think that is possible either. – Martin Smith Jul 26 '13 at 7:14

You can find this question useful, it applies to tables and columns but suggests using a third party tool ApexSQL Clean which can also find unused stored procedures as well as all objects that are not referenced by any other object in database, or in external databases

Disclaimer: I work for ApexSQL as a Support Engineer

share|improve this answer
The OP does not want to find unreferenced stored procedures, instead OP want to find unused SP. Your answer does not serve as an answer for this question. – Kin Jan 23 '14 at 13:09
Kin i'll update. ApexSQL Clean marks unused objects as unreferenced so I understand that that caused the confusion – Milica Medic Jan 23 '14 at 13:52

This post also provides a script to find unused ojects: Find the unused database tables in SQL Server Below is the script from the article, I changed table type "U" into stored procedure type "P":

   USE DBName;

       ao.[name] [Table],
       s.[name] [Schema],
       [create_date] [Created],
        [modify_date] [LastModified]
         sys.all_objects ao JOIN sys.schemas s
           ON ao.schema_id = s.schema_id
         OBJECT_ID NOT IN (
              SELECT OBJECT_ID
              FROM sys.dm_db_index_usage_stats
        AND [type] = 'P'
        [modify_date] DESC
share|improve this answer
This will always return all procedures as procedures don't get entries made in the index usage stats DMV... – Martin Smith Jun 1 '14 at 16:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.