Find Recently Executed Stored Procedures
This past weekend, we had an issue where replication fell far behind on one of our databases. The replicated database is used for all sorts of reporting, so the immediate need was to identify processes that may have been affected by the incomplete data.
Now, there's hundreds of stored procedures that reference the affected database; the trick is finding out which ones are relevant. To do this, I used the sys.dm_exec_query_stats DMV. This does two things for me. One, it shows me a list of stored procedures in cache, meaning they've been executed relatively recently and are probably relevant to the search. Secondly, it shows me the last execution time, which in some cases may have been before the issue, meaning I do not need to worry about re-running those processes.
Here's the query I used:
Select DB_Name(dest.[dbid]) As 'databaseName' , Object_Name(dest.objectid, dest.[dbid]) As 'procName' , Max(deqs.last_execution_time) As 'last_execution' From sys.dm_exec_query_stats As deqs Cross Apply sys.dm_exec_sql_text(deqs.sql_handle) As dest Where dest.[text] Like '%yourTableName%' -- replace And dest.[dbid] Is Not Null -- exclude ad-hocs Group By db_name(dest.[dbid]) , Object_Name(dest.objectid, dest.[dbid]) Order By databaseName , procName Option (MaxDop 1);
This will return results similar to:
databaseName procName last_execution -------------------- ------------------------------ ----------------------- AdventureWorks ufnGetProductListPrice 2009-08-03 09:57:25.390 AdventureWorksDW DimProductCategoryGet_sp 2009-08-03 09:59:05.820 AdventureWorksDW DimProductGet_sp 2009-08-03 09:58:38.370
I want to stress that this is *not* a list of all referencing objects, but rather a list of recently executed stored procedures that are still in memory. This list may not be accurate if your cache has recently been flushed or if you've recently rebooted your server.
Categories
- Business Intelligence
- Internals
- Miscellaneous
- PASS
- Performance & Tuning
- Presentations
- SQL 2008
- SQL Tips
- Syndication
- T-SQL Scripts
Subscribe to my blog!
| Like what you see? Subscribe! |
![]() |
Around the Web
Recent Tweets
- RT @Phil_Factor: SQL Server table columns under the hood http://t.co/vp7gQ77B < what a great post
- @tradney haha that's awesome :)
- OH: @AdamMachanic's #sqlpass session was one of the best I've ever seen.
Archives
- November 2011
- October 2011
- September 2011
- July 2011
- June 2011
- May 2011
- April 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- July 2010
- June 2010
- May 2010
- April 2010
- February 2010
- January 2010
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008



August 4th, 2009 - 00:15
Hello,
I can’t make this script work on sql server 2005.
The following error appears (I translate it from french) :
Msg 174, Level 15, State 1, Line 2
La fonction object_name requiert 1 argument(s).
=>
Msg 174, Level 15, State 1, Line 2
object_name function require 1 argument(s).
Do you know what is this ?
Thanks by advance,
August 4th, 2009 - 06:00
@Matthieu You need to be on SQL Server 2005 SP2 or higher. In prior versions of SQL Server, the OBJECT_NAME function only accepts a parameter for object_id. You can read more about this here: http://msdn.microsoft.com/en-us/library/ms186301(SQL.90).aspx
To make this work for you, you could try the following query:
then execute it for each database.
August 4th, 2009 - 10:04
Pretty good article! Gotta love what you can do with the DMV’s
August 5th, 2009 - 00:19
Thanks for your answer.
It’s working fine. I just missed something at the beginning. I was running the query outside the MASTER DB. Oups.
Thx
August 10th, 2009 - 06:41
Do you know how long the procedure calls remain in cache, approximately? Like, are we talking a few hours, or a few days, or a few weeks?
August 18th, 2009 - 18:08
@Kiran I can’t really give you a good answer. It really depends on your server, i.e. how much activity is going on, how much available memory you have, etc. If I had to take a wild guess, I would say you’re looking at anywhere from 1 to 5 days, depending on the specifics of your environment.