简体   繁体   中英

SQL Server Linked Server Performance plausible solution?

I am in the process to optimize a view's performance which collect data on 2 linked servers objects.

So the view is on server A. The view selects data from server B and C.

When I opened the script I noticed this view is a total mess querying multiple times the same tables over and over using sub sub sub sub selects...

To keep things simple, I will not post the actual code of the query (700+ lines) but I will create a simple query for your understanding:

      SELECT    *
      FROM      [ServerA].myDB.dbo.tableA a
                INNER JOIN [ServerA].myDB.dbo.tableB b ON a.field = b.field
                LEFT JOIN ( SELECT  SUM(field) AS mysum
                            FROM    [ServerA].myDB.dbo.tableB
                            WHERE   field IN ( SELECT   MAX(value)
                                               FROM     [ServerA].myDB.dbo.tableA
                                               WHERE    anotherField IS NOT NULL )
                          ) tbl ON tbl.mysum < b.anotherField

I know this query don't make any sense.. This is just to illustrate my point.

Would it be more performant to create a view in myDB on server A to query the tables and from Server B using a SELECT * FROM ServerA.myDB.myLocalView ?

CREATE VIEW myLocalView
AS
SELECT  *
FROM    dbo.tableA a
        INNER JOIN dbo.tableB b ON a.field = b.field
        LEFT JOIN ( SELECT  SUM(field) AS mysum
                    FROM    dbo.tableB
                    WHERE   field IN ( SELECT   MAX(value)
                                       FROM     dbo.tableA
                                       WHERE    anotherField IS NOT NULL )
                  ) tbl ON tbl.mysum < b.anotherField

A good solution here in my opinion would be to query the data you need and drop it into a temp table and then do the join between the temp table on the same server.

https://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/

By pulling the data over first, and then doing the join locally, you'll see a huge improvement in performance. Trying to join from server to server is very slow due to the network lag during the join.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM