問題描述
我正在通過鏈接服務(wù)器從 VIEWS 中提取大量數(shù)據(jù).我使用的是 SQL Server 2012,鏈接服務(wù)器是 SQL Server 2008
I am extracting large amount of data via linked server from VIEWS. I am using SQL Server 2012 and linked server is SQL Server 2008
我的選擇語(yǔ)句是
SELECT * INTO MY_LOCAL_TABLE
FROM
( SELECT * FROM LINKEDSERVER.DB.TABLE.VIEW
WHERE DATE>'2012-01-01' AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)
) Q
我期待 300K 行有將近 700 個(gè) ID.以前需要幾個(gè)小時(shí),但現(xiàn)在需要 20 多個(gè)小時(shí)!!
I am expecting 300K rows for nearly 700+ IDs. before it used to take couple of hours but now its take more than a 20 hr!!
您能否為此 PAIN 提出任何替代解決方案?
Could you please suggest any alternative solution for this PAIN??
非常感謝!
推薦答案
當(dāng)您使用由 4 部分組成的名稱時(shí),例如 [server].db.dbo.table
,尤其是在 中join
,很多時(shí)候整個(gè)表都通過網(wǎng)絡(luò)復(fù)制到本地機(jī)器上,這顯然不太理想.
When you use a 4-part name such as [server].db.dbo.table
, especially in a join
, often times the entire table is copied over the wire to the local machine, which is obviously not ideal.
更好的方法是使用 OPENQUERY
-- 在源(鏈接服務(wù)器)處理.
A better approach is to use an OPENQUERY
-- which is handled at the source (linked server).
試試:
SELECT *
FROM OPENQUERY([LINKEDSERVER], 'SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01')
AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)
使用這種方法,鏈接服務(wù)器將返回日期 > x 的所有行,然后本地服務(wù)器將通過本地表中的 ID 過濾該行.
With this approach the linked server will return all rows for date > x, and then the local server will filter that by ID's in your local table.
當(dāng)然,索引仍然會(huì)影響 SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01
.
Of course, indexing will still play a factor for doing SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01
.
我在大型子集上使用的另一種方法是將本地 ID 轉(zhuǎn)儲(chǔ)到遠(yuǎn)程服務(wù)器,然后遠(yuǎn)程處理它,例如:
Another approach, which I use on large subsets, is to dump the local ID's to the remote server, THEN handle it all remotely, such as:
-- copy local table to linked server by executing remote query
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'SELECT ID INTO db.dbo.tmpTable FROM [SERVER].DB.DBO.MY_LOCAL_VIEW'
EXEC(@SQL) AT [LINKEDSERVER]
-- index remote table?!?
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'CREATE INDEX [IXTMP] ON db.dbo.tmpTable (ID)'
EXEC(@SQL) AT [LINKEDSERVER]
-- run query on local machine against both remote tables
SELECT *
-- INTO sometable
FROM OPENQUERY([LINKEDSERVER], 'SELECT *
FROM DB.TABLE.VIEW
WHERE DATE>''2012-01-01''
AND ID IN (SELECT ID FROM db.dbo.tmpTable)')
-- now drop remote temp table of id's
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'DROP TABLE db.dbo.tmpTable'
EXEC(@SQL) AT [LINKEDSERVER]
如果本地視圖也很大,那么您可以考慮執(zhí)行一個(gè)使用 openquery 返回本地機(jī)器的遠(yuǎn)程查詢(假設(shè)遠(yuǎn)程機(jī)器有本地作為鏈接).
If the local view is also large, then you may consider executing a remote query that uses an openquery back to the local machine (assuming the remote machine has the local as a link).
-- copy local table to linked server by executing remote query
DECLARE @SQL NVARCHAR(MAX)
SET @SQL = 'SELECT ID INTO db.dbo.tmpTable FROM OPENQUERY([SERVER], ''SELECT ID FROM DB.DBO.MY_LOCAL_VIEW'')'
EXEC(@SQL) AT [LINKEDSERVER]
這篇關(guān)于SQL 鏈接服務(wù)器查詢非常非常慢的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!