优化 Sql 服务器查询以更新大型列表

Optimizing a Sql Server Query to update large list

我必须更新 table 中的文章(Table 待更新:Articolo),以及另一个 table 中的项目(Table 有新文章:TabellaTemp),现在的问题是我必须更新旧文章 more 我必须插入新文章!为此,我使用下面的查询,第一个更新现有文章,第二个插入新文章,第二个查询真的很慢需要将近 20 分钟才能插入新文章(文章超过 50 万)!有没有办法提高此查询在 Sql 服务器上的性能?

我在这里输入了table的结构http://www.sqlfiddle.com/#!6/4f4ea

查询 1:

Update Articolo set Articolo.Stato = 'Disponibile',Articolo.Prezzo = TabellaTemp.PRZNETTO,Articolo.PrezzoListino = TabellaTemp.PRZCASA,Articolo.DataAggiornamento = TabellaTemp.DATAAGG,Articolo.Descrizione = TabellaTemp.DESCR,Articolo.UM = TabellaTemp.UM
from Articolo
inner join TabellaTemp
on TabellaTemp.CodMarca = Articolo.CodMarca and TabellaTemp.CODART = Articolo.CodArt 
where Articolo.Importato = 'COMET'

查询 2:

Insert into Articolo(CodMarca, CodArt, Descrizione, UM, Prezzo, PrezzoListino, DataAggiornamento, Fornitore, Importato) 
SELECT CODMARCA, CODART, DESCR, UM, PRZNETTO, PRZCASA, DATAAGG,'COMET' as Fornitore,'COMET' as Importato 
FROM TabellaTemp as T 
where CODART not in (select CodArt from Articolo where Importato = 'COMET') and CODMARCA  not in (select CodArt from Articolo where Importato = 'COMET')

将IN语句替换为exists可以带来较大的性能提升

Insert into Articolo(CodMarca, CodArt, Descrizione, UM, Prezzo, PrezzoListino, DataAggiornamento, Fornitore, Importato) 
SELECT 
    CODMARCA, 
    CODART, 
    DESCR, 
    UM, 
    PRZNETTO, 
    PRZCASA, 
    DATAAGG,
    'COMET' as Fornitore,
    'COMET' as Importato 
    FROM TabellaTemp as T 
        WHERE NOT EXISTS
        (
            select 1 from Articolo where Importato = 'COMET' AND (CodArt =T.CodArt OR CodArt = T.CODMARCA)
        )

此外,尝试为 table TabellaTemp 上的列编制索引以提高性能