We have a series of programs that are designed to move data from one SQL Server database into another using OPENQUERY and database linked servers.
We are noticing that queries such as the following are taking orders magnitude long than anticipated.
DELETE OPENQUERY(server, 'SELECT * FROM table WHERE pkf1 = ''v1'' AND pkf2 = ''v2''')
For example, if the above was rewritten as:
exec('DELETE * FROM table WHERE pkf1 = ''v1'' and pkf2 = ''v2''') at server
or
DELETE FROM server.schema.table WHERE pkf1 = 'v1' and pkf2 = 'v2'
the performance is only a few seconds compared to minutes using OPENQUERY
.
Can someone explain why the OPENQUERY
approach is so inefficient by comparison? Does it not use the primary key indexes?