Solved

Need to call inline table function without passing parameters or speed up view calling linked server

Posted on 2008-09-29
7
638 Views
Last Modified: 2012-06-27
Hi,

First a bit of background.
I have two SQL 2005 servers.
All of the below are executed on the primary server (ServerA).
The data resides on the second server (ServerB) which is set up as a linked server (LinkedServerB) on ServerA

I have a legacy application that runs queries agains ServerB. I need to move an instance of this application into a firewall and the easiest way to do this it to keep the data on ServerB, create a linked server to ServerB from ServerA and to replace all the tables on ServerA with views that has exactly the same names as the tables on ServerA. The views then make calles to the talbes on ServerB.

Testing performace the following became clear:
1) If I run the following from SQL Management Studio the result comes back sub-sceond
     SELECT *  FROM [LinkedServerB].[Database1].dbo.[Table1] with (nolock)  WHERE sharecode = 'KWV'

2) If I create a view on ServerA called Table1 that does a select against [LinkedServerB].[Database1].dbo.[Table1] in exactly the same way as no 1 above ( SELECT *  FROM [LinkedServerB].[Database1].dbo.[Table1] with (nolock)), then the following query takes op to 50 seconds to complete:
     SELECT *  FROM [ServerA].[Database1].dbo.[Table1] WHERE sharecode = 'KWV'  
                 (Remember that Table1 in this case is actually a view

3) If I create a Inline Table-Valued function called Table1() on ServerAB  that does a select against [ServerB].[Database1].dbo.[Table1] in exactly the same way as no 1 above ( SELECT *  FROM [LinkedServerB].[Database1].dbo.[Table1] with (nolock)) , then the following query takes sub-second to complete:
     SELECT *  FROM [ServerA].[Database1].dbo.[Table1]() WHERE sharecode = 'KWV'  
                 (Remember that Table1 in this case is actually a function)

Wrapping the function in a view has no effect on the speed of the view making the view again taking up to 50 seconds.

Other than re-developing the legacy applications or shipping data between these two databases I seem to have only two options:
A) Find a way to speed up the views
B) FInd a way to call inline table-valued function without have to use the paranthesis when calling them for example:
     SELECT *  FROM [ServerA].[Database1].dbo.[Table1] WHERE sharecode = 'KWV'  as opposed to
     SELECT *  FROM [ServerA].[Database1].dbo.[Table1]() WHERE sharecode = 'KWV'  

This will allow the legacy appliction to use the remote linked server data as if it resides in local tables.

I am willing to give the points for a solution to either A or B above.

Thanks

Gerhatd
       





0
Comment
Question by:dbit
  • 3
  • 2
7 Comments
 
LVL 2

Expert Comment

by:sandson
ID: 22596239
Hello,

it seems that your view retrieves all data on ServerB prior to applying the where close. Can you display the execution plan to confirm that ?

It may also need an index on the sharecode column, try by adding an index on sharecode in the view Table1 on ServerA (look at this articled to create the indexed view : http://msdn.microsoft.com/en-us/library/aa215090(SQL.80).aspx)

regards,
Abdel
0
 

Author Comment

by:dbit
ID: 22596347
Yes, I've already looked at the execution plans for all the scenarios I described. Using a view definitely seem to bring back all the rows from the linked server.

There is already an primary key index on the table on the remote server on the sharecode.
Using the SQL profiler to look at the remote server instructions, I noticed that the bottleneck seem to be caused by the server calling sp_getschemalock:
     exec [sys].sp_getschemalock @p1 output,@p2 output,N'"TimeSeries"."dbo"."hi"'

I tried to get around this by specifying with (nolock) on the query statement in the view but that does not seem to help.
0
 
LVL 2

Expert Comment

by:sandson
ID: 22596729
I ran a test on my servers using your second scenario (a view run a query against the remote server)
ServerA doesn't do anything for me. According to the execution plan it simply exec a remote query on ServerB by combining the where clause of the main query (ie. select * from Table1 where sharecode = 'KWV') with the view definition :
SELECT *  FROM [LinkedServerB].[Database1].dbo.[Table1] with (nolock)  WHERE sharecode = 'KWV'

So meaning that the optimizer understood my query and combined the view and the query to create a remote query to run on ServerB.

How did you linked ServeB to ServerA. Did you set it up as a 'SQL Server' server, thus allowing the SQL engine to use optimizations dedicated to SQL Server ?

regards,
Abdel
0
 

Author Comment

by:dbit
ID: 22597006
Yes, you are correct and I see exactly the same.
As I stated before, it seems to be the fact that the linked server are waiting for schema locks that creates the bottleneck.

In my case waiting for the schema locks take 99% of the time to execute on the linked server.
0
 
LVL 2

Accepted Solution

by:
sandson earned 500 total points
ID: 22603112
Hi dbit,

after a good sleep period, I may have an idea. if ServerB sends all it's data (as it seems to be the case) to ServerA then it means that the two servers may not be compatible for comparing it and ServerB lets ServerA dealing with the translation of incompatible values (here sharecode column and the literal value 'KWV').

Can you check in the server link options for 'Collation Compatible', option which indicate to the server that they share the same collation settings and thus are able to deal with strings (char, varchar, ...) in the same way ? Set it to true and test again.

regards,
Abdel
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now