You are on page 1of 1

Start

The value of log_reuse_wait_desc

NOTHING
Error 9002 - the transaction log full error Currently there are one or more reusable virtual log files
and the database becomes read-only
CHECKPOINT
No checkpoint has occurred since the last log truncation, or the head of the log has not yet moved beyond a virtual log file (all recovery models)

LOG_BACKUP
/* 01. Check Log Space Usage */
A log backup is required to move the head of the log forward (full or bulk-logged recovery models only)
DBCC SQLPERF(LOGSPACE);
ACTIVE_BACKUP_OR_RESTORE
A data backup or a restore is in progress (all recovery models)

/* 02. Find the current reason why log space cannot be reused. */ ACTIVE_TRANSACTION
-- Query sys.databases again and again to examine the value of the log_reuse_wait_desc column A long-running transaction might exist at the start of the log backup/A transaction is deferred
DECLARE @DatabaseName VARCHAR(50); (A long-running or uncommitted transaction is preventing truncation of log and so space reuse)
SET @DatabaseName = 'DatabaseName'
DATABASE_MIRRORING
SELECT NAME, recovery_model_desc, log_reuse_wait_desc Database mirroring is paused, or under high-performance mode, the mirror database is significantly behind the principal database (full recovery model only)
FROM sys.databases
WHERE NAME = @DatabaseName; REPLICATION
During transactional replications, transactions relevant to the publications are still undelivered to the distribution database (full recovery model only)

DATABASE_SNAPSHOT_CREATION
A database snapshot is being created (all recovery models)

log_reuse_wait_desc
LOG_SCAN
= Log Backup ?
NO A log scan is occurring (all recovery models)
YES

/* 03. List information about the oldest open transaction in the database */ OTHER_TRANSIENT
DBCC TRACEON(3604) /* 05. Check if log backups are being taken on the database, and when the last one was taken. */ This value is currently not used
GO -- Determine when the last log backup was taken
USE msdb;
DBCC OPENTRAN; GO
GO
DECLARE @DatabaseName VARCHAR(50); The type of backupset
DBCC TRACEOFF(3604) SET @DatabaseName = 'DatabaseName'
The mode of backup GO
SELECT backup_set_id, backup_start_date, backup_finish_date, backup_size, recovery_model, [type] D - a database backup
FROM dbo.backupset L - a log backup
Simple: WHERE database_name = @DatabaseName; I - a differential backup
All transactions are written to the log. After a checkpoint, all committed transactions are
/* 04. Obtain the information to investigate log growth caused by active transactions */
automatically removed from the log, also known as truncating the log.
SELECT host_name, program_name , original_login_name, st.TEXT
No point-in-time recovery is possible.
FROM sys.dm_exec_sessions es INNER JOIN sys.dm_exec_connections ec
ON es.session_id = ec.session_id
Bulk-logged:
CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) st Available disk space >
All transactions are written to the log except certain types of minimally logged operations
WHERE ec.session_id = SPID; Transaction log ?
such as BULK INSERT and INSERT INTO.
NO
The log can only be truncated with a log backup operation.
YES
Point-in-time recovery is possible as long as no minimally logged operations are in any of the
required log backups.
Available disk space
Full: All transactions are written to the log. The log can only be truncated with a log backup > Database file ?
operation. NO YES /* 06. Execute a transaction log backup */
Point-in-time recovery is possible. USE [DatabaseName];
GO

/* 07 Execute a full database backup */


The database IS in BEGIN
USE [DatabaseName];
an Availability Group DECLARE @rec_date NVARCHAR(8)
GO
/ a Database Mirror ? DECLARE @log_bak_name NVARCHAR(100)
NO DECLARE @log_bak_to NVARCHAR(200)
BEGIN
DECLARE @rec_date NVARCHAR(8)
SET @rec_date = CONVERT(CHAR(19),GETDATE(),112);
/* 08.a Truncate the transaction log */ DECLARE @db_bak_name NVARCHAR(100)
SET @log_bak_name = N'Jail_backup_' + @rec_date;
USE [master]; DECLARE @db_bak_to NVARCHAR(200)
GO
SET @log_bak_to = N'D: \MSSQL \Backup\' + @log_bak_name + N'.trn';
-- Check if the value of auto_update_statistics_async in sys.databases off (default) to set
SET @rec_date = CONVERT(CHAR(19),GETDATE(),112)
--SELECT auto_update_statistics_async FROM sys.databases; BACKUP LOG [DatabaseName]
SET @db_bak_name = N'Jail_DB_Full_Backup_' + @rec_date
--ALTER DATABASE DatabaseName SET AUTO_UPDATE_STATISTICS_ASYNC OFF; TO DISK = @log_bak_to
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE; SET @db_bak_to = N'D:\MSSQL \Backup\' + @db_bak_name + N'.bak'
YES WITH NOFORMAT, INIT, NAME = @log_bak_name, SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 10;
GO END
ALTER DATABASE DatabaseName SET RECOVERY SIMPLE; BACKUP DATABASE [DatabaseName]
GO
GO TO DISK = @db_bak_to
WITH NOFORMAT, NOINIT, NAME = @db_bak_name, SKIP, REWIND, NOUNLOAD, COMPRESSION, STATS = 10;
/* Truncate the transaction log on CHECKPOINT */
END
USE [DatabaseName];
GO GO
CHECKPOINT;
GO
SELECT [Current LSN], [Operation]
FROM fn_dblog(NULL, NULL);
GO
/* 08.b Simply discard transaction log records. */
USE [master];
-- Perform a backup to the nul device, essentially a "black hole"
GO
ALTER DATABASE DatabaseName SET RECOVERY FULL; BACKUP LOG [DatabaseName]
GO TO DISK = 'NUL:'
ALTER DATABASE DatabaseName SET MULTI_USER WITH ROLLBACK IMMEDIATE; WITH NO_CHECKSUM, CONTINUE_AFTER_ERROR;
GO GO

/* 09.a Check Log Space Usage */


DBCC SQLPERF(LOGSPACE);

The database IS in
an Availability Group
/ a Database Mirror ?
NO YES
2

/* 10.a Shrink log file back to its initial size or an appropriate size */ /* 09.b Check Log Space Usage */
USE [master]; DBCC LogInfo
GO
-- Check if the value of auto_update_statistics_async in sys.databases off (default) to set
--SELECT auto_update_statistics_async FROM sys.databases;
--ALTER DATABASE DatabaseName SET AUTO_UPDATE_STATISTICS_ASYNC OFF;
ALTER DATABASE DatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO Status = ?
ALTER DATABASE DatabaseName SET RECOVERY SIMPLE;
0 2
NO GO

USE [DatabaseName]; /* 10.c Shrink log file back to its initial size or an appropriate size */ /* 10.b Shrink log file back to empty file */
GO --sp_helpfile --sp_helpfile
-- Shrink the log file to smallest possible size DBCC SHRINKFILE (N'LogFileName', target_size=0); DBCC SHRINKFILE (N'LogFileName', EMPTYFILE);
--sp_helpfile GO GO
DBCC SHRINKFILE (N'LogFileName', target_size=0);
GO 0
-- Resize the log file to a sensible size as well as minimize fragmentation of the log file
ALTER DATABASE [DatabaseName]
MODIFY FILE (NAME='LogFileName',SIZE='SizeMB',MAXSIZE=UNLIMITED,FILEGROWTH='GrowthSizeMB'); /* 11. Determine how much of the log file is in use */
GO USE [DatabaseName]; Status = ?
GO
USE [master];
GO SELECT name, size/128.0 AS SpaceInMB, (size/128.0 - CAST(FILEPROPERTY(name, 'SpaceUsed') AS int)/128.0) AS AvailableSpaceInMB
ALTER DATABASE DatabaseName SET RECOVERY FULL; FROM sys.database_files;
GO
ALTER DATABASE DatabaseName SET MULTI_USER WITH ROLLBACK IMMEDIATE;
GO

Does truncating and


shrinking reduce the size
of the transaction log ?
YES

End

You might also like