Tech Note 976 - SQL Server Transaction Log Not Truncating
Tech Note 976 - SQL Server Transaction Log Not Truncating
All Tech Notes, Tech Alerts and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.
Topic#: 002801
Created: August 2013
Introduction
It is possible that SQL Server leaves open transactions. When this happens, the Transaction Log will grow until the disk is full.
If you are using Full Recovery mode, the backup procedure should shrink the transaction log. Note that the file itself does not get
smaller, but its space is freed up. See the following procedure for details on how to determine this.
This Tech Note describes the procedure for fixing or freeing space in the SQL Server Transaction Log.
Application Versions
Microsoft SQL Server: All supported versions
The examples in the this Tech Note use the MESDB database with a transaction log named MESDB_LOG. You will have to update these
names in the scripts with the name of your database and transaction log.
You can get the name of the transaction log by clicking on the database name, and click Properties/Files. It is the Logical Name for
the File Type=Log.
Note: See Tech Note 930 Wonderware Application Server Clean-up Guide for information about optimizing your Application Server
system.
Diagnostic Procedure
Complete the following tasks in the order presented for a comprehensive diagnostic procedure. When a task is successful, proceed to the
Cleaning Up section.
Note: The Production database might be running transactions at this time. The problem comes in when old transactions never get closed
or the transaction log thinks it is open. Run the query several times if you see an active transaction. You can also use Activity Monitor
to see what is going on.
If you conclude it is an old transaction, use Activity Monitor to kill the associated process.
DBCC SQLPERF('LOGSPACE') ;
This will return the Log Space Used (%). If the percentage is high ( >80% ) and the Log Size (MB) is high ( > 1000 MB ) you
have a problem.
Check the log space again by running the DBCC SQLPERF query (above). If the % used is low, skip to the Cleaning Up section.
Check the log space again and if the % used is low or the file size is small, skip to the Cleaning Up section.
You should also do another backup as some of the logs where deleted.
References
http://social.technet.microsoft.com/Forums/pt-BR/sqlreplication/thread/34ab68ad-706d-43c4-8def-38c09e3bfc3b
http://www.eggheadcafe.com/conversation.aspx?messageid=34020486&threadid=33890705
Warning: DO NOT do the following procedure during production. This may cause you to lose your DB or it will be recovered in
a bad state!
USE MESDB;
GO
EXEC sp_HelpFile;
1. I:\SQL\FactelligenceProd.mdf
2. I:\SQL\FactelligenceProd_log.ldf
3. I:\SQL\FactelligenceProd_Data.ndf
4. I:\SQL\FactelligenceProd_Data_2.ndf
5. I:\SQL\FactelligenceProd_Index.ndf
6. I:\SQL\FactelligenceProd_Index_2.ndf
7. I:\SQL\FactelligenceProd_Large.ndf
8. I:\SQL\FactelligenceProd_Large_Index.ndf
You can use SQLServer Management Studio or run the following command.
3. Stop all applications connected to the Database. For IAS put the Engines Off Scan.
-- Detach the DB
EXEC sp_detach_db MESDB
6. Find the transaction log file and delete it. It should be the second file in the list above, with the extension ldf.
7. Reattach the DB but don't specify the log file name. It will be created.
EXEC sp_attach_DB
@dbname='MESDB'
, @fileName1='I:\SQL\FactelligenceProd.mdf'
--I:\SQL\FactelligenceProd_log.ldf
, @fileName2='I:\SQL\FactelligenceProd_Data.ndf'
, @fileName3='I:\SQL\FactelligenceProd_Data_2.ndf'
, @fileName4='I:\SQL\FactelligenceProd_Index.ndf'
, @fileName5='I:\SQL\FactelligenceProd_Index_2.ndf'
, @fileName6='I:\SQL\FactelligenceProd_Large.ndf'
, @fileName7='I:\SQL\FactelligenceProd_Large_Index.ndf'
GO
8. Reset to multi-user:
USE Master;
GO
ALTER DATABASE MESDB
SET MULTI_USER;
Cleaning Up
Wonderware Technical Support does NOT recommend scheduling tasks to shrink databases or files. Doing so will result in file
fragmentation.
Instead, configure the database and log settings to reasonable sizes, with a good constant growth parameter.
For the log, an initial size of 500 MB and a growth of 250 MB is recommended.
Hopefully you will never need any more extents and everything will fit into the 500 MB.
Set up the Initial Size (MB) and Auto growth by clicking on the DB name, Properties/Files:
Cleanup Procedure
1. Shrink the DB.
3. Finally do a check on the DB. This is "safe" during production but it can slow down the system. Some prefer to run on off hours
(see the following note on CHECKDB):
In SP2 and later, executing DBCC CHECKDB does not clear the plan cache.
Best Practices
Wonderware Support recommends that you use the PHYSICAL_ONLY option for frequent use on production systems.
Using PHYSICAL_ONLY can greatly shorten run-time for DBCC CHECKDB on large databases.
D. McKenna
Tech Notes are published occasionally by Wonderware Technical Support. Publisher: Invensys Systems, Inc., 26561 Rancho Parkway South, Lake Forest, CA 92630. There is also
technical information on our software products at Wonderware Technical Support.
Back to top
©2013 Invensys Systems, Inc. All rights reserved. No part of the material protected by this copyright may be reproduced or utilized in any form or by any means, electronic or
mechanical, including photocopying, recording, broadcasting, or by any information storage and retrieval system, without permission in writing from Invensys Systems, Inc.
Terms of Use.