question

user-1443 avatar image
user-1443 asked

large log file that I am unable to shrink

i have a sqlserver 2005 database. The mdf file is 15G and the logfile is 35G. The current recovery model for the database is simple. The initial file size for the log file is 34K+ mb. I have tried to shrink the logfile by doing a "backup log with truncate_only", do a "backup log to disc" followed by a shrink. I have tried to reduce the size of the initial file size and then truncate it. I have done a full backup of the database and then tried to shrink the file. Nothing seems to work. And because the backup file for the database is over 45G, I don't have room to do a backup anymore and this is a production database. Please help!!!!! Thanks.

shrink-database
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

CirqueDeSQLeil avatar image CirqueDeSQLeil commented ·
How much free space is in the log file?
0 Likes 0 ·
Matt Whitfield avatar image
Matt Whitfield answered

Run the following command

CHECKPOINT

Then try backup log with truncate_only

Edit ->

If that doesn't work, a good option to try is something along these lines:

DECLARE @file_id [int]
SELECT @file_id = [file_id] FROM [sys].[database_files] WHERE [type] = 1
DBCC SHRINKFILE (@file_id, 1)
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

user-1443 avatar image user-1443 commented ·
I just tried this and the log file is still huge...did not shrink at all
0 Likes 0 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
That sucks. We had a system which did the same thing a while back which had the same symptoms and needed to manually checkpoint it. Might be worth trying a couple of times, but if that doesn't help then I'm not sure off the top of my head...
0 Likes 0 ·
Matt Whitfield avatar image Matt Whitfield ♦♦ commented ·
This KB article was helpful: http://support.microsoft.com/kb/317375, as was this one: http://support.microsoft.com/kb/873235 . You might want to consider using DBCC SHRINKFILE as well...
0 Likes 0 ·
Blackhawk-17 avatar image
Blackhawk-17 answered

Run the following query to see what kind of available space is in the log:

SELECT  instance_name, cntr_value / 1024.0 [Log Space Used (MB)] 
FROM        sys.dm_os_performance_counters
WHERE       counter_name = 'Log File(s) Used Size (KB)'
ORDER BY    instance_name  

Also run this:

DBCC LOGINFO(YourDatabaseName)  

When there is a 2 in the status field it means the log won't shrink less than that portion - the active portion. Create a new table, insert data, delete data etc. then run the DBCC again. When there are status = 0 rows at the end of your log you have room to shrink the log.

Do you have other disks available? You could also set the log to not autogrow and add another log file elsewhere and then perform maintenance to get back to one log file when you can take SQL down.

Another option is to attempt what this article says: Empty Log File

1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image Blackhawk-17 commented ·
Is there a stalled backup in progress during this? It could be that this is preventing the truncates as, I believe (don't quote me) they have to be performed serially, not in parallel.
0 Likes 0 ·
Rob Farley avatar image
Rob Farley answered

Truncating and Shrinking are two very different things. You need to be looking at how much free space the log file indicates it has. To see this, use Management Studio. Right-click on the database and go to Tasks, Shrink File. Pick Log. Don't hit Ok, just read what it says the percentage of free space is.

Hopefully it's almost completely empty. If not, a truncate can help. The way you do this depends on the Recovery Model used by your database. If it's Simple, then you do it using the T-SQL command CHECKPOINT. If it's Full or Bulk Logged, you need to back up your log.

If you're not interested in keeping your log backup, then put the database into Simple and then do a CHECKPOINT. Then put it back into the Full recovery model and do a database backup.

If the file is largely empty, then shrinking the file will make it smaller on disk. Don't shrink it completely as it will only need to grow, and this is painful for logs (as they need to be zeroed out on disk).

Above all, NEVER USE AUTOSHRINK, as this will really hurt your system. Sounds good. Isn't.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image Blackhawk-17 commented ·
Rob, they have already stated it is in Simple recovery and they are still having issues. +1 for the all caps autoshring warning :)
0 Likes 0 ·
Rob Farley avatar image Rob Farley commented ·
Ah, yes. But still he needs to heed the first paragraph, where I describe how to see how empty the file is. And then to do a checkpoint and see if that affects the size at all.
0 Likes 0 ·
Grant Fritchey avatar image
Grant Fritchey answered

It sounds to me like you may have an open transaction that is keeping the log from being truncated and then shrunk. Try running a query against the sys.dm_tran_database_transactions to see if there is an open transaction on your database.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.