Pages

Showing posts with label Linked Server. Show all posts
Showing posts with label Linked Server. Show all posts

Monday, July 27, 2009

Linked Server performance may depend on permission

According to Linchi Shea post you should check the
permissions you are using to access a Linked Server.

As you know SQL Server optimizer tries to choose the best
way to execute a query according to distribution statistics
of the tables involved in the query.

The problem here is that the user you use to access the
Linked Server may have not permissions to get the statictics
on a table in Linked Server. In this case the Optimizer may
choose table scan instead of seek,for example. And this may
hurt the performance of the distributed query.

It turns out that the user needs the same permission as is
required to execute DBCC SHOW_STATISTICS command.
Meaning the user should be one of the following:
Table Owner or SysAdmin or DB_Owner or DB_ddladmin.

Information source:
http://sqlblog.com/blogs/linchi_shea/archive/2009/07/21/performance-impact-linked-server-security-configuration-and-how-it-can-hurt-you.aspx

How to test the connection to a Linked Server?

You may want to test the connection to a Linked Server
before executing a query/sp against the Linked Server.

In SQL Server 2000 there is no way to test the connection
without execution a query/sp against the Linked Server.
In SQL Server 2005 you can use system stored procedure
named "sp_testlinkedserver".It accepts Linked Server name
and returns 0 in case of success and 1 in case of failure.

DECLARE @LinkedServerName SYSNAME,@RC INT
SET @LinkedServerName=
'TargetLinkedServer'
BEGIN TRY
EXEC @RC=sys.sp_testlinkedserver @LinkedServerName
END TRY
BEGIN CATCH

SET @RC=
SIGN(@@ERROR)
END CATCH
IF @RC<>0
RAISERROR('There was en error connecting to "%s" server.',16,1,@LinkedServerName)
ELSE
PRINT 'You should be able to connect to "'+@LinkedServerName+'"
server.'

For more information:
http://technet.microsoft.com/en-us/library/ms189809.aspx

Thursday, July 23, 2009

How to truncate table on Linked Server?

Suppose you want to truncate a table on Linked Server.
If you try to execute something like this:

TRUNCATE TABLE LinkedServerName.DbName.dbo.TableName

You will get the following error:
The object name 'LinkedServerName.DbName.dbo.TableName.'
contains more than the maximum number of prefixes. The maximum is 2.

This is because DDL statements are not supported on Linked Servers.
And "Truncate Table" is DDL statement.

However there is a way to do it:
If you have SQL 2005 then try the following:

EXEC('TRUNCATE TABLE DbName.dbo.TableName) AT LinkedServerName

In SQL 2000 and 2005 the following will also work:

EXECUTE LinkedServerName.DbName.dbo.sp_executesql
N'TRUNCATE TABLE dbo.TableName'