How to diagnose a query with a sub-query against another server.

  • I have a problem with performance of a query embedded in some 3rd party software.

    The query (see below) performs a select of some fields on one server WHERE the product_id is IN a sub-query that exists on another server.

    SELECT

    ServiceProvider.[ServiceProviderGuid],

    ServiceProvider.[ServiceProviderTypeGuid],

    ServiceProvider.[ServiceProviderName],

    ServiceProvider.[ServiceProviderId],

    ServiceProvider.[MainPlant],

    ServiceProvider.[ServiceProviderParentGuid]

    FROM ServiceProvider

    WHERE ServiceProvider.[ServiceProviderGuid] IN (

    SELECTvwCRMServiceProviderAccess.[ServiceProviderGuid]

    FROMvwCRMServiceProviderAccess

    WHERE[vwCRMServiceProviderAccess].[UserGuid] = '4D569648-C703-47BA-BF66-D8E39F5C9D49' )

    The sub-query runs against a view which contains a fully qualified reference to a table on another server.

    Here is the issue: I can run the main query and it returns in less than 1 second. I can extract just the sub-query and it runs in 1 second when executed from main server, performing a cross-server query. But when these are both run in the full query, it takes 33 seconds to return!

    I can't figure out what would cause this. I've attempted to trace this using profiler. I can easily capture information regarding the base query, but I am unable to see the cross-server portion from a profiler trace running on the remote server.

    The actual execution plan shows that 98% of the cost of running this query resides in the remote query portion, but I am stumped by the fact that I can just execute that sub-query portion and have it return in 1 second.

    Do you have any ideas about how I might be able to trouble-shoot this, or what might be happening?

    Thanks in advance for your thoughts and advice.

    Larry

  • Have you tried a JOIN instead of the IN?

    I've read there can be unexpected problems with cross-server queries.

    SELECT --USE DISTINCT if there are multiple rows in vwCRMServiceProviderAccess

    SP.[ServiceProviderGuid],

    SP.[ServiceProviderTypeGuid],

    SP.[ServiceProviderName],

    SP.[ServiceProviderId],

    SP.[MainPlant],

    SP.[ServiceProviderParentGuid]

    FROM ServiceProvider SP

    JOIN vwCRMServiceProviderAccess SPA ON SP.[ServiceProviderGuid] = SPA.[ServiceProviderGuid]

    WHERESPA.[UserGuid] = '4D569648-C703-47BA-BF66-D8E39F5C9D49'

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Larry Kruse (8/29/2012)


    I have a problem with performance of a query embedded in some 3rd party software.

    The query (see below) performs a select of some fields on one server WHERE the product_id is IN a sub-query that exists on another server.

    SELECT

    ServiceProvider.[ServiceProviderGuid],

    ServiceProvider.[ServiceProviderTypeGuid],

    ServiceProvider.[ServiceProviderName],

    ServiceProvider.[ServiceProviderId],

    ServiceProvider.[MainPlant],

    ServiceProvider.[ServiceProviderParentGuid]

    FROM ServiceProvider

    WHERE ServiceProvider.[ServiceProviderGuid] IN (

    SELECTvwCRMServiceProviderAccess.[ServiceProviderGuid]

    FROMvwCRMServiceProviderAccess

    WHERE[vwCRMServiceProviderAccess].[UserGuid] = '4D569648-C703-47BA-BF66-D8E39F5C9D49' )

    The sub-query runs against a view which contains a fully qualified reference to a table on another server.

    Here is the issue: I can run the main query and it returns in less than 1 second. I can extract just the sub-query and it runs in 1 second when executed from main server, performing a cross-server query. But when these are both run in the full query, it takes 33 seconds to return!

    I can't figure out what would cause this. I've attempted to trace this using profiler. I can easily capture information regarding the base query, but I am unable to see the cross-server portion from a profiler trace running on the remote server.

    The actual execution plan shows that 98% of the cost of running this query resides in the remote query portion, but I am stumped by the fact that I can just execute that sub-query portion and have it return in 1 second.

    Do you have any ideas about how I might be able to trouble-shoot this, or what might be happening?

    Thanks in advance for your thoughts and advice.

    Larry

    Cross-server queries can be unpredictable. The first option most folks explore is to run the result from the remote query into a local #temp table, which you can then index if required. Your case may be ideal for this solution, if the result set from the remote query is small.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • Luis Cazares (8/29/2012)


    Have you tried a JOIN instead of the IN?

    I've read there can be unexpected problems with cross-server queries.

    SELECT --USE DISTINCT if there are multiple rows in vwCRMServiceProviderAccess

    SP.[ServiceProviderGuid],

    SP.[ServiceProviderTypeGuid],

    SP.[ServiceProviderName],

    SP.[ServiceProviderId],

    SP.[MainPlant],

    SP.[ServiceProviderParentGuid]

    FROM ServiceProvider SP

    JOIN vwCRMServiceProviderAccess SPA ON SP.[ServiceProviderGuid] = SPA.[ServiceProviderGuid]

    WHERESPA.[UserGuid] = '4D569648-C703-47BA-BF66-D8E39F5C9D49'

    Yep. I did the same thing you did here Luis, and it returns in 1 second. Quite an improvement, however the developers are telling me that this is a piece of code written by some 3rd party consultants long ago, and everyone is leery about making any direct modifications to their code.

    Apparently, this query crossed over a threshhold of some kind on Monday night and began timing out. When I run the query in an SSMS window, it returns in 33 seconds. I think that the application is timing out at the 30 second mark.

    My boss has asked me to research "why" this has just begun failing. I cannot answer that and was hoping to be able to figure out a way to answer his question. It puzzles me that either of the queries can be run by themselves and both return within a second, but when combined using the IN expression, it suddenly takes 33 seconds.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply