Demo: Create a custom sql plan guide with maxdop 1 hint
create table dbo.PlanGuideTest
( Id int identity(1,1) primary key,
Data varchar(100) not null)
insert dbo.PlanGuideTest (data) values ('a'),('b'),('c'),('d')
/* run this a couple of times
and verify in execution plan that QueryPlan DegreeOfParallelism="" is greater than 1 (this will not be the case if your server is not set up for paralellism)*/
exec sp_executesql N'select * from PlanGuideTest where Id between @p0 and @p1',N'@p0 int,@p1 int',2,4
/* check which plan is used, remember that your original query might be altered before stored in cache, so check usecounts column. */
select st.text, cp.objtype, cp.size_in_bytes, cp.plan_handle,cp.usecounts
from sys.dm_exec_cached_plans AS cp
cross apply sys.dm_exec_sql_text(cp.plan_handle) st
where cp.cacheobjtype = N'Compiled Plan'
and cp.objtype IN(N'Adhoc', N'Prepared')
and text like '%select%'
and text like '%PlanGuideTest%'
option (recompile);
/* copy the text column exactly as it is to statement variable, extract parameters (inside ()) into @params. for this example the copied value was
(@p0 int,@p1 int)select * from PlanGuideTest where Id between @p0 and @p1 */
declare @statement nvarchar(max)='select * from PlanGuideTest where Id between @p0 and @p1'
,@params nvarchar(max)='@p0 int,@p1 int';exec sp_create_plan_guide N'MaxDop1GuideForTestQuery', @statement, N'SQL', null, @params, N'OPTION(MAXDOP 1)';
/* find plan */
select * from sys.plan_guides where name='MaxDop1GuideForTestQuery'
/* now run your query again and verify in execution plan thatNote that some guides says that properties window will show plan guides but this is not working in management studio 2014 */
exec sp_executesql N'select * from PlanGuideTest where Id between @p0 and @p1',N'@p0 int,@p1 int',2,4
/* clean up */
exec sp_control_plan_guide 'drop','MaxDop1GuideForTestQuery'drop table PlanGuideTest
( Id int identity(1,1) primary key,
Data varchar(100) not null)
insert dbo.PlanGuideTest (data) values ('a'),('b'),('c'),('d')
/* run this a couple of times
and verify in execution plan that QueryPlan DegreeOfParallelism="" is greater than 1 (this will not be the case if your server is not set up for paralellism)*/
exec sp_executesql N'select * from PlanGuideTest where Id between @p0 and @p1',N'@p0 int,@p1 int',2,4
/* check which plan is used, remember that your original query might be altered before stored in cache, so check usecounts column. */
select st.text, cp.objtype, cp.size_in_bytes, cp.plan_handle,cp.usecounts
from sys.dm_exec_cached_plans AS cp
cross apply sys.dm_exec_sql_text(cp.plan_handle) st
where cp.cacheobjtype = N'Compiled Plan'
and cp.objtype IN(N'Adhoc', N'Prepared')
and text like '%select%'
and text like '%PlanGuideTest%'
option (recompile);
/* copy the text column exactly as it is to statement variable, extract parameters (inside ()) into @params. for this example the copied value was
(@p0 int,@p1 int)select * from PlanGuideTest where Id between @p0 and @p1 */
declare @statement nvarchar(max)='select * from PlanGuideTest where Id between @p0 and @p1'
,@params nvarchar(max)='@p0 int,@p1 int';exec sp_create_plan_guide N'MaxDop1GuideForTestQuery', @statement, N'SQL', null, @params, N'OPTION(MAXDOP 1)';
/* find plan */
select * from sys.plan_guides where name='MaxDop1GuideForTestQuery'
/* now run your query again and verify in execution plan that
exec sp_executesql N'select * from PlanGuideTest where Id between @p0 and @p1',N'@p0 int,@p1 int',2,4
/* clean up */
exec sp_control_plan_guide 'drop','MaxDop1GuideForTestQuery'drop table PlanGuideTest
Comments