• @SixStringSQL (8/14/2014)


    Thanks for the replies, all...

    Question: Is there no benefit of using synonyms over renaming the base tables themselves?

    I like Synonyms for this type of thing for several reasons.

    Let's consider this...

    The table will be 100% loaded every day, and 95% of the data will be unchanged from the day before (lack of PKs on the source system, etc, necessitates this).

    If the data is completely rebuilt, what's happening to the log file? Chances are that your DW (or whatever) database is in the FULL recovery mode and you have Point-in-Time backups being done. That could be a shedload of data that you're backing up every day... data that can easily be reconstructed and doesn't actually need for log file backups on the construction of said tables. With that in mind, putting the data on a different database that uses simple recovery would be the way to go. You wouldn't even have to backup that new database.

    Then, there's the subject of Disaster Recovery. Making a gross understatement, the less data you have in a database, the faster you can recover it using a restore. For such a "swapping" system, one would be tempted to drop the "old" table when the "new" table is ready to help in this area but then you lose the ability to compare the "old" with the "new" for reporting as some would have you do.

    To solve all of those problems, I actually have 3 tables... "old", "new", and "empty". "Old" and "new" are in a separate database with the SIMPLE recovery model. None of that data needs to actually live in the main database so I save on backup time and restore time. The "empty" table lives in the main database and would be used during a disaster recovery if all hell broke loose. I could be recovering the main database and rebuilding the data in the other database at the same time. The main database would restore much more quickly than if all of this data were in just the main database. If the main database comes online before I manage to rebuild the data in the "new" table, then I just point the Synonym to the "empty" table in the main database and I'm back online for any functionality that doesn't use the "new" table. As soon as the rebuild process is done on the other database, it'll flop the Synonym and Bob's your uncle. You can't do that with renaming a table.

    Then there's the idea of expansion vs cost. SAN disks are relatively expensive creatures and having two copies of the same large tables (which can easily be rebuilt, require no backups, etc, etc) is a waste of valuable and sometimes expensive hardware. We made that realization and moved a lot of such data to a much less iSCSI system. People thought I was a little crazy when I insisted the reference to the "old" and "new" tables also be done using synonyms but they didn't think so when we did the move. There was zero downtime and zero code changes because I simply repointed those synonyms to the newly named tables (while the old tables were still in service and working) on the new drives, did the data rebuild like we normally would, and we were done!

    You just can't do that with simple table renames... not without changing some code. How often do things like that happen? Maybe never. But, if it ever does happen, it's bloody simple and it didn't take any extra time to write the initial code for.

    --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)