简体   繁体   English

提高更新查询加入链接服务器的性能

[英]Increase performance on update query joining Linked Servers

I have 2 linked servers in SQL Server, and would like to update 1 record in the remote DB. 我在SQL Server中有2个链接服务器,并且想更新远程数据库中的1条记录。
Problem is that the query takes very long (6 minutes), and as this is only 1 record, I need to update thousands, so I need to take another approach. 问题是查询需要很长时间(6分钟),并且由于这仅是1条记录,因此我需要更新数千条记录,因此我需要采用另一种方法。 Any ideas or suggestions of something else that I can try? 我可以尝试其他的想法或建议吗?

I have indexes on LocalTable's TaskID, TaskType, and on ClosedOn+Completed+Started and on RemoteTable's TaskID, TaskType and Status. 我在LocalTable的TaskID,TaskType,ClosedOn + Completed + Started和RemoteTable的TaskID,TaskType和Status上都有索引。

This is what I currently have 这是我目前所拥有的

UPDATE RemoteTable
SET RemoteTable.TaskType = LocalTable.TaskType,
    RemoteTable.Status = CASE WHEN IsNull(LocalTable.ClosedOn, 0) <> 0 THEN 'Closed'
                              WHEN IsNull(LocalTable.Completed, 0) <> 0 THEN 'Completed'
                              WHEN IsNull(LocalTable.Started, 0) <> 0 THEN 'Started'
                        ELSE 'Created'
                END,
FROM Tasks LocalTable INNER JOIN [172.1.2.3].DBName.dbo.Tasks RemoteTable
     ON Local.TaskID = RemoteTable.TaskID
where RemoteTable.TaskID = 12345

Execution plan below: The 100% for the Remote table scan is on Task ID for the Remote Table. 下面的执行计划:远程表扫描的100%位于远程表的任务ID上。 在此处输入图片说明

I think it's slow due to the distributed transaction to the linked server. 我认为这很慢,这是因为已分发事务到链接服务器。

Can you write a stored procedure on the remote server and call that stored procedure to do the update? 您可以在远程服务器上编写存储过程并调用该存储过程进行更新吗?

https://blog.sqlauthority.com/2007/10/06/sql-server-executing-remote-stored-procedure-calling-stored-procedure-on-linked-server/ https://blog.sqlauthority.com/2007/10/06/sql-server-executing-remote-stored-procedure-calling-stored-procedure-on-linked-server/

If I remember correctly (old job) INSERTS don't suffer from this issue, so you could load data into a staging table on the remote server and the remote stored procedure can process those 'commands' to do the updates. 如果我没记错(旧工作),INSERTS不会遭受此问题的困扰,那么您可以将数据加载到远程服务器上的登台表中,并且远程存储过程可以处理这些“命令”来进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM