Skip to content

Instantly share code, notes, and snippets.

@alex-mtx
Last active January 27, 2017 07:05
Show Gist options
  • Save alex-mtx/e0afd9e10170fd556c4e761f9c93a15f to your computer and use it in GitHub Desktop.
Save alex-mtx/e0afd9e10170fd556c4e761f9c93a15f to your computer and use it in GitHub Desktop.
This query shows the plan cache for a given procedure in a given database
-- original source: http://www.sommarskog.se/query-plan-mysteries.html
-- replace object_id and db_id as needed
-- Reminder: You need the server-level permission VIEW SERVER STATE to run queries against the plan cache
SELECT qs.plan_handle, a.attrlist
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) est
CROSS APPLY (SELECT epa.attribute + '=' + convert(nvarchar(127), epa.value) + ' '
FROM sys.dm_exec_plan_attributes(qs.plan_handle) epa
WHERE epa.is_cache_key = 1
ORDER BY epa.attribute
FOR XML PATH('')) AS a(attrlist)
WHERE est.objectid = object_id ('dbo.List_orders_6')
AND est.dbid = db_id('Northwind')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment