简单的更改会导致SQL查询执行时间大幅增加

时间:2015-07-10 16:37:29

标签: sql sql-server tsql execution-time sql-execution-plan

我在我的Microsoft SQL Server(2012 Express)数据库上运行以下SQL查询,它工作正常,在不到一秒的时间内执行:

SELECT
  StringValue, COUNT(StringValue)
FROM Attributes
WHERE
  Name = 'Windows OS Version'
  AND StringValue IS NOT NULL
  AND ProductAssociation IN (
    SELECT ID
    FROM ProductAssociations
    WHERE ProductCode = 'MyProductCode'
  )
GROUP BY StringValue

我在内部查询中添加了一个过滤器,它继续正常工作,返回的结果略少(如预期的那样),并且还在不到一秒的时间内执行。

SELECT
  StringValue, COUNT(StringValue)
FROM Attributes
WHERE
  Name = 'Windows OS Version'
  AND StringValue IS NOT NULL
  AND ProductAssociation IN (
    SELECT ID
    FROM ProductAssociations
    WHERE ProductCode = 'MyProductCode'
    AND ID IN (
      SELECT A2.ProductAssociation
      FROM Attributes A2
      WHERE A2.Name = 'Is test' AND A2.BooleanValue = 0
    )
  )
GROUP BY StringValue

但是当我添加一个标志变量以使我能够“打开/关闭”内部查询中的过滤器,并将标志设置为零时,查询似乎无限期地执行(我离开了)它运行大约5分钟,然后强制取消):

DECLARE @IsTestsIncluded bit
SET @IsTestsIncluded = 0

SELECT
  StringValue, COUNT(StringValue)
FROM Attributes
WHERE
  Name = 'Windows OS Version'
  AND StringValue IS NOT NULL
  AND ProductAssociation IN (
    SELECT ID
    FROM ProductAssociations
    WHERE ProductCode = 'MyProductCode'
    AND (
      @IsTestsIncluded = 1
      OR
      ID IN (
        SELECT A2.ProductAssociation
        FROM Attributes A2
        WHERE A2.Name = 'Is test' AND A2.BooleanValue = 0
      )
    )
  )
GROUP BY StringValue

为什么呢?我做错了什么?我发誓我过去使用过这种模式没有问题。

(当我在上面的最终查询中设置@IsTestsIncluded = 1时,将跳过过滤器并且执行时间正常 - 延迟仅发生在@IsTestsIncluded = 0

修改

根据Joel在评论中的请求,这是第一个查询的执行计划:

Execution plan for first query

这是第二个查询的执行计划:

enter image description here

(我不能发布第三个查询的执行计划,因为它永远不会完成 - 除非有其他方法可以在SSMS中获取它吗?)

3 个答案:

答案 0 :(得分:5)

  

为什么呢?我做错了什么?

您正在尝试编译一个需要根据变量满足多个不同条件的查询。优化器必须提供一个计划,该计划适用于两个情况。

尽量避免这种瘟疫。只需发出两个查询,一个用于一个条件,一个用于另一个,这样优化器可以单独优化每个查询,并编译一个最适合每种情况的执行计划。

对该主题的冗长讨论,以及替代方案和利弊:Dynamic Search Conditions in T‑SQL

答案 1 :(得分:3)

试试这个:

SELECT
  a.StringValue, COUNT(a.StringValue)
FROM Attributes a
INNER JOIN ProductAssociations p ON a.ProductAssociation = p.ID
    AND p.ProductCode = 'MyProductCode'
LEFT JOIN Attributes a2 ON a2.ProductAssociation = p.ID
    AND a2.Name = 'Is Test' AND a2.BooleanValue = 0       
WHERE
  Name = 'Windows OS Version'
  AND StringValue IS NOT NULL
  AND COALESCE(a2.ProductAssociation, NULLIF(@IsTestsIncluded, 1)) IS NOT NULL
GROUP BY a.StringValue

coalesce/nullif组合并不是我写过的最容易理解的内容,但只要连接条件匹配0或1条记录,它就应该在功能上等同于你所拥有的组合。加入了表格。

答案 2 :(得分:-1)

Joel +1的好答案

OR难以优化

回到第二个 优化者难以优化的地方 考虑加入所有那些在中的人 这仍然具有可能导致错误查询计划的OR,但它为优化器提供了更小的机会来最小化OR

SELECT A1.StringValue, COUNT(A1.StringValue)
 FROM Attributes A1
 JOIN ProductAssociations PA
   ON PA.ID = A1.ProductAssociation
  AND A1.Name = 'Windows OS Version'
  AND A1.StringValue IS NOT NULL
  AND PA.ProductCode = 'MyProductCode'
 JOIN Attributes A2 
   ON A2.ProductAssociation = A1.ProductAssociation 
  AND (     @IsTestsIncluded = 1
        OR (A2.Name = 'Is test' AND A2.BooleanValue = 0)
      )
GROUP BY A1.StringValue  

如果你重构@IsTestsIncluded,你可以这样做

SELECT A1.StringValue, COUNT(A1.StringValue)
 FROM Attributes A1
 JOIN ProductAssociations PA
   ON PA.ID = A1.ProductAssociation
  AND A1.Name = 'Windows OS Version'
  AND A1.StringValue IS NOT NULL
  AND PA.ProductCode = 'MyProductCode'
 LEFT JOIN Attributes A2 
   ON A2.ProductAssociation = A1.ProductAssociation 
  AND A2.Name = 'Is test' 
  AND A2.BooleanValue = 0
WHERE ISNULL(@IsTestsIncluded, A2.ProductAssociation) is NOT NULL
GROUP BY A1.StringValue