Link to home
Start Free TrialLog in
Avatar of mvanral
mvanral

asked on

Slow Insert to LinkedServer

Insert statement to remote server is running very slowly. I have run Profiler and find there is a 'sp_cursor' call for each row. The source system is SQL2005 and destination is SQL2000(sp4). The linked server is using 'SQL server' type connection. Source query is against a single table with a where clause. source and destination table are identical with Primary keys. Purpose is just to move the rows. Connection is a slow network connection - should be ok. I have already overcome same problem for related update and delete queries by use of 'EXECUTE (query) AT LinkedServer' that works great - but insert can not take advantage of this...

INSERT [LinkedServSQL2000sp4].dbname.schema.tablename
({column list})
Select
      {column list}
from tablename
WHERE col1 =  '7/20/2006'
  AND col2 in (2,5,7,12,32,54,45,33)

Any thoughts?
Avatar of DanielSKim
DanielSKim

is this any faster for you?

INSERT OPENQUERY([LinkedServSQL2000sp4], 'SELECT {column list} FROM dbname.schema.tablename')
Select
     {column list}
from tablename
WHERE col1 =  '7/20/2006'
  AND col2 in (2,5,7,12,32,54,45,33)
or

INSERT OPENQUERY([LinkedServSQL2000sp4], 'SELECT {column list} FROM dbname.schema.tablename WHERE 1 = 0')
Select
     {column list}
from tablename
WHERE col1 =  '7/20/2006'
  AND col2 in (2,5,7,12,32,54,45,33)
Avatar of mvanral

ASKER

I have tried both of your suggestions and both physically run - cool. BUT both still create the sp_cursor for each row. I am expecting it to do an insert statement from 1 server to the other for all the rows without the need for it to create and use a server based "Cursor" [note: I am not explicitly creating  a cursor!] and then do each row 1 at a time. I have, in test limited my row count to 50 to make sure that size is not the issue.
Avatar of mvanral

ASKER

Well I have found the answer:
It is not possible to have INSERT to another server and do it FAST(i.e as a single INSERT statement). It is necessary to do 'EXECUTE (@cmdstr) AT Linked server' where the whole insert statement is passed and used to Pull the data from the source server!!! Now if you are pulling the data from a linked server all is just fine...
Took 2 days of trying everything....

I think i should get the points????
Avatar of mvanral

ASKER

By the way - getting it right has a radical effect on performace  - 17000 about 16 mins(975 secs) with straight INSERT statement inserting to remote server. But when instart statement put inside an EXECUTE and passed to remote server that then gets the data and inserts it locally it takes <30secs. [All other variables same!]
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial