n my series of articles, "Paranoid and Proud of It," I pointed out that systems require different backup strategies, depending on the criticality of the information stored in them. SQL Server 2000 contains a feature called "recovery models" that can help you classify your backup needs and simplify your backup plan. In this 10-Minute Solution, I explain this feature and show you how to exploit it.
What Are Recovery Models?
Although you and I might work on very different databases, when it comes down to backups, most people's requirements are met by a similar set of needs. For example, many people have development databases that they don't need to back up that often. Likewise, many of us have worked on systems where every minute of activity is important and needs to be backed up as soon as possible. For each of these situations, a different backup strategy should be applied.
In order to make backups easier to implement, Microsoft has grouped the various strategies that can be employed into three stereotypes or models: simple, full, and bulk-logged. These SQL Serverspecific models not only help you think about your needs, but also simplify the implementation of the appropriate strategy.
Understanding Transaction Logs and Bulk Copy
Before I delve into recovery models, let me first briefly review two related features of SQL Server: the transaction log and the bulk copy utility. How the recovery models treat these two features will be one of the main differences between them.
Every database must have at least one file that is used as the transaction log. When a change is made to any information in a database, the change is first written to the transaction log before the transaction is committed. The change is only written to the database itself when a "checkpoint" occurs. During a checkpoint, all committed transactions are written to the database itself. (SQL Server periodically initiates a checkpoint for each database.).
SQL Server uses the transaction log, among other things, to ensure that committed transactions are not lost due to a power failure. A transaction log also allows transactions to be aborted and rolled back before they are committed.
Think about a transaction that moves money from your checking accounting to your savings account. The transaction requires two separate actionsa debit to your checking account and a credit to your savings account. If any change were written directly to the database, a failure between the debit and the credit would result in an imbalance in your accounts. Instead, the change is first written to the transaction log. Only after both operations have been successfully recorded in the transaction log and the transaction is committed are the changes written to the database.
SQL Server comes with a utility (the bulk copy program) that is very useful when loading large amounts of data from a file into the database. One of the ways that this utility can be faster than a similar number of INSERT statements is by allowing the bulk copy program to operate in non-logged mode. In this mode, each inserted row will not be written to the transaction log, thereby speeding up the operation. (Similarly, updates to text fields can be done in either logged or non-logged mode.)
Now let's look at the various recovery models and see where these features come into play.
Exploring the Recovery Models
SQL Server 2000 has three models: simple, full, and bulk-logged. Let's look at what each model means, starting with the easiest.
Choosing the Right Model for Your Needs
Now that you understand the recovery models, it should be relatively straightforward for you to choose one for a particular situation. Ask yourself these questions.
ALTER DATABASE [database name]
SET RECOVERY [either: FULL | BULK_LOGGED | SIMPLE]In order to tell what model is being used by a particular database, execute this command: SP_HELPDB [database name]This stored procedure returns a wealth of information regarding the database. You will find the recovery model of the database under the Status column where it says, "RECOVERY=[name of model]".
| Where Have All My Options Gone? |
||
| Those of you familiar with previous versions of SQL Server might be wondering what's become of sp_dboption. Read on... | ||
For those of you who have used SQL Server in the past and were expecting to use the SP_DBOPTION command, see the sidebar on this topic.
Choosing a recovery model doesn't mean that you are stuck with your choice. On the contrary, it is easy to switch from one to another simply by issuing another ALTER DATABASE command. For example, you might choose to switch to BULK_LOGGED mode right before you perform a bulk copy in order to speed up that task and immediately after switching back to FULL mode in order to minimize the possible loss of data. Not only is the switch easy and painless, but backups can continue as scheduled.
Your Next Step
Choosing a model does not automatically generate backups for you. You still need to set up and schedule them. In a future article, I'll discuss the various types of backupsfull, differential, transaction, and file backupsthat SQL Server provides, and demonstrate how to choose among them.
If you need some immediate information on the subject you can look in Books Online in the chapter called "Backing Up and Restoring Databases."
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |