shafiqm


Hi,

Database transaction log file has reached to 1 GB and (99% of this space is free). When try to reclaim the unused space, I have no luck. Initial size of the transaction log has now reaches 1GB. When trying to change the initial size to 5 MB, it does not work.

I have backup the transaction log with Truncate the transaction log but system is no releasing the space.

Please Guide.

Thanks

Shafiq




Re: transaction log and file size

Michael Hotek


That is because the log pointer must move to a VLF boundary point before it can roll back to the beginning of the tran log and thus allow you to truncate the space in the file from the end.




Re: transaction log and file size

gmilner


Try this. Put it in your master databse and run it IN the database where you want to shrink the log. If you run it in a production DB you should back it up immediately afterward.

Run it like this:

Use MyDatabase

GO

EXEC sp_trx_log_shink 2, 200

You may have to run it 2 or 3 times.

Here's the proc code:

create Procedure sp_trx_log_shrink (@MaxMinutes INT,
@NewSize INT )
/*=========================================================================
Title: Shrink SQL Server Transaction Log Stored procedure
Script C:\Docs\SQL_Scripts\Administrative\Maintenance\Logs\sp_trx_log_shrink.sql

Purpose: system proc based on
INF: How to Shrink the SQL Server 7.0 Transaction Log
Force shrink transaction log of current database to
specific size.

Params: @MaxMinutes = Max number minutes to run before stoppint
(recommend 2 at least)
@NewSize = New size in MBs of the log file after shrinking
(recommend at least 10 MB in most DBs)

Create/Update History:
10/31/2005 9:38:24 PM - Pre-delete DummyTrans table if exists.
3/9/2005 3:33:44 PM - Converted to procedure.

Notes:
Assumes only 2 physical database files and that _Data file
is file id 1 in sysfiles table and that log file is file id 2.
Original Source:
http://support.microsoft.com/default.aspx scid=kb;EN-US;Q256650
Microsoft Knowledge Base Article - 256650 ;
=========================================================================*/
AS
SET NOCOUNT ON
DECLARE @err int
DECLARE @LogicalFileName sysname
--DECLARE @SSQL as VARCHAR(255)
DECLARE @DBN as nVarchar(50)

-- Setup / initialize
DECLARE @OriginalSize int
set @DBN = (select db_name())
PRINT 'Database: ' + @DBN


SET @LogicalFileName = (SELECT FILE_NAME (2))

PRINT 'Log logical filename: ' + @LogicalFileName
PRINT ''
EXEC sp_helpdb @DBN

SELECT @OriginalSize = size -- in 8K pages
FROM sysfiles
WHERE name = @LogicalFileName
SELECT 'Original Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),@OriginalSize) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(@OriginalSize*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName

IF OBJECT_ID('DummyTrans') IS NOT NULL
DROP TABLE DummyTrans

CREATE TABLE DummyTrans
(DummyColumn char (8000) not null)

-- Wrap log and truncate it.
DECLARE @Counter INT,
@StartTime DATETIME,
@TruncLog VARCHAR(255)
SELECT @StartTime = GETDATE(),
@TruncLog = 'BACKUP LOG ['+ db_name() + '] WITH TRUNCATE_ONLY'
-- Try an initial shrink.
DBCC SHRINKFILE (@LogicalFileName, @NewSize)

EXEC (@TruncLog)

-- Wrap the log if necessary.
WHILE @MaxMinutes > DATEDIFF (mi, @StartTime, GETDATE()) -- time has not expired
AND @OriginalSize = (SELECT size FROM sysfiles
WHERE name = @LogicalFileName) -- the log has not shrunk
-- IF thhe value passed in for new size is smaller than the current size...
AND (@OriginalSize * 8 /1024) > @NewSize
BEGIN -- Outer loop.
SELECT @Counter = 0
WHILE ((@Counter < @OriginalSize / 16) AND (@Counter < 50000))
BEGIN -- update
-- Because it is a char field it inserts 8000 bytes...
INSERT DummyTrans VALUES ('Fill Log')
DELETE DummyTrans
SELECT @Counter = @Counter + 1
END -- update
EXEC (@TruncLog) -- See if a trunc of the log shrinks it.
END -- outer loop
SELECT 'Final Size of ' + db_name() + ' LOG is ' +
CONVERT(VARCHAR(30),size) + ' 8K pages or ' +
CONVERT(VARCHAR(30),(size*8/1024)) + 'MB'
FROM sysfiles
WHERE name = @LogicalFileName
DROP TABLE DummyTrans
PRINT '*** Perform a full database backup ***'
SET NOCOUNT OFF

IF @err <> 0
BEGIN
RETURN(@err)
PRINT '*** SHRINK FAILED!!! ***'
END

IF @err = 0
BEGIN
PRINT '*** Perform a full database backup ***'
END





Re: transaction log and file size

shafiqm


Here is what I did.

Deattach the database. Move or rename the transaction log. Attach the database it will show trn log file not found. Remove that file and system will automatically create the transaction log with minimum size. It will also change the Recovery Mode to simple. Change the Recover Model back to Full.






Re: transaction log and file size

Michael Hotek


You can do that. However, you will have lost anything that was in the tran log that had not been flushed to disk. Doing something like this is an absolute last resort and should only be done in a production environment when there are no other alternatives.




Re: transaction log and file size

bass_player


Before doing anything to your transaction log file, make sure you understand why it is there in the first place. They are the "black boxes" of your databases




Re: transaction log and file size

Michael Hotek


They aren't "black boxes". The information contained in a transaction log is very straightforward. There are several tools on the market - Log Explorer, Log PI, and SQL Log Rescue are three of them that will quite literally let you look at the contents of a tran log as well as tran log backups. So, there is very little that is "black box" about them.




Re: transaction log and file size

Paul Randal - MSFT


I don't agree with you. The transaction log is a black-box for the majority of customers and absolutely should stay that way. What people need to know is how to manage transaction log space and the ramifications of the various recovery modes - the meaning of the contents of the log are irrelevant to the vast majority of customers.

Thanks






Re: transaction log and file size

ADS4K


The easiest way to reclaim the space is to change a database option (change from full recovery to simple, or whatever you want to change) and then change it right back to what it was before. You will then be able to shrink the log file the first time you try it to reclaim the space.



Re: transaction log and file size

Michael Hotek


Yeah, you could then shrink the log file, but at the expense of not being able to backup the tran log until you run a full backup. So, I VERY STRONGLY discourage doing this.