3

I have a set of queries that SELECT various data over a linked server connected to a 2nd SQL Server.

If I run a single query, I can see results being returned to the local server.

Run as a group, it just sits there, showing either OLEDB waits on the local box or in a SUSPENDED state on the linked server (wait type: ASYNC_NETWORK_IO).

The query finished (after 44 mins) with the following errors:

OLE DB provider "SQLNCLI10" for linked server "[MyServer]" returned message "Protocol error in TDS stream".

OLE DB provider "SQLNCLI10" for linked server "MyServer" returned message "Query timeout expired".

Msg 7330, Level 16, State 2, Line 127 Cannot fetch a row from OLE DB provider "SQLNCLI10" for linked server "MyServer".

Can anyone here point me in the direction of how I can both diagnose and fix this?

0

4 Answers 4

2

Pulling data through a linked server is unlikely to be quick especially if you are joining remote tables to local tables. (Just use Profiler to watch what your local server sends to the remote server and I think that you'll be convinced.) Linked Servers are convenient and often "good enough", but not when you start blowing through your batch processing windows.

I have seen overall processing go faster by pulling the remote data into local (temporary or permanent) tables and then "doing the joins" locally to figure out what to put into the production tables.

If you have a large amount of data, I suggest looking into using a SSIS package or bcp and probably bringing the data into a local staging table first and converting the code to run off of local tables. This might involve a lot of surgery to your jobs since you need to have a job step (or steps) to run bcp code and/or packages.

If you are using SQL 2008 or better, MERGE should be available and it could provide more efficient operations. I am unsure if MERGE works with tables on a linked server and, if it does, performance may not be much better than what you have. So, you are still stuck getting the data onto the local server.

Also, you could look into partition switching, but I think would be the most costly thing to do in terms of time spent on getting things to work. You could view it as a learning experience.

Alternatively, you might consider increasing the query timeout, which is a server-level setting on the local server. That might help with the timeout error (until your data volume increases, then you would need to tweak it again), but I doubt that it will help with the TDS error problem.

If you want to minimize the length of time that your production tables are locked (due to the INSERT, not the SELECT with nolock), you would want to get all of the data from the ETL system onto the local box first, then go and insert it all. You want to avoid pulling data for one table, inserting that, pulling the data for the next table, inserting that, etc. (I'm presuming that you are doing all of this in a declared transaction that I'm not seeing.)

You may also need to increase the timeout on the linked server, or simply set it to 0, which indicates to wait forever:

EXEC sys.sp_serveroption @server = N'<servername>'
      , @optname = 'query timeout'
      , @optvalue = '0'; 

You may also benefit from setting the collation compatible option to 1 if the source and target SQL Server databases use the same0or-compatible collations, since that will allow certain predicates to be pushed to the source server, reducing the need for all rows in the source table to be transferred to the target server:

EXEC sys.sp_serveroption @server = N'<servername>'
      , @optname = 'collation compatible'
      , @optvalue = '1';

Details for the sp_serveroption stored procedure are available here.

1

Using an ETL tool such as SSIS would be my preferred method of importing your data. However, if you must use Linked Servers, then I would suggest pulling the data from the Linked Server first and storing it in a temp table or another staging table.

SELECT peETL.* 
INTO #TEMPpeETL 
FROM [remoteServer].[remoteDBName].[dbo].[productExtended_accessory]

INSERT INTO [LocalDBName].[dbo].[productExtended_accessory]
SELECT #TEMPpeETL.* 
FROM #TEMPpeETL 
LEFT OUTER JOIN [LocalDBName].[dbo].[productExtended_accessory] pePROD ON peETL.id = pePROD.id
WHERE pePROD.id IS NULL

DROP TABLE #TEMPpeETL

By pulling all (or some) of the data over and then doing the join locally, you will have better results. Doing a join over a Linked Server can cause unpredictable results.

If you can add a where clause to the initial data pull from the Linked Server, then you can improve the performance even more by only pulling some of the data over.

0

Looking at your queries I assume the purpose of your queries is to keep the local DB tables in sync with the records in the remote DB by inserting the records that do not currently exist in the local DB.

SSIS would be more suited and likely to be a quicker way of doing this. If you wish to have an exact carbon copy of the tables from your remote DB within your local DB you could also look into Replication to keep them in sync.

0

Just to put it out there as an alternative, "Service Broker" is really fast and allows you to communicate with other nodes using certificates. It has a learning curve but it's one of the fastest and most reliable messaging/queueing systems available.

I would also look into your network issues to see if they can be resolved. Do you know how many hops you're going through and what the bottleneck is? Can you directly query that server for a large amount of data and get it fast w/o acquiring high network_io waits?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.