上的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