Thursday, 11 March 2010

Setup Database Snapshot against a Mirrored Database (Part 2)

This post is the second post in my series about Database mirroring. For part one is titled Configuring Database Mirroring in SQL Server. I believe that high availability is the primary reason people mirror their important production databases. Depending on your situation there can be some other benefits of using mirroring that can help performance of your production database. With database mirroring in SQL Server you have two exact copies of your production database on two separate instances , wouldn’t it be nice to have access to that mirrored database to act as a read-only database supporting all your reporting requirements? This post will show how to set that up.
So we have our mirrored database all setup and its sat in a RECOVERING state, so how can we use it as reporting solution? Well, the answer lies in another feature new to SQL 2005… Database Snapshots.
You can’t run queries against your mirrored DB because it is a in a RECOVERING state but you can create a snapshot of your mirrored database and run queries against that…here’s how:
You need to get the logical file names of the data files that make up the mirrored database, these will be the same for both databases, to get these I run the following script against the principal database:
use MirrorDBtest
select * from sysfiles

The result of this query is:


So with the logical name of my data file

I run the following CREATE DATABASE statement to create a snapshot on the mirror database:

ON (NAME = 'MirrorDBtest', FILENAME = 'C:\DATA\SQLServer2008\MirrorDBTest_mirror.snp')

In the above script I specifying the location of the snapshot file on the secondary instance 

Once this script has been run you should have a fully fledged database snapshot created against the mirrored database.

You can then run queries against the snapshot:


Related Posts:

Part 1 - Configuring Database Mirroring in SQL Server.

No comments:

Post a Comment

Featured post

OUT OF SUPPORT: SQL Server 2008 and SQL Server 2008 R2

Welcome to this post, on SQL Server 2008 and 2008 R2, both of these versions of SQL server will go out of  extended support with Microsoft t...