Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to diagnose a query with a sub-query against another server. Expand / Collapse
Author
Message
Posted Wednesday, August 29, 2012 6:20 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 7:58 AM
Points: 91, Visits: 640
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 (
SELECT vwCRMServiceProviderAccess.[ServiceProviderGuid]
FROM vwCRMServiceProviderAccess
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
Post #1351555
Posted Wednesday, August 29, 2012 6:49 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:34 PM
Points: 2,788, Visits: 5,973
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]
WHERE SPA.[UserGuid] = '4D569648-C703-47BA-BF66-D8E39F5C9D49'




Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1351576
Posted Wednesday, August 29, 2012 6:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:12 AM
Points: 6,783, Visits: 12,893
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 (
SELECT vwCRMServiceProviderAccess.[ServiceProviderGuid]
FROM vwCRMServiceProviderAccess
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
Exploring Recursive CTEs by Example Dwain Camps
Post #1351583
Posted Wednesday, August 29, 2012 7:48 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: 2 days ago @ 7:58 AM
Points: 91, Visits: 640
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]
WHERE SPA.[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.
Post #1351625
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse