SQL:使用JOIN子句

时间:2016-01-25 14:22:47

标签: sql sql-update rdbms sql-execution-plan

我正在使用SQL Server 2014,我在下面的查询中使用来自导入表的50.000批次更新了一个非常大的表(500万条记录):

set rowcount 50000
update c
set         
     AddressLine1 = d.AddressLine1      
    ,Postcode = d.Postcode
    ,FirstName = d.FORENAME
    ,LastName = d.SURNAME
    ,TelephoneNumber = d.TelephoneNumber
    ,EmailAddress = d.EMAIL_ADDRESS
    ,DataExpiryDate = getdate()
    ,DateCreated = getdate()        
    ,DateModified = getdate()   
FROM 
Customer c
inner join 
[Import_Table] d        
on  
c.AccountNumber = d.AccountNumber 
AND 
c.Id = d.Id
where 
c.DateModified < '2016-01-25 12:00:00 AM' 
set rowcount 0

每批大约花费9分钟。

有没有办法改进上面的查询?

我为:( AccountNumber,Id)和(Id,AccountNumber)添加了唯一索引。

更新:我添加了旧计划(在索引中包含DateModified列之前)以及新计划(见下文)。

谢谢, 甲

Initial query plan

New Plan

1 个答案:

答案 0 :(得分:1)

转储GETDATE()函数;它正在为每一次更新执行。做一次并传递价值。

DECLARE @updateDT DATETIME

SET @updateDT=GETDATE()

set rowcount 50000
update c
set         
     AddressLine1 = d.AddressLine1      
    ,Postcode = d.Postcode
    ,FirstName = d.FORENAME
    ,LastName = d.SURNAME
    ,TelephoneNumber = d.TelephoneNumber
    ,EmailAddress = d.EMAIL_ADDRESS
    ,DataExpiryDate = @updateDT
    ,DateCreated = @updateDT        
    ,DateModified = @updateDT   
FROM 
Customer c
inner join 
[Import_Table] d        
on  
c.AccountNumber = d.AccountNumber 
AND 
c.Id = d.Id
where 
c.DateModified < '2016-01-25 12:00:00 AM' 
set rowcount 0