Monday, January 24, 2011


Transaction log principles
Whenever a data update is made entries are added to the transaction log.
It is not possible to prevent this as it is part of the way sql server 
maintains integrity - particularly during recovery.

The transaction log is a circular file i.e. when the end is reached any 
free entries at the start will be used.
This means that all being well the file will stay at a constant size as 
the current entry cycles round.

The system maintains the MinLSN which is a pointer to the first active log
Any log records before this (in the circular file) are free.
The MinLSN will be prevented from moving forward by any open transactions -
i.e. the oldest open transaction entry will be >= the MinLSN.
The MinLSN is updated at checkpoint so committing a transaction will not 
immediately free entries and anything that holds up the checkpoint can cause 

If the database is in simple recovery mode all entries prior to the MinLSN 
will be freed at checkpoint.
If the database is in full recovery mode (and a full backup has been taken) 
the entries prior to the MinLSN will only be freed by a transaction log 
backup (not full backup).

Common cause of large transaction log file (.ldf)

Unfortunately the sql server default (except local editions) leaves the 
databases in full recovery mode.
This menas that if no action is taken no tr log entries will be freed and 
the log file will eventally fill the disk and crash the system.
The SQL Server installation process is very simple and commonly carried out 
by inexperienced personel. This will appear to work happily but cause 
problems later.
I would recommend always setting the model database to simple recovery 
mode to set the default for new databases.

Stopping the transaction log file (.ldf) from growing

If the log file has grown do to being in full recovery mode then set it to 
simple before going any further. This should immediately stop the log from 
Enterprise manager
Right click on the database, properties, Options, set model to simple, OK.
sp_dboption [dbname], 'trunc. log on chkpt.', 'true'
Shrinking the transaction log file (.ldf)
Before this make sure there are free entries by setting the recovery model 
to simple or backing up the log.

Enterprise manager
Right click on the database, All tasks, Shrink database, Files, Select log 
file, OK.

dbcc shrinkfile ([db_log_name])
Here [db_log_name] is the logical name of the log file as found from 
sp_helpdb or the table sysfiles

Shrinking the log file via detach/attach
Always take a full backup before a detach.

Detach the database, delete/rename the log file, attach the database - 
this will create a minimum size log file.
Note that the log file must be deleted/renamed otherwise it will be 
re-used even though it is not mentioned in the attach.

Enterprise manager
Right click on the database, All tasks, Detach database, OK.
Delete/rename the disk log file.
Right click on databases, All tasks, Attach database, Select the .mdf file, 
OK, Yes (to the create new log message).

sp_detach_db [dbname]
Delete/rename the disk log file.
sp_attach_single_file_db [dbname], [filename]
where [filename] is the name of the physical data file (.mdf).