If one database is accessing tables on another database on the same SQL server through synonyms, does that incur any significant overhead to either of the databases?
Setup: We have one database (A) that contains all the data for our application. This database runs some time consuming calculations for a point in time, and then stores the output in another database (A_Reports).
Through the client application, a user can load reports hosted on SSRS. These reports connect to database A which uses synonyms to access the data stored on database A_Reports. Some reports might need some of the data from A, but that is rarely needed and only occurs under certain conditions that the report will always be aware of.
We want to keep the utilization of A low since it has all the data for the app servers and A_Reports is only used to store the calculated reports data.
I would assume that since they are on the same SQL server it doesn't matter which database is connected to. So the real question is, if we change the reports in SSRS to connect to A_Reports instead of A, will this allow reports to access the data quicker or enable database A to run its queries more efficiently?
Best Answer
SQL Server resolves
Synonyms
before it generates the query plan and it should have little to no impact.If your report runs faster on A_Reports instead of A and your synonym references A_Reports, it will run faster. If it references A, it will run slower.
I know your example is asking about cross referencing databases with synonyms and my examples below are regarding synonyms in the same DB. It should be handled identically though, as seen at this answer by Scott Hodgin. To illustrate further it would be handled the same. You will notice the use of the synonym has no impact and furthermore, you can't even notice the impact because it does not appear in the query plan or stats. (Because it happens in the bind phase and has negligible to no cost.)
Consider the experiment:
I ran each select independently and ran
DBCC FREEPROCCACHE
(DON'T DO THAT IN PRODUCTION) in between.My first experiment, I created the table as a HEAP. I saw no change in plan or performance.
Test 1: Synonym used in HEAP plan
Test 1: No synonym used in HEAP plan
The second experiment I added a primary key and clustered index, I also saw no change in plan or performance.
Test 2: Synonym used in clustered table plan
Test 2: No synonym used in clustered table plan
Last experiment I created a non-clustered index on the two column to include columns three and four as well and forced the use of an index.
Test 3: Synonym used in clustered table with non-clustered index plan
Test 3: No synonym used in clustered table with non-clustered index plan