Take the 2-minute tour ×
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. It's 100% free, no registration required.

We recently upgraded an application we use, which involved modifying the schema for the database. These changes could have forced cached execution plans to be discarded. If SQL Server was forced to create a bunch of new plans, this could have slowed down the user experience. I'd like to find out if this was the case.

So, my question is, does SQL Server 2008 store the creation date of cached execution plans? The management view sys.dm_exec_cached_plans doesn't have any date fields, so I suspect not.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

It isn't stored in sys.dm_exec_cached_plans, nor is it buried anywhere in the plan XML that I can find. There is useful information in other DMVs however.

For stored procedures we can get the time a plan was cached from sys.dm_exec_procedure_stats:

SELECT TOP(250) 
    p.name AS [SP Name]
    , ps.execution_count
    , ps.cached_time
FROM 
    sys.procedures p WITH (NOLOCK)
INNER JOIN 
    sys.dm_exec_procedure_stats ps WITH (NOLOCK)
ON  p.[object_id] = ps.[object_id]
WHERE 
    ps.database_id = DB_ID()
ORDER BY 
    ps.cached_time DESC 
OPTION 
    (RECOMPILE);

For ad-hoc queries, there is the creation time in sys.dm_exec_query_stats:

SELECT TOP(250) 
    st.[text] AS [QueryText]
    , qs.execution_count
    , qs.creation_time
FROM 
    sys.dm_exec_cached_plans cp WITH (NOLOCK)
INNER JOIN
    sys.dm_exec_query_stats qs WITH (NOLOCK)
ON  qs.plan_handle = cp.plan_handle
CROSS APPLY 
    sys.dm_exec_sql_text(cp.plan_handle) st
WHERE 
    cp.objtype = N'Adhoc' 
ORDER BY 
    qs.creation_time DESC 
OPTION 
    (RECOMPILE);

According to @SqlKiwi... the change from creation_time to cached_time was just because procedures and triggers were added in 2008 and the opportunity was taken to choose a more descriptive name. The created/cached time reflects the last compilation, not the creation time of the original plan.

share|improve this answer

Your Answer

 
discard

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.