I have a database running on SQL Server 2005. The database is 20Gb and the LDF file is 35Gb ! I'm now running low on disk space and want to shrink the log file. How can I do this and how can I stop this happening again ?
Many thanks
Scott
feedback
|
|
Well, basically, your SQL Server Log files need to be backed up regularly - every couple hours or days. When you do that, they'll shrink. Now, in your case, there's two things you can do:
| |||||||||||
feedback
|
|
A database in Microsoft SQL Server has different "recovery modes" it can be configured to be in:
Full recovery is great because you can restore your database nearly up to the time of a crash, but it also necessitates that you take log backups. Why? Because logs inside the transaction log file are only marked as "truncated" once you take a log backup. Once a log is truncated, new logs are allowed to overwrite it. If you never take a log backup, then nothing in the transaction log is ever marked as truncated, and therefore the log file must grow with every database change. For more information on how the transaction log works, please read Paul Randal's excellent articles:
Now, in the situation where you find yourself with an enormous transaction log, there are two real options: Option 1: Switch to the simple recovery model
Now shrink the log file to get yourself out of the bad situation:
Essentially, this means the log file will truncate itself automatically after every transaction. Therefore, its size will not grow out of control. You will not have to manage the transaction log at all; however, you lose the ability to restore your database beyond your latest full or differential backup. Option 2: Start taking transaction log backupsIf you really do need full recovery, then you must start taking transaction log backups. I suspect First, to get yourself out of the bad situation, you want to truncate the entire log, then shrink it:
Now your transaction log file should be a reasonable size. It will start growing again immediately; though. You must now start taking log backups regularly. You can do this through a maintenance plan, or through regularly running a command such as:
Side noteYou'll notice we used a command | ||||
|
feedback
|