The reason that it is so slow to insert into the remote table from the local table is because it inserts a row, checks its insertion and then inserts the next row, checks that it is inserted, etc.
I don’t know if you understood it or not, but here’s how I solved this problem using linked servers.
Firstly, I have LocalDB.dbo.Table with multiple columns:
IDColumn (int, PK, Auto Increment) TextColumn (varchar(30)) IntColumn (int)
And I have RemoteDB.dbo.Table, which is almost the same:
IDColumn (int) TextColumn (varchar(30)) IntColumn (int)
The main difference is that the remote IDColumn is not configured as an identifier column, so I can embed it in it.
Then I set the trigger on the remote table, which happens when deleting
Create Trigger Table_Del On Table After Delete AS Begin Set NOCOUNT ON; Insert Into Table (IDColumn, TextColumn, IntColumn) Select IDColumn, TextColumn, IntColumn from MainServer.LocalDB.dbo.table L Where not exists (Select * from Table R WHere L.IDColumn = R.IDColumn) END
Then, when I want to do the insertion, I do it like this on the local server:
Insert Into LocalDB.dbo.Table (TextColumn, IntColumn) Values ('textvalue', 123); Delete From RemoteServer.RemoteDB.dbo.Table Where IDColumn = 0;
By running a remote server to pull data from the local server and then paste, I was able to complete a task that took 30 minutes to insert 1258 rows into a task that took 8 seconds to do the same paste.
This requires a connected connection to the server on both sides, but after that it works very well.
Update:
Therefore, in the last few years, I made some changes and moved away from the delete trigger as a way to synchronize the deleted table.
Instead, I have a stored procedure on a remote server that has all the steps to pull data from a local server:
CREATE PROCEDURE [dbo].[UpdateTable] -- Add the parameters for the stored procedure here AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here --Fill Temp table Insert Into WebFileNamesTemp Select * From MAINSERVER.LocalDB.dbo.WebFileNames --Fill normal table from temp table Delete From WebFileNames Insert Into WebFileNames Select * From WebFileNamesTemp --empty temp table Delete From WebFileNamesTemp END
And on the local server, I have a scheduled task that does some processing in local tables, and then starts the update through a stored procedure:
EXEC sp_serveroption @server='REMOTESERVER', @optname='rpc', @optvalue='true' EXEC sp_serveroption @server='REMOTESERVER', @optname='rpc out', @optvalue='true' EXEC REMOTESERVER.RemoteDB.dbo.UpdateTable EXEC sp_serveroption @server='REMOTESERVER', @optname='rpc', @optvalue='false' EXEC sp_serveroption @server='REMOTESERVER', @optname='rpc out', @optvalue='false'