14

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.

1 Answer 1

13

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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