{"id":5618,"date":"2016-03-31T13:20:45","date_gmt":"2016-03-31T05:20:45","guid":{"rendered":"http:\/\/rmohan.com\/?p=5618"},"modified":"2016-03-31T13:20:45","modified_gmt":"2016-03-31T05:20:45","slug":"snapshot-database-in-sql-server","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5618","title":{"rendered":"Snapshot database in SQL Server"},"content":{"rendered":"<p>What is a database snapshot ?<\/p>\n<p>Database snapshots are an Enterprise only feature which was introduced \u00a0in SQL Server 2005.A database snapshot is a view of what the source database looked like at the time when the<br \/>\nsnapshot was created.<\/p>\n<p>How does a snapshot work:-<\/p>\n<p>a. When you create a snapshot a sparse file is created for each data file.<br \/>\nb.When data is modified in the source database for the first time, the old value of the modified data is copied to the sparse file.<br \/>\nc.If the same data is the subsequently changed again, those changes will be ignored and not copied to the snapshot.<\/p>\n<p>How to create a snapshot?<\/p>\n<p>USE master<br \/>\nGO<br \/>\n&#8212; Create a source Database<br \/>\nCREATE DATABASE sourcedb<br \/>\nGO<br \/>\nUSE sourcedb<br \/>\nGO<br \/>\n&#8212; Populate sourcedb Database with some Table<br \/>\nCREATE TABLE Employee (ID INT, Value VARCHAR(10))<br \/>\nINSERT INTO Employee VALUES(1, &#8216;First&#8217;);<br \/>\nINSERT INTO Employee VALUES(2, &#8216;Second&#8217;);<br \/>\nINSERT INTO Employee VALUES(3, &#8216;Third&#8217;);<br \/>\nINSERT INTO Employee VALUES(4, &#8216;Fourth&#8217;);<br \/>\nGO<br \/>\n&#8212; Now Create Snapshot Database<br \/>\nCREATE DATABASE SnapshotDB ON<br \/>\n(Name =&#8217;sourcedb&#8217;,<br \/>\nFileName=&#8217;E:\\snapshotdb.ss1&#8242;)<br \/>\nAS SNAPSHOT OF sourcedb;<br \/>\nGO<br \/>\n&#8212; Select from source and Snapshot Database<br \/>\nSELECT * FROM sourcedb.dbo.Employee;<br \/>\nSELECT * FROM SnapshotDB.dbo.Employee;<br \/>\nGO<\/p>\n<p>ID Value<br \/>\n1 First<br \/>\n2 Second<br \/>\n3 Third<br \/>\n4 Fourth<\/p>\n<p>ID Value<br \/>\n1 First<br \/>\n2 Second<br \/>\n3 Third<br \/>\n4 Fourth<\/p>\n<p>Now lets do some dml activity on source db :-<br \/>\n&#8212; Delete from sourcedb Database<br \/>\nDELETE FROM sourcedb.dbo.employee;<br \/>\nGO<\/p>\n<p>ID Value<\/p>\n<p>ID Value<br \/>\n1 First<br \/>\n2 Second<br \/>\n3 Third<br \/>\n4 Fourth<\/p>\n<p>So we can see the rows have been deleted from the sourcedb but the snapshot db is totally intact as it is.<\/p>\n<p>Now, incase if we want to undo all the changes we did, we can revert back the database from the snapshotdb which was created before doing the activity.<\/p>\n<p>&#8212; Restore Data from Snapshotdb Database<br \/>\nUSE master<br \/>\nGO<br \/>\nRESTORE DATABASE sourcedb<br \/>\nFROM DATABASE_SNAPSHOT = &#8216;Snapshotdb&#8217;;<br \/>\nGO<br \/>\n&#8212; Select from sourcedb and Snapshotdb Database<br \/>\nSELECT * FROM sourcedb.dbo.employee;<br \/>\nSELECT * FROM Snapshotdb.dbo.employee;<br \/>\nGO<\/p>\n<p>ID Value<br \/>\n1 First<br \/>\n2 Second<br \/>\n3 Third<br \/>\n4 Fourth<\/p>\n<p>ID Value<br \/>\n1 First<br \/>\n2 Second<br \/>\n3 Third<br \/>\n4 Fourth<\/p>\n<p>So we see all the datas are restored to the point when the snapshotdb was created.<\/p>\n<p>Benefits of Database Snapshots:-<\/p>\n<p>1.Snapshots can be used for reporting purposes.<br \/>\n2.Maintaining historical data for report generation.<br \/>\n3.Using a mirror database that you are maintaining for availability purposes to offload reporting.<br \/>\n4.In the event of a user error on a source database, you can revert the source database to the state it was in when a given database snapshot was created. For example, before doing<br \/>\nlarge dml activity, usually create a snapshot of the original db prior to the dml activity being done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a database snapshot ?<\/p>\n<p>Database snapshots are an Enterprise only feature which was introduced in SQL Server 2005.A database snapshot is a view of what the source database looked like at the time when the snapshot was created.<\/p>\n<p>How does a snapshot work:-<\/p>\n<p>a. When you create a snapshot a sparse file is [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5618"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5618"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5618\/revisions"}],"predecessor-version":[{"id":5619,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5618\/revisions\/5619"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}