What? Not WITH NORECOVERY?? |
|
Catching up on my own training this week, I've learnt a new thing. While restoring databases, the WITH STANDBY option rolls transactions back and then forward between transaction log restores. Let me explain... |
|
Lets setup a demo | |
We have a new database 'TestRestoreDB' in Full Recovery mode and a table 'TestTable' with some dummy data. |
|
Nightly Backup | |
Like good, honest DBAs, we do a full backup of our database overnight.
Just ignore that I'm backing up to the C:\ drive. It's not a fun place to do backups to. I'm using my laptop to run the demo and it's only got a C:\ drive. As a smart DBA, you'll be backing up to a much smarter location!! |
|
Someone changed Something. | |
Around 8:10am and 8:20am, someone in our company added some data to our table:
|
|
9am Transaction Log Backup | |
Because we don't trust hardware, software, users or anything else, we backup our TLogs hourly
|
|
More Changes | |
What do you know, those darn users keep changing things. But this time, the user's transaction doesn't complete before we take our hourly TLog backup
|
|
10am Transaction Log Backup | |
Our hourly TLog backup runs on time
|
|
Transaction Completes | |
A split-second later, the user's transaction completes
|
|
11am Transaction Log Backup | |
Just for completeness, our hourly TLog backup runs again
|
|
Where We're At | |
|
|
Disaster Strikes!!! | |
Some sysadmin buffoon decides the core business database isn't worth the storage it's sitting on, and deletes it. Who in their right mind would delete a database called TestRestoreDB??
|
|
Let's RECOVER this Situation | |
We have a couple of options: WITH NORECOVERY or WITH STANDBY between TLog restores. What's the difference? Most notably, a Transaction Log restore WITH NORECOVERY leaves the database unreadable, WITH STANDBY leaves the database READ-ONLY between restores. If you need to roll forward through Transaction Log backups to find when a user started deleting a bunch of data from your database and the best estimates they can give you are: 'sometime yesterday', WITH STANDBY becomes a good option. You can check the database after each restore to narrow down the time frame you're looking at. Lets look at both restores... |
|
WITH NORECOVERY | |
Can we see our missing data?
What State is the database in?
We can see that the database is unreadable. The database is also showing as 'RESTORING'. Our only option is to complete the restore process:
Once we have issued the WITH RECOVERY option on our last restore, we can now read the data. |
|
WITH STANDBY | |
Can we see our missing data?
What State is the database in?
Can we change data?
We can see that the database is READ-ONLY. The database is also showing as 'ONLINE'. If the data we're looking for isn't in this restore we can try the next TLog restore:
Still not the data we're looking for? Lets restore the next TLog backup.
Whoa!!! We're missing 931 and 932. They were inserted, although the transaction wasn't committed. Turns out, the uncommitted transaction was rolled into the STANDBY file ready for the COMMIT in the the next TLog restore.
And there they are. Just for completeness, lets finish our restore WITH RECOVERY to make the database READ-WRITE again and confirm the database status.
And everything's looking good in the world again. |
|
ConclusionWhen you restore WITH STANDBY, you won't see uncommitted transactions in the READ-ONLY version of the database. When you restore the next Transaction Log backup in turn, those committed transactions get rolled forwards. A restore WITH NORECOVERY doesn't have to deal with uncommitted transactions so may provide a faster recovery time if you know where you need to stop. Especially if you have long-running or large transactions. |
|
AddendumI completed this blog post, and then started thinking... Can you mix and match WITH NORECOVERY and WITH STANDBY restores if you know roughly when you need to find your missing data? Yes you can!
Nice! |