Guide · SQL Server
Why the SQL Server transaction log grows and how to keep it under control
A 5 GB database with an 80 GB log file: a familiar sight for many companies that run SQL Server. The log does not shrink by itself and can end up filling the disk. This guide explains why the log grows, how to find the cause and how to keep it under control for good.
What the transaction log is for
Every SQL Server database has a data file (.mdf) and a transaction log (.ldf). Every change is written to the log first. SQL Server uses these records to bring the database back to a consistent state after a crash, to roll back unfinished transactions and to restore to a point in time.
The log works like a ring: once its records are no longer needed, the space can be reused. The log only grows when this reuse is blocked for some reason. So the real question is not "why is the log growing?" but "why can the log space not be reused?"
The recovery model: the most common cause
Every database has a recovery model:
- Simple: log space is reused automatically after each checkpoint. There are no log backups; you can restore only to the last full or differential backup.
- Full: log space is reused only after a log backup. In return you can restore to a specific point in time.
- Bulk-logged: like Full, with less logging for some bulk operations. Log backups are still needed.
The most common situation: the database is in the Full model, a full backup runs every night, but no log backup is ever taken. A full backup does not free log space, so the log grows a little more every day. The fix is either regular log backups (for example every 15 to 60 minutes), or, if you do not need point-in-time restores, the Simple model.
Ask SQL Server for the cause
SQL Server tells you why it cannot reuse the log space:
SELECT name, recovery_model_desc, log_reuse_wait_desc
FROM sys.databases;
The most common values of log_reuse_wait_desc:
- NOTHING: nothing blocks reuse.
- CHECKPOINT: waiting for a checkpoint; usually short.
- LOG_BACKUP: waiting for a log backup. You see this when no log backups run in the Full or Bulk-logged model.
- ACTIVE_TRANSACTION: a long-running transaction is open. A query that never finishes or an uncommitted transaction holds the log.
- ACTIVE_BACKUP_OR_RESTORE: a backup or restore is running.
- REPLICATION or AVAILABILITY_REPLICA: replication or a high-availability partner has not received the log records yet.
To see how full the log is, use DBCC SQLPERF(LOGSPACE);.
Getting it under control, step by step
- Find the cause. Check
log_reuse_wait_descwith the query above. - If it is LOG_BACKUP, set up log backups. For example:
BACKUP LOG [DatabaseName] TO DISK = N'E:\Backup\DatabaseName_log.trn';Run it regularly with a maintenance plan or a SQL Server Agent job, and make sure the backups are copied to another disk or server. - If it is ACTIVE_TRANSACTION, find the open transaction.
DBCC OPENTRAN;shows the oldest one. Find out why the application does not close it. - Once the log can be reused, shrink it once if needed. If the log grew far beyond what it will ever need again, shrink it to a sensible size:
DBCC SHRINKFILE (N'DatabaseName_log', 4096);(in MB). - Set the growth step. Growing the log by a fixed amount (for example 512 MB or 1 GB) instead of a percentage avoids a large number of small virtual log files (VLFs). Many VLFs can slow down startup and restores; on SQL Server 2016 SP2 and later you can count them with
sys.dm_db_log_info.
Why shrinking on a schedule is not a fix
A job that shrinks the log regularly looks tempting but solves nothing. If the cause remains, the log grows back to the same size, and every growth makes writes wait while SQL Server zeroes the new space. In the Full model, emptying the log without a log backup also takes away the ability to restore to a point in time. The lasting fix is to remove the cause and let the log stay at the size it needs.
Monitoring the log continuously
Log problems are usually noticed when the disk is full, with error 9002 "the transaction log is full". Secura RMM watches the log usage and the last log backup of every database, without signing in to SQL Server, from the event log and performance counters. It alerts you when log backups stop or log usage passes 90% by default. See SQL Server monitoring for details.
Frequently asked questions
Does a full backup shrink the log?
No. A full backup does not free log space. In the Full model, log space becomes reusable only after a log backup.
Is switching to the Simple model safe?
Yes, if you do not need point-in-time restores and returning to the last full or differential backup is enough. It is a business decision; first agree how much data loss you can accept.
Can I delete the log file?
No. The log file is part of the database; deleting it can leave the database unable to open. Always shrink it with the steps above.
Related: SQL Server monitoring · Password attempts on SQL Server · Alerts