Recover Data Using Database Snapshots
Now in order to get the deleted records back in Address table you can choose any one option among the two mentioned. One will be to restore the SourceDB database from the database snapshot as you don’t have full backup available for the database. The second option will be to use INSERT INTO … SELECT option to repopulate the Address table from the database snapshot. Now let us see both the options in detail.
Execute the below mentioned TSQL code to restore SourceDB database from the SourceDB_Snapshot database snapshot.
RESTORE DATABASE SourceDB
FROM DATABASE_SNAPSHOT =’SourceDB_Snapshot’
Execute the below mentioned TSQL code to populate Address table records from Database Snapshot using
INSERT INTO … SELECT option.
INSERT INTO SourceDB.dbo.Address
SELECT * FROM SourceDB_Snapshot.dbo.Address
Dropping Database Snapshot
Database Administrators can remove a database snapshot either by using the DROP DATABASE command or by right clicking the SourceDB_Snapshot and selecting the delete option in SQL Server Management Studio as shown in the below snippet.
Execute the below TSQL code to drop the SourceDB_Snapshot database snapshot.
DROP DATABASE SourceDB_Snapshot
Advantages of Database Snapshots
- They are very easy to create and take very less time when compared to taking the Full database backups.
- They require very less disk space when compared to Full database backups, However database backups are always a better option.
- It is a static, read-only copy of user database at a given point in time, all the uncommented transactions are rolled back to make the snapshot transactionally consistent at the given time.
- They basically contain the copy of data pages prior to the data modification in the source database.
- Reports can be run against database snapshots, the users will not experience blocking because of update / insert operations when they are connected to database snapshots which they normally experience when connected to source database.
- Database Administrators can create database snapshot for historical data
- Database Administrators can easily recover the corrupted or deleted data from the database snapshot while repairing the primary database
- Test team can quickly revert to old state of database by quickly restoring the database from database snapshots.
- You can create database snapshots for all the user databases irrespective of the size of the database.
Disadvantages of Database Snapshots
- You won’t be able to detach or attach the snapshot database.
- You won’t be able to backup or restore snapshot database.
- Both source database and database snapshot should exist on the same server.
- Full Text indexes are not supported on database snapshots.
- It is not possible to add or drop any objects in the snapshot database as it is a read-only database.
- Database snapshot feature is only available in Enterprise Edition of SQL Server 2005 & SQL Server 2008.
- If your source database is not available by any chance then database snapshots are of no use. Queries which are run against snapshots are indirectly getting the data from the actual source database.
- If there are lot of changes suppose to happen in source database for which database snapshot is created then the size of the snapshot will grow quickly and in that case the best option will be to take full database backups.
- This can also affect the performance as the data pages are copied to database snapshots whenever changes are done to the existing data in the source database.
- You cannot create a database snapshot against any of the system databases.
Database Snapshots are an excellent feature which was introduced in SQL Server 2005. Database Administrators can take database snapshot prior to making any structural changes to a user database. However, you need to understand that database snapshots are not a replacement to database backups, at any point in time database backups are always a better option. If there are any issues encountered by a DBA while making structural changes then by restoring a database from a database snapshot you can quickly revert to aprevious version of the database. I hope in future releases of SQL Server that this feature will be made available in all editions of SQL Server.