• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

Day 1 of 31 Days of Disaster Recovery: Does DBCC Automatically Use Existing Snapshot?

January 1, 2013 1:40 pm / 24 Comments / SQLSoldier

Day 1 of 31 Days of Disaster Recovery: Does DBCC Automatically Use Existing Snapshot?

31 Days of Disaster Recovery

31 Days of Disaster Recovery

Welcome to my series on Disaster Recovery. I will spend the entire month of January focusing on all things related to disaster recovery including topics like corruption, data integrity, data loss, DBCC commands, and more.

For my first post of this month, I want to take a look at the myth that the DBCC CHECK commands will automatically use an existing database snapshot if one exists for the database. This is a myth I believed myself at one time and told to others. This was done to prove it to myself as much as to anyone else. This is an attempt to prove definitively that the DBCC CHECK commands will not use an existing snapshot for a database.

Does DBCC Automatically Use Existing Snapshot?

I was doing some bigger investigations into DBCC CHECK commands (post to come) and was looking for a way to see that hidden snapshot the DBCC CHECK commands create and use. The snapshots are not visible in sys.databases, sys.master_files, nor any other system catalog that I could find. Additionally, the snapshot creation does not trigger server level events for a DDL trigger nor the database create or database start events for SQL Trace or Extended Events.

I found it in the [i]databases_dbcc_logical_scan[/i] event via Extended Events. The event reports database_id of the database the command was run against as well as the database_id of the database where the action is actually occurring. If the action database is the hidden snapshot, the database_id will not show up in sys.databases, but the DB_NAME() function will return the name of the source database. I set up an Extended Events session to capture this event with the columns database_id and database_name. I use the ring buffer target because I don’t intend to retain any of this data. This session will not be active yet.

CREATE EVENT SESSION [TestSnap] ON SERVER 
ADD EVENT sqlserver.databases_dbcc_logical_scan(
    ACTION(sqlserver.database_id,
        sqlserver.database_name)) 
ADD TARGET package0.ring_buffer
WITH (MAX_DISPATCH_LATENCY=30 SECONDS,
    MAX_EVENT_SIZE=0 KB);

Then I create a snapshot of the AdventureWorksDW2012 database called AWSnap so there is an existing snapshot.

CREATE DATABASE AWSnap
    ON (NAME = N'AdventureWorksDW2012_Data',
        FILENAME = N'C:\bak\AdventureWorksDW2012_Data.ndf')
        AS SNAPSHOT OF AdventureWorksDW2012;

Now, using Object Explore in SQL Server Management Studio (SSMS), I start the Extended Events session (expand Management -> expand Extended Events -> right-click on the session -> click Start Session). Then I right-click on the session and click on Watch Live Data. This allows me to see the events in real-time while DBCC CHECKDB is running against the AdventureWorksDW2012 database. Next I open a new query window and run DBCC CHECKDB.

We can see by looking at any one entry in the Extended Events session that the database_id and database_id (Action) are different database IDs. If you query sys.databases, you will see that database_id of 6 is the AdventureWorksDW2012 database and there is no entry for the database_id (Action). If we check the action database_id with the DB_NAME() function while the CHECKDB is running, it will return AdventureWorksDW2012, but after DBCC completes, it will return NULL.

DBCC Scan Output

DBCC Scan Output

For the next part of the test, I close out the Live Data window and reopen it so that it is all clear again. Then I run DBCC CHECKDB against the snapshot AWSnap. checking the Live Data window, we see that the database_id and database_id (Action) are now the same value. If you query sys.databases, you will see that the value for both maps to the snapshot AWSnap.

DBCC Scan Output

DBCC Scan Output

Summary

As I have shown here, if you want to control the snapshot that DBCC uses, you need to specifically run DBCC on the snapshot itself. It won’t detect and use the snapshot just because one exists. The reason why this is important to understand will become apparent in a later post. So be sure to check back and see how I build on this information.

Posted in: SQL Server / Tagged: 31 Days of Disaster Recovery, DBCC, Disaster Recovery, Extended Events, Internals

24 Thoughts on “Day 1 of 31 Days of Disaster Recovery: Does DBCC Automatically Use Existing Snapshot?”

  1. Pingback: Day 3 of 31 Days of Disaster Recovery: Determining Files to Restore Database | SQLSoldier

  2. Pingback: Day 4 of 31 Days of Disaster Recovery: Back That Thang Up | SQLSoldier

  3. Pingback: Day 6 of 31 Days of Disaster Recovery: Dealing With Corruption in Allocation Pages | SQLSoldier

  4. Pingback: Day 7 of 31 Days of Disaster Recovery: Writing SLAa for Disaster Recover | SQLSoldier

  5. Pingback: Day 8 of 31 Days of Disaster Recovery (T-SQL Tuesday #38): Resolutions for All DBAs | SQLSoldier

  6. Pingback: Day 9 of 31 Days of Disaster Recovery: Use All the Checksums | SQLSoldier

  7. Pingback: Day 10 of 31 Days of Disaster Recovery: Monitoring for Corruption Errors | SQLSoldier

  8. Pingback: Day 11 of 31 Days of Disaster: Converting LSN Formats | SQLSoldier

  9. Pingback: Day 12 of 31 Days of Disaster Recovery: Extreme Disaster Recovery Training | SQLSoldier

  10. Pingback: Day 14 of 31 Days of Disaster Recovery: Fixing a Corrupt Tempdb | SQLSoldier

  11. Pingback: Day 15 of 31 Days of Disaster Recovery: Running DBCC CheckTable in Parallel Jobs | SQLSoldier

  12. Pingback: Day 16 of 31 Days of Disaster Recovery: Disaster Recovery Gems From Around The Net | SQLSoldier

  13. Pingback: Day 17 of 31 Days of Disaster Recovery: When is a Checksum Written to a Page | SQLSoldier

  14. Pingback: Day 17 of 31 Days of Disaster Recovery: When are Checksums Written to a Page | SQLSoldier

  15. Pingback: Day 18 of 31 Days of Disaster Recovery: How to CHECKDB Like a Boss | SQLSoldier

  16. Pingback: Day 20 of 31 Days of Disaster Recovery: The Case of the Backups That Wouldn’t Restore | SQLSoldier

  17. Pingback: Day 22 of 31 Days of Disaster Recovery: Which DBCC CHECK Commands Update Last Known Good DBCC | SQLSoldier

  18. Pingback: SQL Server | Pearltrees

  19. Pingback: Day 25 of 31 Days of Disaster Recovery: Improving Performance of Backups and Restores | SQLSoldier

  20. Pingback: Day 26 of 31 Days of Disaster Recovery: The Mysterious Case of the Long Backup | SQLSoldier

  21. Pingback: Day 27 of 31 Days of Disaster Recovery: Restoring Part of a Database | SQLSoldier

  22. Pingback: Day 28 of 31 Days of Disaster Recovery: Recovering SQL if the Tempdb Drive Dies | SQLSoldier

  23. Pingback: Day 29 of 31 Days of Disaster Recovery: Using Database Snapshots to Restore Replicated Databases in Test | SQLSoldier

  24. Pingback: Day 30 of 31 Days of Disaster Recovery (T-SQL Tuesday #40): Using Partial Availability and Initialize from Backup to Replicate a Partial Database | SQLSoldier

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this