patternsqlMinor
Can I shrink the transaction log file on a mirror database?
Viewed 0 times
canthefilelogmirrorshrinkdatabasetransaction
Problem
This is a followup question to a previous question about why I couldn't shrink the log file on the principal database.
To make a long story short, I setup database mirroring but forgot to make sure the job that backed up the transaction log was running again, and the transaction log grew to almost 60GB.
Since mirroring was set up, this size increase was duplicated on the mirrored server, and eventually took up all the disk space and made the mirror database unusable.
Per this question about transaction log maintenance on a mirror database, you can't back up the log on the mirror, but when asked specifically in comments about how to shrink an overgrown log file on a mirror database, a comment was left that
One way to do this would be to failover to the mirrored database and
do the shrink there. Thoroughly test this out in a non-production
environment to ensure it has the behavior you want/expect.
This seems to suggest that there might be other ways to shrink the log file on a mirror, and this method might not necessarily be safe to do on a production server.
Is there a way to safely shrink the transaction log file on a database mirror?
To make a long story short, I setup database mirroring but forgot to make sure the job that backed up the transaction log was running again, and the transaction log grew to almost 60GB.
Since mirroring was set up, this size increase was duplicated on the mirrored server, and eventually took up all the disk space and made the mirror database unusable.
Per this question about transaction log maintenance on a mirror database, you can't back up the log on the mirror, but when asked specifically in comments about how to shrink an overgrown log file on a mirror database, a comment was left that
One way to do this would be to failover to the mirrored database and
do the shrink there. Thoroughly test this out in a non-production
environment to ensure it has the behavior you want/expect.
This seems to suggest that there might be other ways to shrink the log file on a mirror, and this method might not necessarily be safe to do on a production server.
Is there a way to safely shrink the transaction log file on a database mirror?
Solution
The
Create a sample database on the principal:
Create the same database from a backup with
Setup your mirroring session however which way you choose.
On the principal database look at the database file sizes:
My result set looks like this following:
On the mirror database, create a snapshot and look at the same information:
My result set looks like the following:
Now grow the transaction log file on the principal database (I brought it to 1 GB):
Looking at the principal database's transaction log size, we now see the adjusted size:
My result set looks like the following:
Create another snapshot on the mirrored database, and look at the transaction log file size there:
My result set looks like the following:
Now do the
My result set is the following:
Create a third and final snapshot on the mirrored database, and look at the size:
And I get the following result set:
So as you can see here, the
DBCC SHRINKFILE command will be mirrored from the principal to the mirrored database. Here's some proof.Create a sample database on the principal:
create database MirroredDb;
goCreate the same database from a backup with
NORECOVERY:restore database MirroredDb
from disk = '\\backupdir\MirroredDb.bak'
with norecovery;
goSetup your mirroring session however which way you choose.
On the principal database look at the database file sizes:
use MirroredDb;
go
select
name,
size
from sys.database_files;My result set looks like this following:
name size
MirroredDb 392
MirroredDb_log 104On the mirror database, create a snapshot and look at the same information:
create database MirroredDbss
on
(
name = 'MirroredDb',
filename = 'c:\sqlserver\MirroedDb.ss'
)as snapshot of MirroredDb;
use MirroredDbss;
go
select
name,
size
from sys.database_files;My result set looks like the following:
name size
MirroredDb 392
MirroredDb_log 104Now grow the transaction log file on the principal database (I brought it to 1 GB):
alter database MirroredDb
modify file
(
name = MirroredDb_log,
size = 1GB
);
goLooking at the principal database's transaction log size, we now see the adjusted size:
use MirroredDb;
go
select
name,
size
from sys.database_files;My result set looks like the following:
name size
MirroredDb 392
MirroredDb_log 131072Create another snapshot on the mirrored database, and look at the transaction log file size there:
create database MirroredDbss2
on
(
name = 'MirroredDb',
filename = 'c:\sqlserver\MirroedDb2.ss'
)as snapshot of MirroredDb;
use MirroredDbss2;
go
select
name,
size
from sys.database_files;My result set looks like the following:
name size
MirroredDb 392
MirroredDb_log 131072Now do the
DBCC SHRINKFILE on the principal:use MirroredDb;
go
dbcc shrinkfile('MirroredDb_log', 0);
go
select
name,
size
from sys.database_files;My result set is the following:
name size
MirroredDb 392
MirroredDb_log 104Create a third and final snapshot on the mirrored database, and look at the size:
create database MirroredDbss3
on
(
name = 'MirroredDb',
filename = 'c:\sqlserver\MirroedDb3.ss'
)as snapshot of MirroredDb;
use MirroredDbss3;
go
select
name,
size
from sys.database_files;And I get the following result set:
name size
MirroredDb 392
MirroredDb_log 104So as you can see here, the
DBCC SHRINKFILE command is in fact mirrored to the mirror database.Code Snippets
create database MirroredDb;
gorestore database MirroredDb
from disk = '\\backupdir\MirroredDb.bak'
with norecovery;
gouse MirroredDb;
go
select
name,
size
from sys.database_files;name size
MirroredDb 392
MirroredDb_log 104create database MirroredDbss
on
(
name = 'MirroredDb',
filename = 'c:\sqlserver\MirroedDb.ss'
)as snapshot of MirroredDb;
use MirroredDbss;
go
select
name,
size
from sys.database_files;Context
StackExchange Database Administrators Q#40609, answer score: 7
Revisions (0)
No revisions yet.