SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
Larry Kruse
Larry Kruse
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 756
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
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16248 Visits: 19059
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.
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16030 Visits: 19524
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
Larry Kruse
Larry Kruse
SSC Veteran
SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)SSC Veteran (227 reputation)

Group: General Forum Members
Points: 227 Visits: 756
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search