Advertisement

04.17.2008 at 10:09PM PDT, ID: 23333283
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.3

Server aliases/linked servers cause remote queries

Asked by nolee in SQL Server 2005, Databases Miscellaneous, SQL Query Syntax

Tags: , ,

Can someone please shed some light on what seems to me to be a common requirement.

If I create an alias or linked server to Server1 - say Alias1 - on Server1 and then use that name in a query on Server1, a remote/distributed query is always used (even though we are running on the local server and that overhead is completely unnecessary).

Is SQL Server really not capable of deciding that
select * from Alias1.db1.dbo.table1
and
select * from Server1.db1.dbo.table1
should be optimized and executed exactly the same when Alias1 is Server1, but that it is a distributed query ONLY when Alias1 is really referring to a remote server? I realize that the four part name is not necessary when I am referring to objects on the current server, but I am trying to write code that is server instance independent.

It just seems that if that is not possible, then the only way to create system independent stored procs that can run in dev, staging, and production environments and work with multiple databases on multiple servers is to create all sorts of scripts to regenerate all the procs whenever you move a database between servers?

If SQL Server is even close to the enterprise big iron server that MS now claims it is, it surely needs to support running in dev, staging, and production environments and work with multiple databases on multiple servers?!

I'm really looking for someone to tell me I'm missing something simple, and of course you can do this - but complex workarounds are invited too :-)
This is not something I am investigating as an academic exercise, I am already doing this, but I have to figure out how to do it better because with all these unnecessary distributed queries, performance is horrible.

Specifically - if I am using an alias and it refers back to itself, then SQL Server uses a distributed query instead of a regular local query and this is really slow - I just tried one and the query took 22 seconds if I used the alias and 14 milliseconds without the alias.

If the data really was on another server I could understand that there must be overhead to run the distributed query, but if the alias is pointing to the local server surely SQL Server should be able to recognise that and use a local query?

Try this yourself, create a linked server that refers to itself (or create a server alias, they both result in the same thing). Then write two queries that both use four part names, one that uses the real server name, one that uses the linked server or alias name for the server.

Run the queries one at a time and trace them. You'll see that the one that uses an alias runs a whole bunch of distributed query code and it will run slower (how much depends on the specific query). The things that you'll see for the alias query will look like this:Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
SET XACT_ABORT OFF
go
declare @p1 int
set @p1=1
declare @p2 bigint
set @p2=168684858717458
exec [sys].sp_getschemalock @p1 output,@p2 output,N'"Polls"."dbo"."tblQuestions"'
select @p1, @p2
go
declare @p1 int
set @p1=1
exec sp_prepexec @p1 output,NULL,N'SELECT "Tbl1002"."QuestionID" "Col1004"
,"Tbl1002"."label_url" "Col1005","Tbl1002"."QuestionText" "Col1006"
,"Tbl1002"."IsArchived" "Col1007","Tbl1002"."IsHidden" "Col1008"
,"Tbl1002"."DateToRelease" "Col1009","Tbl1002"."DateToExpire" "Col1010"
,"Tbl1002"."EntityID" "Col1011","Tbl1002"."DateCreated" "Col1012"
,"Tbl1002"."DateModified" "Col1013" FROM "Polls"."dbo"."tblQuestions" "Tbl1002"'
select @p1
go
exec sp_unprepare 1
go
exec [sys].sp_releaseschemalock 1
go
[+][-]04.18.2008 at 01:56AM PDT, ID: 21384143

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.18.2008 at 07:57PM PDT, ID: 21390966

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.18.2008 at 08:12PM PDT, ID: 21391008

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, Databases Miscellaneous, SQL Query Syntax
Tags: Microsoft, SQL Server, 2005
Sign Up Now!
Solution Provided By: Einstine98
Participating Experts: 1
Solution Grade: B
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628