SQL可执行文件非常慢如何加速?

时间:2018-12-12 14:52:31

标签: sql sql-server query-optimization

(SQL Server)此代码非常慢,我如何加快速度?

update g set g.Ebat=mtib.icerik_anlami  from #gecici g
left outer join database.dbo.table2 mtib on mtib.malzeme_kodu=g.malzeme_kodu
where mtib.alan_sabit_adi='ebat' and g.Ebat=mtib.icerik

update ga set ga.zemin_rengi=mtib.sira_no  from #gecici ga
left outer join database.dbo.table2 mtib on mtib.malzeme_kodu=ga.malzeme_kodu
where mtib.alan_sabit_adi='ZeminRengi' and ga.zemin_rengi=mtib.icerik

update gaa set gaa.bordur_rengi=mtib.sira_no  from #gecici gaa
left outer join database.dbo.table2 mtib on mtib.malzeme_kodu=gaa.malzeme_kodu

2 个答案:

答案 0 :(得分:1)

您无需出于目的使用LEFT JOIN。尝试INNER JOIN。同样,您不需要在所有与联接表相关的过滤器中使用。

update  g 
set     g.Ebat=mtib.icerik_anlami  
from    #gecici g
inner join database.dbo.table2 mtib on mtib.malzeme_kodu=g.malzeme_kodu
    and mtib.alan_sabit_adi='ebat' 
    and g.Ebat=mtib.icerik

答案 1 :(得分:0)

我首先将其编写为单个查询:

update g
    set Ebat = (case when mtib.alan_sabit_adi = 'ebat' and g.Ebat = mtib.icerikmtib.icerik_anlami
                     then mtib.icerik_anlami else g.Ebat
                end),
         zemin_rengi = (case when mtib.alan_sabit_adi = 'ZeminRengi' and ga.zemin_rengi = mtib.icerik
                             then mtib.sira_no else g.zemin_rengi
                        end),
         bordur_rengi = mtib.sira_no
    from #gecici g join
         database.dbo.table2 mtib
         on mtib.malzeme_kodu = g.malzeme_kodu;

合并查询可防止多次更新同一行。

inner joinouter join的区别仅在于第三个查询,因为这将更新所有行。我猜那不是您想要的。

然后,您要在database.dbo.table2(malzeme_kodu)上建立索引。您可以在索引中的查询中包含所有其他列。