Technical Article

fetch an execution plan from SQL Server's buffer cache

,

To fetch an execution plan from SQL Server's buffer cache (i.e., the in-memory cache of execution plans), you can query the dynamic management views (DMVs) that SQL Server exposes. Here's how to do it:


βœ… Fetch Cached Execution Plans from SQL Server

You can use the following T-SQL query to view cached execution plans stored in memory:

πŸ” Key Notes:

  • sys.dm_exec_query_stats: Contains aggregate performance statistics.

  • sys.dm_exec_sql_text: Returns the SQL text for a batch.

  • sys.dm_exec_query_plan: Returns the actual query plan XML.

  • This returns cached plans, not "actual runtime plans" unless the plan includes runtime info (like in Query Store or with SET STATISTICS XML).

SELECT
qs.sql_handle,
qs.plan_handle,
qs.execution_count,
qs.total_worker_time,
qs.total_elapsed_time,
qs.creation_time,
qs.last_execution_time,
st.text AS query_text,
qp.query_plan
FROM
sys.dm_exec_query_stats qs
CROSS APPLY
sys.dm_exec_sql_text(qs.sql_handle) st
CROSS APPLY
sys.dm_exec_query_plan(qs.plan_handle) qp
ORDER BY
qs.total_elapsed_time DESC;

Rate

β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜…

You rated this post out of 5. Change rating

Share

Share

Rate

β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜… β˜…

You rated this post out of 5. Change rating