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 Cloud and Datacenter Management & Data Platform MVP who has worked in IT for over 20 years. She is the creator of the popular SQL PowerShell module dbatools, and holds a number of certifications, including those relating to SQL Server, Linux, SharePoint and network security. 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 *

*