繁体   English   中英

来自不同SQL Server中同一查询的不同执行计划

[英]Different execution plan from the same query in different SQL Server

我对此查询有一些疑问:

select distinct
    Date_Int,
    CodF,
    Desc_Com,
    DataDesc_Com,
    CodC,
    Function,
    Tratt_Number,
    Tratt_State
from 
    tmp_SIC_Trattative_Stato_com_l2

UNION 

SELECT DISTINCT
    case 
       when (ts.Date_Int is not null) 
          then ts.Date_Int 
          else All_Day.Date_Int 
    end as Date_Int,
    case 
       when (ts.CodF is not null) 
          then ts.CodF 
          else All_Day.CodF  
    end as CodF,
    case 
       when (ts.Desc_Com is not null) 
          then ts.Desc_Com 
          else All_Day.Desc_Com 
    end as Desc_Com,
    case 
       when (ts.DataDesc_Com is not null) 
          then ts.DataDesc_Com 
          else All_Day.DataDesc_Com 
    end as DataDesc_Com,
    case 
       when (ts.CodC is not null) 
          then ts.CodC 
          else All_Day.CodC 
    end as CodC,
    case when (ts.Function is not null) then ts.Function else All_Day.Function end as Function,
    case when (ts.Tratt_Number is not null) then ts.Tratt_Number else All_Day.Tratt_Number end as Tratt_Number,
    case when (ts.Tratt_State is not null) then ts.Tratt_State else All_Day.Tratt_State end as Tratt_State
FROM 
    Commerciali_All_Day as All_Day 
LEFT OUTER JOIN
    tmp_SIC_Trattative_Stato_com_l2 as ts ON ts.Date_Int = All_Day.Date_Int
                                          AND ts.CodF = All_Day.CodF
                                          AND ts.Desc_Com = All_Day.Desc_Com
                                          AND ts.DataDesc_Com = All_Day.DataDesc_Com
                                          AND ts.CodC = All_Day.CodC
                                          AND ts.Function = All_Day.Function
                                          AND ts.Tratt_State = All_Day.Tratt_State
WHERE 
    ts.Date_Int IS NULL

我在存储过程中执行此查询,但是如果使用生产SQL Server或测试SQL Server执行存储过程,则执行计划会更改。

这是测试执行计划:

测试执行计划

这是生产执行计划:

生产执行计划

源表和存储过程在“测试和生产”中是相同的,并且我不理解,因为执行计划和时间不同。

在“测试”中,查询将在6分钟内执行,在生产中将在15分钟内执行。

测试和生产SQL Server是Microsoft SQL Server 2014版本12.0.4100.1。

  • 生产服务器具有24 GB RAM和8 CPU 2GHz
  • 测试服务器具有16 GB RAM和4 CPU 2GHz

我不明白为什么该过程在测试环境中而不在生产环境中表现更好。

问题的标题不是您真正要的。 产品和测试服务器之间的查询计划相同。 您真正要问的是,为什么具有相同查询的prod服务器比测试服务器要慢。

在评论中,您回答说测试和生产之间的表及其内容是相同的。 具体来说,您提到它们具有相同的行数。

生产计划显示的返回数据比测试计划更多。 返回数据的最大兴趣是在Commerciali_All_Day上进行的表扫描,这是您对哈希表的构建输入。 在测试中,它返回725,858行,总大小为47MB。 在生产中,它返回728,941行,总大小为120MB。 那就是大小的两倍多,相差3,083行。

由于散列生成输入表返回的数据量增加了一倍以上,因此它在prod中比在测试中要大得多。 在测试中,哈希表为19,897,066行,大小为2,713MB。 在产品中,哈希表是20,006,362行,大小为4,732MB。 Prod正在处理额外的2GB数据。

您需要回过头来更好地了解生产和测试中的数据之间的区别。 比较生产计划和测试计划时,您的表都没有返回相同数量的数据。 特别是,此查询的真正痛点是Commerciali_All_Day表。

您对问题标题的评论正确。 最初,生产和测试执行计划也有所不同,但是明确了左外部联接,我获得了相同的执行计划,但是执行时间不同。

生产和测试之间的行号有所不同,因为测试的数据已更新到一天之前,因此没有3,083行。

我检查了在生产环境中处理的额外数据之间的差异,并发现了一个临时表,其中有2个字段为varchar(max),在测试环境中为varchar(25)。 我更改了这两个字段的类型,现在执行时间和处理后的数据相同。

非常感谢你的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM