I am trying to track down all stored procedures in a database that have never been used, or that have not been used in many months.
I would like to find a query to show all the stored procedures that are not in use so that those stored procedures can be analyzed to determine if they can be removed.
I am familiar with sys.procedures, but don’t know how to determine if a procedure is in use or not.
SELECT *
FROM sys.procedures;
Using SQL Server 2008 R2.
UPDATE UPDATE UPDATE
Using the query from Aaron Bertrand below, slightly modified, this is what I ended up using, and it was perfect.
SELECT p.*
FROM sys.procedures AS p
LEFT JOIN sys.dm_exec_procedure_stats AS s ON s.[object_id] = p.[object_id]
WHERE s.object_id IS NULL;
Thanks for the hlep.
DMVs will record stats for procedures, but they only possibly go as far back as the last restart (and often not that far, depending on how long a plan lives in cache):
So if your system has only been up for a short time, this is not a reliable measure. The link @Siva points out is useful as well for some other ideas. Unfortunately SQL Server doesn’t really track this for you overall so unless you add tracing or logging you are stuck with the trust you place in the DMV…
EDIT it was a good point, I was solving for the procedures that have run. Instead you may want this:
Or you may want to also include procedures that have run as well, but order them by when they last ran:
This will order first the procedures that haven’t run since a restart, then the rest by when they were executed last, oldest first.