A database snapshot provides a read-only, static view of a source database.
Use cases:
- When performing updates on table (drop / structure changes).
- For example, before doing major updates, such as a bulk update or a schema change, create a database snapshot on the database protects data. If you make a mistake, you can use the snapshot to recover by reverting the database to the snapshot. Reverting is potentially much faster for this purpose than restoring from a backup; however, you cannot roll forward afterward.
Why? It takes very minimal disk space and restoration is fast to rollback the changes. When the databases are huge if you want to go with traditional backup and restore methods, the rollback will take hours. We can eliminate the downtime with the Snapshots.
2. Snapshots can be used for reporting purposes, but they will have the date till the time of snapshot creation only.
Note: Snapshots do not protect against disk errors or data corruption. If the source database becomes corrupt or unavailable for any reason all snapshots also become unavailable. So, snapshots are not a substitute for backups.
Testing Snapshot:
–creating a new database
Create database snaps
— creating table
use snaps
create table st (id int, dates date)
insert into st values ( 1, getdate()),(2,getdate())
create table st2 ( id int, dates datetime)
insert into st2 values ( 1, getdate()),(2,getdate())
–select data
use snaps
select * from st
go
select * from st2
— get the file info of snaps database
sp_helpdb ‘snaps’
— creating snapshot on DB
create database snapshot_snaps on
( name =’snaps’ — name is the logical filename fo the snaps.mdf file
,filename=’D:\SQLBackup\snaps.ss’)
as SNAPSHOT OF snaps; — snaps is the source db name
go
–verify the sparse by going into the properies of a snapshot file and db file
–Drop the table on snaps database
use snaps
go
drop table st
— read the sanpshot databse
use snapshot_snaps
select * from st
select * from st2 — even if the source table is not exit, we can access the table from the snapshot
— restore from the snapshot
use master
go
restore database snaps
from Database_snapshot = ‘snapshot_snaps’
— drop sanpshot
drop database snapshot_snaps