Question

the code below works fine but it takes an absolute age to run. How can I speed this up?

Background: I have linked my local server to my remote server. From my local server, I need to insert data from the remote server into my local server and then update the table on my remote server. See snippet for the exact details.

DECLARE @temp1 TABLE
(LoginID INT PRIMARY KEY, 
UserID UNIQUEIDENTIFIER, 
Message NCHAR(1000))

INSERT INTO [My Local Server].[DB Name].dbo.Login 
(LoginID, UserID, Message)
OUTPUT INSERTED.LoginID, INSERTED.UserID, INSERTED.Message INTO @temp1
SELECT LoginID, UserID, Message
FROM [Remote Server].[Remote DB Name].dbo.Login2 
WHERE Date IS NULL

UPDATE [Remote Server].[Remote DB Name].dbo.Login2 
SET Date = GETDATE()
FROM [Remote Server].[Remote DB Name].dbo.Login2 AS z
WHERE EXISTS (SELECT 1 FROM @temp1 AS x WHERE z.Date IS NULL AND x.LoginID = z.LoginID)

EDIT:

In addition, is there anyway I can compress/zip the data being sent back and forth?

Was it helpful?

Solution

Is the INSERT or the UPDATE the culprit, or are they both bad?

You're bringing the rows with NULL values for Date from the remote server, and then sending those values back again in the UPDATE statement. You could potentially save traffic by selecting the updated rows and inserting them locally. It looks like you could do this (sorry, no MS SQL Server available to test against) by saying

INSERT INTO [My Local Server].[DB Name].dbo.Login 
(LoginID, UserID, Message)
SELECT *
FROM
  UPDATE [Remote Server].[Remote DB Name].dbo.Login2 
  SET Date = GETDATE()
  OUTPUT LoginID, UserID, Message
  WHERE Date IS NULL

See the MSDN INSERT docs and OUTPUT docs for details.

Or maybe OUTPUT INTO is the way to go:

UPDATE [Remote Server].[Remote DB Name].dbo.Login2 
SET Date = GETDATE()
OUTPUT LoginID, UserID, Message
INTO [My Local Server].[DB Name].dbo.Login (LoginID, UserID, Message)
WHERE Date IS NULL

OTHER TIPS

Seems that the UPDATE has to pull the entire remote table over, scan the records and do a nested look up into the @temp1, the ship back the updates. Its just a guess, w/o more additional information. You should look at the actual execution plan and trace where is the time spent. Also using STATISTICS IO ON could help.

This is really just a shot in the dark, but maybe you can try a join instead of the EXISTS:

WITH cte_remote AS (
  SELECT z.Date
    FROM [Remote Server].[Remote DB Name].dbo.Login2 AS z
    JOIN @temp1 AS x ON z.DateCollected IS NULL AND x.LoginID = z.LoginID)
UPDATE cte_remote 
    SET Date = GETDATE();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top