Groups | Blog | Home
all groups > sql server new users > october 2007 >

sql server new users : How to: find the last time a procedure was compiled and why



Edmund
10/26/2007 12:00:00 AM
Hello,

Does anyone know how to select the last time a stored procedure was compile
and which recompile condition caused it?

Thanks,

E

Adam Machanic
10/28/2007 12:00:00 AM
I am not aware of any way to query that information from the system catalog.
What you can do if you want to start monitoring recompiles is set up a
server-side trace and watch the SP:Recompile event. You can periodically
insert the trace data into a table, and then you will have your own running
record of what happened. The recompile event populates an EventSubStatus
column that will tell you why the recompile occurred.

For more information see:
http://msdn2.microsoft.com/en-us/library/ms187105.aspx


--

Adam Machanic
SQL Server MVP - http://sqlblog.com

Author, "Expert SQL Server 2005 Development"
http://www.apress.com/book/bookDisplay.html?bID=10220



[quoted text, click to view]
Edmund
10/28/2007 12:00:00 AM

Thanks Adam, I'll give this a try. By the way, if any does know how to
select this information from the system tables, I'm still checking this
thread for an answer.

[quoted text, click to view]

Andrew J. Kelly
10/28/2007 6:36:02 PM
If you are on SQL 2005 have a look at the sys.dm_exec_query_stats DMV. The
Creation_Time should give you what you want.

--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


[quoted text, click to view]
Andrew J. Kelly
10/28/2007 6:50:53 PM
Sorry but I just realized you also wanted the reason for a recompile as
well. That can only be found via a trace as Adam suggested AFAIK. Since the
plan is removed and replaced with a new one each time it recompiles the
previous data is lost.

--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


[quoted text, click to view]
Edmund
10/31/2007 12:00:00 AM
Hi Andrew,

What is the the sys.dm_exec_query_stats DMV? I've looked in the systems
stored procedures in the database but do not see one by that name.

E

[quoted text, click to view]

Andrew J. Kelly
10/31/2007 1:34:21 PM
It is not a stored procedure it is a view. More specifically a Dynamic
Management View or DMV. You can find details in BooksOnLine.

--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


[quoted text, click to view]
Edmund
10/31/2007 4:15:48 PM
Hi Andrew,

I've tried the following but with the database name in place of {db} and so
on. It does not get me anything. Does that mean there is no execution plan
in the database for it. I've run the stored proceedure so there should be
something there.
SELECT *

FROM sys.dm_exec_query_stats

WHERE sql_handle = OBJECT_ID('{db}.{scheme}.{name}')


[quoted text, click to view]

Andrew J. Kelly
10/31/2007 5:23:15 PM
It doesn't work that way, you need to cross apply the sql text like this:

SELECT t.[text] AS [Adhoc Batch or Object Call],
SUBSTRING(t.[text], (qs.[statement_start_offset]/2) + 1,
((CASE qs.[statement_end_offset]
WHEN -1 THEN DATALENGTH(t.[text]) ELSE qs.[statement_end_offset] END
- qs.[statement_start_offset])/2) + 1) AS [Executed Statement]
, qs.*
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS t
ORDER BY [Adhoc Batch or Object Call]

Then just look for or filter on the proc name

--
Andrew J. Kelly SQL MVP
Solid Quality Mentors


[quoted text, click to view]
AddThis Social Bookmark Button