上的LocalServer(一个SQL Server 2008 R2),我有个同义词叫做syn_view1
指向链接服务器remoteserver.remotedb.dbo.view1
性能的影响
这种缓慢的查询需要20秒运行。
select e.column1, e.column2
from syn_view1 e
where e.column3 = 'xxx'
and e.column4 = 'yyy'
order by e.column1
这种快速查询需要1秒运行。
select e.column1, e.column2
from remoteserver.remotedb.dbo.view1 e
where e.column3 = 'xxx'
and e.column4 = 'yyy'
order by e.column1
在两个查询唯一的区别是真正的代名词的存在。 显然,同义词会影响查询的性能。
的慢查询执行计划是:
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
而对于快速查询:
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
我的理解是,在慢速查询时,服务器会从所有数据远程服务器,然后应用过滤器(尽管没有索引),而在FAST查询中,服务器从远程服务器获取过滤的数据,从而使用远程索引。
快速时有什么方法可以使用同义词吗? 也许设置了链接的服务器?本地数据库服务器?
感谢您的帮助!
建议在远程服务器上创建一个过程。 – 2013-03-08 12:54:59
@AaronBertrand,它会改变什么? 索引是否会被使用?我能否避免在存储过程中指定链接服务器的完整限定名称? – 2013-03-11 06:24:05
是的,是的。您可以为存储过程名称而不是视图创建同义词。 – 2013-03-11 11:00:16