SQL Server中链接服务器上同义词的性能影响

Fra*_*ier 11 sql-server performance linked-server synonym sql-server-2008-r2

在localserver(SQL Server 2008 R2)上,我有一个名为syn_view1指向链接服务器的同义词remoteserver.remotedb.dbo.view1

此SLOW查询需要20秒才能运行.

select e.column1, e.column2
from syn_view1 e
where e.column3 = 'xxx'
  and e.column4 = 'yyy'
order by e.column1
Run Code Online (Sandbox Code Playgroud)

此FAST查询需要1秒才能运行.

select e.column1, e.column2
from remoteserver.remotedb.dbo.view1 e
where e.column3 = 'xxx'
  and e.column4 = 'yyy'
order by e.column1
Run Code Online (Sandbox Code Playgroud)

两个查询的唯一区别实际上是同义词的存在.显然,同义词会影响查询的性能.

SLOW查询的执行计划是:

Plan                Cost %  Subtree cost
4 SELECT
I/O cost: 0.000000  CPU cost: 0.000000  Executes: 0  
Cost: 0.000000                  0.00    3.3521
    3 Filter
    I/O cost: 0.000000  CPU cost: 0.008800  Executes: 1  
    Cost: 0.008800              0.26    3.3521
        2 Compute Scalar
        I/O cost: 0.000000  CPU cost: 3.343333  Executes: 1  
        Cost: 0.000000          0.00    3.3433
            1 Remote Query
            I/O cost: 0.000000  CPU cost: 3.343333  Executes: 1  
            Cost: 3.343333      99.74   3.3433
Run Code Online (Sandbox Code Playgroud)

对于FAST查询:

Plan            Cost %  Subtree cost
3 SELECT
I/O cost: 0.000000  CPU cost: 0.000000  Executes: 0  
Cost: 0.000000              0.00    0.1974
    2 Compute Scalar
    I/O cost: 0.000000  CPU cost: 0.197447  Executes: 1  
    Cost: 0.000000          0.00    0.1974
        1 Remote Query
        I/O cost: 0.000000  CPU cost: 0.197447  Executes: 1  
        Cost: 0.197447      100.00  0.1974
Run Code Online (Sandbox Code Playgroud)

我的理解是,在SLOW查询中,服务器从远程服务器获取所有数据,然后应用过滤器(尽管没有索引),而在FAST查询中,服务器从远程服务器获取过滤后的数据,从而使用远程索引.

快速有没有办法使用同义词?也许是链接服务器的设置?本地数据库服务器?

谢谢您的帮助!

Pie*_*ens 1

dba.stackexchange.com 上这篇文章的接受答案指出,由于链接服务器上的访问权限有限,限制了表统计信息对本地服务器的可见性,因此在通过链接服务器进行查询时可能会出现性能问题。这可能会影响查询计划,从而影响性能。

摘抄:

这就是为什么我得到不同的结果。当以 sysadmin 身份运行时,我获得了完整的分布统计信息,表明没有订单 ID > 20000 的行,并且估计为一行。(回想一下,优化器从不假设统计数据为零行。)但是当以普通用户身份运行时,DBCC SHOW_STATISTICS 因权限错误而失败。此错误不会传播,但优化器接受没有统计信息并使用默认假设。由于它确实获得了基数信息,因此它了解到远程表有 830 行,因此估计有 249 行。