【发布时间】:2016-07-28 12:25:22
【问题描述】:
我对这个查询有一些疑问:
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
我不明白为什么该程序在测试环境中执行得更好,而不是在生产环境中。
【问题讨论】:
-
方案其实是一样的,费用是不一样的。如果您可以将计划提供为 XML,您可以获得更好的答案。具体来说,当您将鼠标悬停在排序和哈希匹配/连接上时,它将包括字段(可能相同)以及所涉及的记录数。
-
当
UNION时不需要做SELECT DISTINCT,因为UNION会删除所有重复项。 (UNION实际上是UNION DISTINCT的缩写。) -
Test 和 Production 中的表格内容是否相同?表中的行数会影响计划。
-
执行计划可能因服务器而异。这是数据库引擎针对给定查询(如果之前未缓存)的决定,以利用可用资源获得最佳性能。因此,如果资源不同,那么我们可以预期执行计划会有所不同。或者对于存储过程,如果参数集变化很大,那么对于第一次执行,缓存的执行计划最多不会执行其他参数集。
-
test和producton表是一样的:都是用同一个sql创建表,记录数也一样:Commerciali_All_Day是728.501行,tmp_SIC_Trattative_Stato_com_l2是22.539.465行
标签: sql sql-server stored-procedures query-optimization sql-server-2014