SQL Server 2008 R2 - 看似冲突的连接导致查询速度非常慢

时间:2011-12-01 05:44:40

标签: sql-server sql-server-2008-r2

此查询需要16秒才能运行

SELECT 
    WO.orderid
FROM 
    WebOrder as WO
    INNER JOIN Addresses AS A ON WO.AddressID = A.AddressID
    LEFT JOIN SalesOrders as SO on SO.SO_Number = WO.SalesOrderID   

如果我注释掉任何一个连接,它会在很短的时间内运行。例如:

SELECT 
    WO.orderid
FROM 
    WebOrder as WO
    INNER JOIN Addresses AS A ON WO.AddressID = A.AddressID
    -- LEFT JOIN SalesOrders as SO on SO.SO_Number = WO.SalesOrderID    

SELECT 
    WO.orderid
FROM 
    WebOrder as WO
    -- INNER JOIN Addresses AS A ON WO.AddressID = A.AddressID
    LEFT JOIN SalesOrders as SO on SO.SO_Number = WO.SalesOrderID

备注

  • SalesOrders和Adddresses表中各有大约40,000条记录。
  • 我在ON子句中使用的所有字段都有索引或PKeys。

慢版本的执行计划(SalesOrders Join已注释掉)

enter image description here

快速版的执行计划

enter image description here



为什么这些连接在一起使用时会导致从0.01秒到16秒?

1 个答案:

答案 0 :(得分:2)

您的执行计划没有显示任何昂贵的操作,我会尝试关注以解决性能不佳问题:

  • 重建索引
  • 更新统计信息
  • DBCC FREEPROCCACHE

我个人不希望后者做任何事情 - 看起来你有一个合理的查询计划。