Its essential that a DBA understands what happens when a snapshot is created. The following process happens,
Database snapshots operate at the data-page level. Before a page of the source database is modified for the first time, the original page is copied from the source database to the snapshot. This process is called a copy-on-write operation. The snapshot stores the original page, preserving the data records as they existed when the snapshot was created.
To store the copied original pages, the snapshot uses one or more sparse files. Initially, a sparse file is an essentially empty file that contains no user data and has not yet been allocated disk space for user data. As more and more pages are updated in the source database, the size of the file grows. As data is written to the sparse file, NTFS allocates disk space gradually. Potentially, a sparse file can grow very large. The source database, however, is not affected; actions on it continue normally. Sparse files grow in 64-kilobyte (KB) increments; thus, the size of a sparse file on disk is always a multiple of 64 KB.
This is illustrated with the help of this diagram:
1. At first when the snapshot is created, the sparse file is empty.
2. When the first update operation happens, the original data is copied into the sparse file.
The database engine then updates the data in the source database. This process is called Copy-on-write-operation.
During the read operation on the snapshot, the original data is read from the source database. For the data which has been changed, the snapshots read from the sparse file.
If any uncommitted transactions are in progress in the database @ the time when the snapshot was created, they will not be reflected in the snapshot.
Disk Space consumed by Sparse File :
You can find this by two methods,
1.Right click the file and select properties and choose general tab and view the size on disk which is 48 kb. Refer the below figure,
2. Another method is using fn_virtualfilestats function as follows,
Here I specified Null for the function fn_virtualfilestats in order to obtain information for all the files in the database. The BytesonDisk column will return the disk space consumed by sparse file in bytes. Here it is 49152 bytes = 49152/8192 = 6 Pages=48 kb.Compare the output of 1 and 2 both will same as 48 kb.
The maximum size to which a sparse can grow is the size of the corresponding source database file at the time of the snapshot creation. You can view this by two methods,
- Right click the file and select properties and choose general tab and view the size value which is 6 Mb (6291456 bytes).
- You can make use of the catalog view sys.master_files and view the size column it will be in pages. In my case it is 768 pages = 768*8192 = 6291456 bytes. On comparing the output of 1 and 2 both are same as 6291456 bytes.