In SQL Server, should I create synonym for a table or a stored procedure?

bsayegh

If this has been answered elsewhere, please post a link to it, yell at me, and close this question. I looked around and saw similar things, but didn't find exactly what I was looking for.

I am currently writing several stored procedures that require data from another database. That database could be on another server or the same server, it just depends on the customer's network. I want to use a Synonym so that if the location of the table that I need data from changes, I can update the synonym once and not have to go back in to all of the stored procedures and update their references.

What I want to know is what the best approach is with a synonym. I read a post on SO before that said there was a performance hit when using a view or table (especially across a linked server). This may be due to SQL Server's ability to recognize indexes on tables when using synonyms. I can't find that post anymore or I would post a link to it. It was suggested that the best approach is to create a synonym for a stored procedure, and load the resulting data in to a memory or temp table.

I may not have my facts straight on that, though, and was hoping for some clarification. From what I can tell, creating and loading data in to memory tables generally accounts for a large percentage of the execution plan. Is using a stored procedure worth the extra effort of loading the data in to a table over just being able to run queries against a view or table? What is the most efficient way to get data from another database using a synonym?

Thanks!

RBarryYoung

Synonyms are just defined alias's to make redirection easier, they have no performance impact worth considering. And yes, they are advised for redirection, they do make it a lot easier.

What a synonym points to on the other hand can have a significant performance impact (this has nothing to do with the synonym itself).

Using tables and views in other databases on the same server-instance has a small impact. I've heard 10% quoted and I can fairly say that I have never observed it to be higher than that. This impact is mostly from reductions in the optimizers efficiency, as far as I can tell.

Using objects on other server-instances, whether through linked server definitions, or OpenQuery is another story entirely. These tend to be much slower, primarily because of the combined effects of MS DTC and the optimizer deciding to do almost no optimizations for the remote aspects of a query. This tends to be bearable for small queries and small remote tables, but increasingly awful the bigger the query and/or remote table is.

Most practitioners eventually decide on one of two fixes for this problem, either 1) If it is a table, then just copy the remote table rows to a local #temp table first and then query on that, or, 2) if it is more complex, then write a stored procedure on the remote server and then execute it with INSERT INTO..EXECUTE AT, to retrieve the remote info.


As for how to use/organize your synonyms, my advice would be to create a separate owner-schema in your database (with an appropriate name like [Remote]) and then put all of your Synonyms there. Then when you need to redirect, you can write a stored procedure that will automatically find all of the synonyms pointing to the old location and change them to the new location (this is how I do it). Makes it a lot easier to deal with location/name changes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

In SQL Server, should I create synonym for a table or a stored procedure?

From Dev

How to create a stored procedure on SQL server 2014 for create table operation?

From Dev

Create stored procedure on SQL Server

From Dev

How to DROP and CREATE a table in SQL Server 2016 Stored Procedure

From Dev

SQL Server : create and call stored procedure

From Dev

SQL Server : create and call stored procedure

From Dev

SQL server create stored procedure syntax error

From Dev

Create and execute stored procedure in SQL Server

From Dev

Create and execute stored procedure with parameter in SQL Server

From Dev

SQL server stored procedure return a table

From Dev

SQL Server insert table column to stored procedure

From Dev

SQL Server - create stored procedure that runs several stored procedures sequentially

From Dev

Create Stored Procedure in SQL

From Dev

SQL server : stored procedure

From Dev

How can I create a cursor from xml nodes in a stored procedure in SQL Server?

From Dev

Create table with stored procedure in Teradata

From Dev

SQL Server: trying to create view inside a stored procedure

From Dev

Create text file from stored procedure SQL Server 2008

From Dev

How to create stored procedure in C#, then *save* it to SQL Server?

From Dev

SQL Server encryption : create key inside stored procedure

From Dev

How to create a stored procedure in SQL Server Management Studio

From Dev

Create stored procedure in SQL Server from a MS Access update query

From Dev

create a stored procedure that compare all the attribut betwen them with sql server

From Dev

How to create a select, and then an update stored procedure in SQL Server 2012

From Dev

How to create dynamic parameters in SQL Server stored procedure

From Dev

SQL Server stored procedure : create increment ID in each occurrence of value

From Dev

How to create a stored procedure in SQL Server Management Studio

From Dev

How Can I Create Record and Table of That Record Within a Stored Procedure?

From Dev

SQL Server stored procedure error: Declare Table variable required

Related Related

  1. 1

    In SQL Server, should I create synonym for a table or a stored procedure?

  2. 2

    How to create a stored procedure on SQL server 2014 for create table operation?

  3. 3

    Create stored procedure on SQL Server

  4. 4

    How to DROP and CREATE a table in SQL Server 2016 Stored Procedure

  5. 5

    SQL Server : create and call stored procedure

  6. 6

    SQL Server : create and call stored procedure

  7. 7

    SQL server create stored procedure syntax error

  8. 8

    Create and execute stored procedure in SQL Server

  9. 9

    Create and execute stored procedure with parameter in SQL Server

  10. 10

    SQL server stored procedure return a table

  11. 11

    SQL Server insert table column to stored procedure

  12. 12

    SQL Server - create stored procedure that runs several stored procedures sequentially

  13. 13

    Create Stored Procedure in SQL

  14. 14

    SQL server : stored procedure

  15. 15

    How can I create a cursor from xml nodes in a stored procedure in SQL Server?

  16. 16

    Create table with stored procedure in Teradata

  17. 17

    SQL Server: trying to create view inside a stored procedure

  18. 18

    Create text file from stored procedure SQL Server 2008

  19. 19

    How to create stored procedure in C#, then *save* it to SQL Server?

  20. 20

    SQL Server encryption : create key inside stored procedure

  21. 21

    How to create a stored procedure in SQL Server Management Studio

  22. 22

    Create stored procedure in SQL Server from a MS Access update query

  23. 23

    create a stored procedure that compare all the attribut betwen them with sql server

  24. 24

    How to create a select, and then an update stored procedure in SQL Server 2012

  25. 25

    How to create dynamic parameters in SQL Server stored procedure

  26. 26

    SQL Server stored procedure : create increment ID in each occurrence of value

  27. 27

    How to create a stored procedure in SQL Server Management Studio

  28. 28

    How Can I Create Record and Table of That Record Within a Stored Procedure?

  29. 29

    SQL Server stored procedure error: Declare Table variable required

HotTag

Archive