Database Files – Part 3

DATABASE SNAPSHOTS were added to the SQL Server product in the 2005 version. They are great for saving a version of the database before major data changes happen and perfect for rolling back the changes if they are not successful. While books online suggest other uses, I think this one is the best. Please see the limitations of snapshots before creating one. I am going to explain how to create a database snapshot, how to revert back to a snapshot after corrupted occurs, and how to drop a database snapshot.

A SNAPSHOT uses the NTFS SPARSE file technology. Basically, snapshot data files are empty upon creation and grown in increments of 64 KB. When changes such as UPDATES or DELETES happen to the data, the original data is written to the snapshot before applying the change to the database. The size of the SNAPSHOT is at most the size of the database at the time the snapshot is taken. As a convention, a *.ss extension is used for snapshot data files.

The CREATE DATABASE, RESTORE DATABASE, and DROP DATABASE statements are used perform operations with SNAPSHOTS.

This article assumes the [WILDLIFE] database has been built using the scripts in either Part 1 or Part 2. We are going to create a sub-directory to hold the new snapshot data files. Snapshots are based on the original data files. The below snippet creates a SNAPSHOT of the [WILDLIFE] database.

I can not stress enough that SNAPSHOTS should not be used for point in time recovery. They are great precaution before execution a major ETL process. If the ETL process fails, the SNAPSHOT can be used to revert the database. SNAPSHOTS are quicker than a RESTORE process since they keep track of the changes only. A 1 TB database that is corrupted after 1 GB of ETL data changes has alot less data to restore than using a full recovery strategy.

The following snippet deletes odd number entries in the ANIMALS table. This is a simple example of a ETL process gone bad.

Last but not least, the SNAPSHOT can be used to revert the database to a state before the unwanted DELETE operation.

To clean up after the ETL process, use the DROP DATABASE command to remove the SNAPSHOT.

In summary, DATABASE SNAPSHOTS can be used to prevent unwanted changes to the database during major ETL updates. They should not be used in place of normal FULL, DIFFERENTIAL, and LOG backups. Snapshots will not get you that point in time recovery that is needed by most business lines.

Enclosed is the complete sample SQL script.

Related posts

Leave a Comment