为什么我的计划指南没有被使用?

RK *_*ala 9 performance sql-server execution-plan plan-guides query-performance

我们最近遇到了临界点问题,我们的一些报告查询过去需要在几秒钟内完成执行,现在需要超过 2 分钟,因为查询优化器只是忽略了搜索列上的非聚集索引。下面是一个示例查询:

select top 100 *
from   [dbo].[t_Call]
where  ID > 0 
  and  throwtime between '3/20/2014 7:00:00 AM' and '3/24/2014 6:59:59 AM'
order by id
Run Code Online (Sandbox Code Playgroud)

ID列是聚集索引并且Throwtime具有非聚集索引。在这种情况下,我们注意到 ordering bythrowtime而不是ID更改查询计划和非聚集索引被使用。我们还计划存档一些旧数据(它目前有 2000 万行!!)。但是在应用程序中进行这些更改需要一些时间,我需要找到一种方法来使报告运行得相当快,而无需在应用程序级别进行更改(哦,这就是生活!)。

进入计划指南。我使用非聚集索引查询提示创建了以下计划指南,但出于某种原因,仍未使用非聚集索引。我错过了什么吗?

EXEC sp_create_plan_guide 
@name = N'[prod2reports_callthrowtime]', 
@stmt = N'select top 100 *
          from   [dbo] . [t_Call]
          where  ID > @0 and @1 < = ThrowTime and ThrowTime < = @2 order by ID',
@type = N'SQL', 
@module_or_batch = N'select top 100 *
                     from   [dbo] . [t_Call]
                     where  ID > @0 and @1 < = ThrowTime and ThrowTime < = @2 order by ID', 
@params = N'@0 int, @1 datetime, @2 datetime', 
@hints = N'OPTION (TABLE HINT( [dbo] . [t_Call],
                   INDEX(IDX_NC_t_call_ThrowtimeProblemCodes)))'
GO
Run Code Online (Sandbox Code Playgroud)

Rob*_*ley 11

查询必须完全匹配,包括空格。我建议您从缓存中获取查询,并从中创建它,而不是以任何其他方式输入它。