Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 不同参数值的不同执行计划_Sql Server_Sql Server 2008 - Fatal编程技术网

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

Sql server 不同参数值的不同执行计划,sql-server,sql-server-2008,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') SEL

查询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') 
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') 
查询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/12/2014 02:30:00.000 PM') 
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') 
问题:

  • Query2正在尝试获取Query1的子集数据,因此Query2应该更快
  • 这两个查询只因参数值不同而不同,因此为什么执行计划完全不同呢
  • 关于服务器的信息:这是在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的索引 非群集开启(活动,日期)

    表2的索引 包括[Active]上的非群集(列,列1ID) 聚集在(Col,Col1ID)


    欢迎对此提供任何帮助。

    尝试将包含的列添加到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);
    

    你的桌子结构会很有帮助。还有你的索引。我刚刚更新了问题,Query1并行运行,Query2串行运行。如果需要静止表结构,请告诉我。是的。当然,帕特里克,我会在几分钟内更新这个问题。我不能马上做,因为我不能透露确切的表名和列名。公司政策:(。