One of the things that DBAs love to do is keep their servers running and healthy. A healthy server, after all, is your ticket to a stress free day and a full night’s sleep. Granted this not a guarantee but it sure helps make life easier.
We are always looking for the big ticket items to keep the servers tuned and purring. But from time to time, like with humans, it’s not Ebola that takes us down but the little sniffles and minor aches and pains that keep us from doing our best.
Like that tickle in the back of the throat, that makes you cough now and again, there is an occasional tickle that can bug SQL Server and cause some pain here and there. I have written about this nagging cough in the past.
The problem with this ailment is that since it is not always a cold, or always present, it is often times missed and frequently hard to diagnose. I talked about this previously as one of those things that should be checked from time to time. This is that nagging synonym* cough. You can read about it here.
That’s great, we can take a little cough syrup, fix some synonyms and feel a lot better about SQL Server in the morning.
Ever have one of those trick knees that decides to give out on you out of the blue? You might be walking up the stairs (or down the stairs) and suddenly the knee is gone and you end up flat on your face. And it could be great most of the time, it’s just that once in a while the knee decides to give a little twinge of pain, fold up and drop you to the floor.
SQL Server has a similar problem with this next one. I come across on a frequent basis, within SQL Server, an bad knee in the form of a linked server. Sure, I can hear you saying that linked servers are always bad. Fair enough! I have seen linked servers work wonderfully and most of the time they cause pain.
The type of pain with a linked server I would like to share today is around more of an edge case (like standing on the edge of the stairs with your knee about to give way).
As a good DBA would want to do, you may want to restore your databases to a test environment on a routine basis to ensure the backups worked and that you have a functioning recovery point for your databases in the event of a disaster. I was working on just that sort of thing for a client recently when I ran into this beautifully pain filled knee.
This customer had not one, not two, not even three instances of this problem. They had a glorious 492 instances of this problem. The vendor for this client decided the best thing to do would be to replicate the production database to a separate database to help offload performance. This other database happens to be on the same server (so no performance offload). While that is not the most intelligent thing to do, it is not the end of this ailment.
In addition to the replication, there were 492 views that utilized linked servers to UNION ALL data between the two databases. The data in each table between the two databases (in this case) is the same. So we have a linked server to UNION ALL this data between two databases on the same server that is replicated. Wowza!
Now, due to this linked server proliferation in the views to get to data the long way around, when restoring this database to a test environment there is a lot of cleanup work left to be done. After all, the restore of the database is only a piece of the healthy backup puzzle. You would want to test the data and the application against it.
Gratefully, this kind of cleanup can be made easy by doing a simple search and replace when querying sql_modules to find any views or stored procedures need to be updated to work in the test environment. Here is an example of such a script to help fix that problem.
SELECT OBJECT_NAME(sm.object_id) as ModName,
replace(replace(sm.definition,'create view ','GO' + char(10) + 'alter view '),'[Really Stinky Onions].','') as ModCode
FROM sys.sql_modules sm
INNER JOIN sys.objects o
on o.object_id = sm.object_id
WHERE definition like '%Really Stinky Onions%'
and o.type_desc = 'view'
Take the results from a query such as that and now, I can either change the views en masse or I could copy all of the results from the ModCode column and paste those to a new window. Using a regex (to replace all GO statements with a GO \n as shown in the pic) or something like SQL Prompt to prettify the code would be pretty easy from there to make it more useful.
Of course, this only helps address the issue with the linked servers in the views. The same problem would exist with stored procedures. It is up to the DBA to know which ones need to be changed and which should not be changed. Just understand that linked servers are there to present yet another nagging symptom to keep your server from being healthy and worse is they help keep you from being healthy (remember the lack of sleep they cause).
If you are interested, I also have this article to help you find those pesky linked servers before you start diving down the restore rabbit hole. The article will help evaluate the scope of linked server use and has a query to help identify linked servers.
*Funny afterthought is that both of these niggles that can help decrease health of your SQL Server have ties back to Linked Servers. If you read the links, you will see what I mean.