有没有办法加速这个SQL?我正在使用链接服务器。我尝试合并,性能相同。
- 通过recordModified Server 1更新参与者 - >服务器2
update p1
set p1.[RecordCreated] = p2.RecordCreated
,p1.[RecordModified] = p2.RecordModified
,p1.[Email] = p2.Email
from ERSS_Local.dbo.Participant p1
join [Computer2\SqlExpress].ERSS_Local.dbo.Participant p2
on p2.RecordModified >= (select LastSync from ERSS_DB_Sync)
and p1.ParticipantID = p2.ParticipantID
and p1.RecordCreated = p2.RecordCreated and p1.RecordModified < p2.RecordModified
我正在更新超过35列,我将它们留下来简化查询。
我添加了这个并且它似乎没有帮助
on p2.RecordModified >= (select LastSync from ERSS_DB_Sync)
瓶颈似乎是我正在更新的列数。
是否有加快此更新声明的速度?
答案 0 :(得分:0)
本地Sql Server对远程/链接的Sql Server上的统计信息,记录数或索引一无所知。因此,它需要从远程服务器带来所有记录甚至对RecordModified
列进行检查。
您可以通过使用`OPENQUERY()来改进事物,让链接的远程服务器在将结果发送到本地服务器之前预先过滤结果。这将限制网络流量只是相关记录,并允许远程服务器在构建结果集时使用它的索引:
update p1
set p1.[RecordCreated] = p2.RecordCreated
,p1.[RecordModified] = p2.RecordModified
,p1.[Email] = p2.Email
from ERSS_Local.dbo.Participant p1
join OPENQUERY([Computer2\SqlExpress],
'SELECT * FROM ERSS_Local.dbo.Participant WHERE p2.RecordModified >= ''' + (select LastSync from ERSS_DB_Sync) + '''') p2
on p1.ParticipantID = p2.ParticipantID
and p1.RecordCreated = p2.RecordCreated
and p1.RecordModified < p2.RecordModified
如果您还可以通过将*
更改为您需要的内容来限制返回的列,那就更好了。
答案 1 :(得分:0)
--assign a variable prevents some issues with query optimizer
DECLARE @LastSync DATETIME;
SELECT @LastSync = LastSync from ERSS_DB_Sync
update
p1
set
p1.[RecordCreated] = p2.RecordCreated
,p1.[RecordModified] = p2.RecordModified
,p1.[Email] = p2.Email
from
ERSS_Local.dbo.Participant p1
join (
SELECT
[Computer2\SqlExpress].ERSS_Local.dbo.Participant
WHERE
RecordModified >= @LastSync
-- force the destination engine to filter this set first
) as p2
on p1.ParticipantID = p2.ParticipantID
and p1.RecordCreated = p2.RecordCreated and p1.RecordModified < p2.RecordModified