Quickly find a cached plan for Stored Procedure in SQL Server

Hi Geeks,

Today,on the first day of this year 2014 I am sharing useful query often required during performance tuning/Optimization phase of the project.The query makes use of system DMV’s and DMF’s provided by SQL server.Open new query window in SQL Server and fire the below query against the respective database.For this example,I have restricted the output to only 10 rows using the TOP clause and haven’t’ provided any specific name of any stored proc.You can modify it as per your requirement by providing any procedure name whose plan you want to analyze.

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT TOP 10
st.text AS [SQL]
, cp.cacheobjtype
, cp.objtype
, COALESCE(DB_NAME(st.dbid),
DB_NAME(CAST(pa.value AS INT))+’*’,
‘Resource’) AS [DatabaseName]
, cp.usecounts AS [Plan usage]
, qp.query_plan
FROM sys.dm_exec_cached_plans cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) st
CROSS APPLY sys.dm_exec_query_plan(cp.plan_handle) qp
OUTER APPLY sys.dm_exec_plan_attributes(cp.plan_handle) pa
WHERE pa.attribute = ‘dbid’
AND st.text LIKE ‘%CREATE PROCEDURE%’

 

 

Result:

image

I would like to have feedback from my blog readers.

Please post your feedback, question, or comments about this article.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s