不同参数值的不同执行计划

时间:2014-06-20 12:23:42

标签: sql-server sql-server-2008

查询1快速运行并使用并行执行计划

SELECT c.[Date]
FROM Table1 c
left join Table2 bec on bec.Col1ID = c.Col1ID and bec.Active = 1
WHERE c.Active = 1
AND (c.Col2ID not in (3,4,5,6,7,8,9,10) or c.Col2ID is null) 
and (c.[Date] >= '06/12/2014 02:30:00.000 PM') 

查询2需要更长时间并使用正常(串行)执行计划

SELECT c.[Date]
FROM Table1 c
left join Table2 bec on bec.Col1ID = c.Col1ID and bec.Active = 1
WHERE c.Active = 1
AND (c.Col2ID not in (3,4,5,6,7,8,9,10) or c.Col2ID is null) 
and (c.[Date] >= '06/15/2014 02:30:00.000 PM') 

问题:

  1. Query2正在尝试获取Query1的子集数据,因此Query2应该更快。
  2. 两个查询的区别仅在于参数值,所以为什么完全不同的执行计划。
  3. 有关服务器的信息:这是在SQL Server 2008中运行

    表结构如下:

    TABLE Table1(
           Col1Id [int] IDENTITY(1,1) NOT NULL,
           Col2Id [int] NULL,
           Col3 [int] NOT NULL,
           Col4 [int] NULL,
           Active [bit] NOT NULL
           [Date] [datetime] NOT NULL)
    

    表1中的索引 Non Clustered on(Active,Date)

    TABLE Table2(
           [Col] [int] NOT NULL,
           Col1ID [int] NOT NULL,
           [Col2] [int] NOT NULL,
           [Col3] [datetime] NOT NULL,
           [Col4] [int] NOT NULL,
           [Col5] [datetime] NULL,
           [Col6] [int] NULL,
           [Active] [bit] NULL)
    

    表2中的索引 包含[有效]的非群集(Col,Col1Id) 聚集在(Col,Col1ID)

    欢迎提供任何帮助。

1 个答案:

答案 0 :(得分:1)

尝试将包含的列添加到Table1索引并在Table2上创建新索引。此外,请确保统计信息是最新的。

CREATE INDEX idx_Table1_Active_Date ON dbo.Table1 (Active, Date) INCLUDE (Col1Id, Col2Id);
CREATE INDEX idx_Table2_Col1ID_Active ON dbo.Table2 (Col1ID, Active);