Database Snapshots in SQL Server

Ever wanted to make a read-only point-in-time copyrestoring from a snapshot renders all other snapshots
of a database, and wondered which technique to use?unusable - they should be deleted and re-created if
Microsoft SQL Server 2005 provides a plethora ofrequired.
ways to do this, including database backup/restore,How do Database Snapshots work?
database detach/re-attach, log-shipping, replication,A Database Snapshot looks like an ordinary read-only
mirroring, and so forth. However, one method availabledatabase, from the user's point of view; it can be
in the Enterprise edition, the Database Snapshot, isaccessed with a USE statement, and can be browsed
new to SQL Server 2005, and is worth taking a closerfrom within Management Studio. However, it initially
look.occupies almost no disk space, and so can be created
Why are Database Snapshots Useful?almost instantly. This magic is achieved via an NTFS
There are many applications where a point-in-timefeature, sparse files. A sparse file is a file that may
snapshot is useful. Microsoft suggest the following useappear to be large, but in fact only occupies a portion
cases:of the physical space allocated to it.
- Reporting up to a specific time period, ignoring laterNow, because a database snapshot presents a
dataread-only view of your source database, it need not
- Reporting against mirror or standby databases thatstore a copy of every page. Instead, SQL Server
are otherwise unavailableperforms a copy-on-write operation; in the source
- Insuring against user or administrator error, providing adatabase, the first time a data page changes after the
quick way to revert to an older version of thecreation of a snapshot, a copy of the original page is
databaseplaced in the sparse file. The snapshot serves data
- Managing test databases, particularly during rapidfrom the snapshot copies where source data has
feature and schema development Of course, thesechanged, and the original source pages when they are
needs could be served by a database backup orunchanged.
attached copy of a database, but the key benefit ofBest Practices
choosing a snapshot over one of the other methods isSometimes you will choose a copy of a backup over
simple: creating a database snapshot is fast.a snapshot, sometimes it'll be a detached copy of the
Creating and Using Database Snapshotsdata file. However, for many situations your best bet is
Creating database snapshots is easy - it's a CREATEa database snapshot, so it's worth keeping some
DATABASE statement, specifying only the logical andpoints in mind. In particular:
physical filenames. Remember it's a read-only- The file size will look considerably larger than the
snapshot, so we don't need to add autogrowth orspace it consumes on disk, and should be clearly
transaction log settings. Here's the code:marked as a snapshot for this reason. Use explicit
CREATE DATABASEnaming conventions to make it clear to administrators.
AdventureWorks_Snapshot_Monday ON- Snapshots are at their best when young and fresh,
(and don't take up too much space. If you need to
NAME=AdventureWorks_Data,keep a snapshot for any length of time, consider using
FILENAME='C:SnapshotsAD2K_Monday.ss'another method to create your read-only copies.
)- As snapshots persist until deleted, you will need to
AS SNAPSHOT OF AdventureWorksexplicitly rotate snapshots, either manually or with a
Snapshot creation is not supported by the Objectscript.
Explorer interface in Management Studio; you must- Performing index operations such as
use a CREATE DATABASE statement as above,defragmentation or index rebuilding will modify so many
with the AS SNAPSHOT OF clause indicating thepages that the snapshot will likely contain a complete
source database. Also, note that only the Enterprisecopy of the source data for that index. The more
edition of SQL Server 2005 supports databasesnapshots there are, the more copies will exist.
snapshots.- If the disk containing a snapshot fills up, and a page
The snapshot contains a version of the data as itwrite fails, the snapshot will become useless, as it will
existed at its creation, having rolled back uncommittednot contain all necessary pages. Make sure the disk
transactions. This means that otherwise unavailablecan't fill up! Database snapshots are a worthwhile
databases, such as mirrors and standby servers, canaddition to the arsenal of any SQL Server DBA, and fit
be used to create snapshots.well with other techniques, particularly when you may
Having created a snapshot, you can now use it as youneed to quickly revert a database, or if you need to
would any other read-only database; all objects aremaintain rolling snapshots. Remember the key
exposed in exactly the same way, via Objectadvantages: high speed and low physical size. But also
Explorer, scripts, or reporting tools.remember that these advantages diminish as the
Reverting a database to the version stored in thesnapshot ages and grows, and if the number of
snapshot is similarly easy:snapshots increases.
RESTORE DATABASE AdventureWorks FROMAbove all, database snapshots are fast and easy to
ot_Monday'use; it won't cost you anything to try them out, and you
This returns the database to the state it was in whenwill probably find them very useful indeed. If all you
the snapshot was created, minus any uncommittedneed to do with a point-in-time copy is select from it, or
transactions - remember that a snapshot ispossibly revert to it, then a database snapshot is likely
transactionally consistent at its creation. Note thatthe best choice available.