SQL 链接服务器查询非常非常慢

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24272652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 02:03:50  来源:igfitidea点击:

SQL Linked server query very very slow

sqlsql-servertsql

提问by arm

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

我正在通过链接服务器从 VIEWS 中提取大量数据。我使用的是 SQL Server 2012,链接服务器是 SQL Server 2008

My select statement is

我的选择语句是

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

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!!

我预计将近 700 个 ID 有 300K 行。以前需要几个小时,但现在需要 20 多个小时!!

Could you please suggest any alternative solution for this PAIN??

您能否为此 PAIN 提出任何替代解决方案?

Very many thanks in advance!

非常感谢提前!

回答by Dave Cullum

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.

当您使用 4 部分名称时[server].db.dbo.table,尤其是在 a 中时join,通常会通过网络将整个表复制到本地计算机,这显然是不理想的。

A better approach is to use an OPENQUERY-- which is handled at the source (linked server).

更好的方法是使用OPENQUERY-- 在源(链接服务器)处处理。

Try:

尝试:

SELECT *
FROM OPENQUERY([LINKEDSERVER], 'SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01')
AND ID IN (SELECT ID FROM MY_LOCAL_VIEW) 

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.

使用这种方法,链接服务器将返回日期 > x 的所有行,然后本地服务器将通过本地表中的 ID 过滤该行。

Of course, indexing will still play a factor for doing SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01.

当然,索引仍然会在 do 中发挥作用SELECT * FROM DB.TABLE.VIEW WHERE DATE>'2012-01-01

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:

我在大型子集上使用的另一种方法是将本地 ID 转储到远程服务器,然后远程处理它,例如:

    -- 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]

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).

如果本地视图也很大,那么您可以考虑执行使用 openquery 返回本地机器的远程查询(假设远程机器有本地作为链接)。

-- 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]

回答by Rahul

Others have already suggested about indexing. So I am not going there. suggest another option, if you could change that inner query

其他人已经提出了关于索引的建议。所以我不去那里。建议另一种选择,如果您可以更改该内部查询

 SELECT * FROM LINKEDSERVER.DB.TABLE.VIEW
 WHERE DATE>'2012-01-01' AND ID IN (SELECT ID FROM MY_LOCAL_VIEW)

To a joinedquery using inner joinsince you said having 700+ inlist elements. give it a try.

要在joined使用查询inner join,因为你说有700+ INLIST元素。试一试。

   SELECT lnv.* FROM LINKEDSERVER.DB.TABLE.VIEW lnv
   inner join MY_LOCAL_VIEW mcv
   on lnv.ID = mcv.ID
   and lnv.DATE > '2012-01-01'