Performance impact of self-referencing Linked Server

  • I am going through a consolidation exercise where I want to put databases from one instance into another existing instance. Currently the instances are on separate hardware.

    So SERVER1\INSTANCE_A has some procs that query SERVER2\INSTANCE_A. I am going to move all databases from Server2 onto Server1 and change the linked server on SERVER1 to reference itself. I'm wondering if the optimizer is smart enough to "realize" that the linked server refers to itself and not have to wade through layers of linked server code to access the data.

    So, my question is what is the performance difference of doing

    Select * from linkedserver.database.schema.table

    as opposed to

    Select * from database.schema.table

    if the databases are on the same server? We have a lot of references to the linked server in our stored procs and views and I'm wondering if I must make the code change during the move of the datbases, or if I can wait and do it in a separate code release a few weeks later.

  • Functionally, so long as the linked server exists, you shouldn't have too much of a problem. But remember that it takes a little bit extra CPU to process those linked servers. So the more references you have to them, the longer the query execution will take. Each little bit of "extra" added to each other little bit of "extra" adds up.

    Also, in my experience, changing a linked server isn't quite so easy as opening it up and changing the server name. In my situation, I've had to delete the previous linked server and create a new one with the same name but a different connection to get it to work.

    The self-referencing linked server is very useful in non-prod environments where one is testing code that will go up to production where there actually IS a linked server (non-self-referencing). I've not noticed much of a difference with my code. But then I don't have a lot of nested views / procs / functions. Just joined tables and a couple of subqueries using the linked server. So I guess the answer really depends on your setup.

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

  • keymoo (11/17/2015)


    I am going through a consolidation exercise where I want to put databases from one instance into another existing instance. Currently the instances are on separate hardware.

    So SERVER1\INSTANCE_A has some procs that query SERVER2\INSTANCE_A. I am going to move all databases from Server2 onto Server1 and change the linked server on SERVER1 to reference itself. I'm wondering if the optimizer is smart enough to "realize" that the linked server refers to itself and not have to wade through layers of linked server code to access the data.

    So, my question is what is the performance difference of doing

    Select * from linkedserver.database.schema.table

    as opposed to

    Select * from database.schema.table

    if the databases are on the same server? We have a lot of references to the linked server in our stored procs and views and I'm wondering if I must make the code change during the move of the datbases, or if I can wait and do it in a separate code release a few weeks later.

    If the two servers are up and running, you could simply do a test.

    As a bit of a side bar, I strongly recommend NOT using 3 and 4 part naming anywhere except within a synonym or passthrough view. I do recommend using just 2 part naming that references a synonym. Think of it as an "alias".

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden (11/17/2015)


    As a bit of a side bar, I strongly recommend NOT using 3 and 4 part naming anywhere except within a synonym or passthrough view. I do recommend using just 2 part naming that references a synonym. Think of it as an "alias".

    Jeff, could you go into more detail about this, please? As in the reasons behind this recommendation?

    Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/[/url]On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply