Skip to content

Instantly share code, notes, and snippets.

@mdickin
Last active December 16, 2019 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdickin/f4f6744774e650bf4c3aae2d313bd71a to your computer and use it in GitHub Desktop.
Save mdickin/f4f6744774e650bf4c3aae2d313bd71a to your computer and use it in GitHub Desktop.
Finding and clearing SQL execution plans
SELECT cp.plan_handle, cp.objtype, cp.usecounts,
DB_NAME(st.dbid) AS [DatabaseName], st.text, cp.refcounts--, qp.query_plan
FROM sys.dm_exec_cached_plans AS cp
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st
CROSS APPLY sys.dm_exec_query_plan(plan_handle) qp
where objtype = 'Prepared' and
cp.usecounts > 4 and
st.text LIKE N'%some text in the query you want to search for%' OPTION (RECOMPILE);
-- For SQL Server
DBCC FREEPROCCACHE (some plan_handle you want to clear)
-- For Azure
ALTER DATABASE SCOPED CONFIGURATION CLEAR PROCEDURE_CACHE plan_handle_you_want_to_clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment