SQL: Guaranteed Way to Quickly Shrink the T-Log

My hero, Brad McGehee, has often advised against shrinking the transaction log when it’s unnecessary. According to his (really amazing) SQL Server DBA Best Practices PowerPoint Presentation, shrinking too often can lead to increased file fragmentation and excessive use of resources allocated to grow/shrink the file.

Sometimes, however, it is necessary. Often times, getting the transaction log to shrink can be tricky but here’s how you can do it. That is, unless the t-log is currently in use (think SharePoint Search crawls), then the query will just wait there until the transactions are complete.

DBCC SHRINKFILE(logicalfilename, 1)
BACKUP LOG MyDatabase WITH TRUNCATE_ONLY
DBCC SHRINKFILE(logicalfilename, 1)

 
If you don’t know the logical filename, run sp_helpfile for a list of database filenames. Be sure to make a FULL backup of your database once the shrink is complete.

Update: This guy also has a great write-up on reasons to avoid shrinking the transaction log when possible.

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in SQL Server
One comment on “SQL: Guaranteed Way to Quickly Shrink the T-Log
  1. gladchenko says:

    + CHECKPOINT (first)

Leave a Reply

Your email address will not be published. Required fields are marked *

*