SQL链接服务器查询非常慢

时间:2022-06-13 15:42:45

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

我正在从视图中通过链接服务器提取大量数据。我正在使用SQL Server 2012和link Server是SQL Server 2008

My select statement is

我的select语句是

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

你能给我建议一下治疗这种疼痛的其他方法吗?

Very many thanks in advance!

非常感谢您的到来!

2 个解决方案

#1


24  

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.

当您使用一个由四部分组成的名称时,例如[server].db.dbo。表,特别是在连接中,经常会通过连接将整个表复制到本地机器,这显然不是理想的。

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.

当然,索引仍然是在DB.TABLE中执行SELECT *的一个因素。视图在日期> ' 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转储到远程服务器,然后远程处理所有的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]

#2


1  

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 joined query using inner join since you said having 700+ inlist elements. give it a try.

对于使用内部连接的已连接查询,因为您说有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'

#1


24  

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.

当您使用一个由四部分组成的名称时,例如[server].db.dbo。表,特别是在连接中,经常会通过连接将整个表复制到本地机器,这显然不是理想的。

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.

当然,索引仍然是在DB.TABLE中执行SELECT *的一个因素。视图在日期> ' 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转储到远程服务器,然后远程处理所有的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]

#2


1  

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 joined query using inner join since you said having 700+ inlist elements. give it a try.

对于使用内部连接的已连接查询,因为您说有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'