Cameron Castillo
Cameron Castillo

Reputation: 2842

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.
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. 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.

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. enter image description here

Upvotes: 0

Views: 369

Answers (1)

TDP
TDP

Reputation: 1231

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/

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.

Upvotes: 1

Related Questions