Skip to content

Instantly share code, notes, and snippets.

@richardbasile
Created February 16, 2018 14:53
Show Gist options
  • Save richardbasile/5a08a48f2ae48475d1727234a527d68c to your computer and use it in GitHub Desktop.
Save richardbasile/5a08a48f2ae48475d1727234a527d68c to your computer and use it in GitHub Desktop.
SQL Server - Plan Guide
DECLARE @planguide nvarchar(max) = 'PlanGuide'
SELECT st.dbid, st.text, qs.*, qp.*
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
cross apply sys.dm_exec_text_query_plan(qs.plan_handle, DEFAULT, DEFAULT) AS qp
WHERE CONVERT(date, last_execution_time) = CONVERT(date, getdate())
AND st.text like '%query text%'
AND st.text not like '%e971a5bc-cd6c-4465-9232-2cb419163112%'
DECLARE @handle varbinary(64)
set @handle = (
SELECT top 1 qs.plan_handle
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
--cross apply sys.dm_exec_text_query_plan(qs.plan_handle, DEFAULT, DEFAULT) AS qp
WHERE CONVERT(date, last_execution_time) = CONVERT(date, getdate())
AND st.text like '%query text%'
AND st.text not like '%e971a5bc-cd6c-4465-9232-2cb419163112%'
)
select @handle
exec sp_create_plan_guide_from_handle @name = @planguide, @plan_handle = @handle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment