30

I have a fairly large SQL Server database that is using SIMPLE recovery mode. We don't really have a need for up to the second recovery so i'd prefer we stay with this mode.

For some reason the transaction log for this database is massive (410 GB) with 99% of the space unallocated.

I've tried to shrink the file using ( DBCC SHRINKFILE (MyDatabase_log, 20000) )but it doesn't seem to work.

Anyone have any tips on why a SIMPLE recovery mode database would have such a huge file? I'd really like to get it shrunk way down.

1
  • 1
    Last time I checked, the default shrink units were in GB. So what you declared there might have been 20000 GB, which would not have any effect :)
    – jocull
    Mar 21, 2018 at 16:40

6 Answers 6

29

It means you once had a single transaction that lasted for so long that it forced the log to grow 410GB. The log cannot be reused if there is an active transaction since the rollback information cannot be erased. Such an example would be if someone open an SSMS query, starts a transaction, updates a record and then goes in vacation. The transaction will be active and force the log to grow until is eventually committed or rolled back. When the transaction eventually ends the used space can finally be reclaimed, leaving a huge empty log file.

Another scenario is if you had about 200GB of data updated in a single transaction. The log will store the before and after images of the changes thus consuming twice the space, and it cannot be reused, again because is all one single transaction.

Update

I neglected to mention Replication that is also a factor that can prevent log truncation. And so is Mirroring, an distributed transaction (technically that is the same as an 'active transaction', but the DTC implication makes it a distinct case). The complete list and explanations is at Factors That Can Delay Log Truncation.

13
  • It wouldn't show as unallocated space if it were mid-rollback/transaction, would it?
    – Eric
    Jul 28, 2009 at 18:46
  • Thanks! This sounds very possible. Do you know what can be done to remove the log if it's in this locked state?
    – Eric
    Jul 28, 2009 at 18:47
  • If it shows as unallocated the space must be already claimed back by the SIMPLE recovery mode. Does the log_reuse_wait_desc column of sys.databases for the database in question says NOTHING or not? Jul 28, 2009 at 18:51
  • 1
    @Eric (OP) from the reply to no_one is clear you have replication from the database. Replication is another one of them log shrink/reuse restriction factors. You need all your subscribers to catch up, the server keeps track of who has received what in replication. If a subscriber is behind, it will cause the log to grow, same as an active transaction. Jul 28, 2009 at 19:07
  • 2
    Yep. Turning off replication has now allowed me to shrink the log using NOTRUNCATE. Thanks Eric & Remus!
    – Eric
    Jul 28, 2009 at 19:44
6

You're missing an argument in dbcc shrinkfile:

dbcc shrinkfile (MyDatabase_log, 20000, TRUNCATEONLY)

NOTRUNCATE is the default, which moves allocated blocks to the beginning of unallocated space. TRUNCATEONLY removes the unallocated space. So if you do a NOTRUNCATE followed by a TRUNCATEONLY, you get one slimmed down log.

3
  • I did try that. It didn't change anything. I did add a second log file to get the batabase back online when the space ran out. Maybe that's part of it?
    – Eric
    Jul 28, 2009 at 18:40
  • 1
    @Eric (Good name, btw): Make sure then, that you're shrinking the right log. If you have two log files, you'll need to shrink both independently. Check in the Files section of the database properties for what they're called. I think the default is MyDatabase_log_1, but I'm going off the top of my head.
    – Eric
    Jul 28, 2009 at 18:45
  • The documentation I read said that the default units were in GB, not MB, which may be the underlying reason why this did not work. This may vary between versions of SQL Server - I am not sure.
    – jocull
    Mar 21, 2018 at 16:42
3

If you have only one mdf file and one log file, perhaps the simplest way will be to detach the database, rename the log and reattach the database. SQL Server will create a new log file. After that your huge log file can be safely deleted.

This though will not work if you have multiple data files.

2
  • Thanks. I did try this but it wouldn't let me detach because the database acts as a replication publisher.
    – Eric
    Jul 28, 2009 at 18:49
  • This worked great, I could not get the damn thing to shrink all the DBCC SHRINKFILEs in the world would not budge. Anyway it was just a test DB not production, so I tried this. Works great just need to remove the "re-attach" .ldf file part as obviously it is not there. Thanks!
    – bendecko
    Oct 27, 2013 at 13:03
2

Replication Publisher? Could this be the reason for the huge transaction log?

1

As said in other responses, Active Transactions and Replications are typical causes for this problem.
Another one, less visible, is Change Data Capture (CDC).

I had a similar problem recently and the procedure which allowed me to free the log was as follow:

  • Disable CDC: EXEC sys.sp_cdc_disable_db
  • Create a publication on an arbitrary table within the database in question
  • Delete this/all publication(s). EXEC sp_removedbreplication 'my_db' is a convenient way to do so.
  • Shrink the log as desired

I'm unsure why the creation/deletion of this dummy/never-used publication was necessary but it was so. Tentatively the database may have had previous publications which were not disposed of properly (it is said to happen frequently with databases restored from a previous backup).

Another useful diagnostic idiom is to check the log_reuse_wait_desc in sys.databases for the offending database. This field read REPLICATION until I completed the above procedure.

SELECT log_reuse_wait_desc, * FROM sys.databases where name = 'my_db'
0

I had a similar issue on a local dev db and could only get it to shrink after running CHECKPOINT on the db a few times, not sure what got it in that locked state to begin with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.