Hae usein kysyttyjä kysymyksiä
How to shrink the transaction log file in SQL Server
Issue
When it is observed that a SQL Server transaction log file (.ldf) has grown significantly it may be desirable to reduce the size of this database log file.
Product
Struxureware Power Monitoring
Power Monitoring Expert
ION Enterprise
ION EEM
Environment
SQL Server 2005, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014, SQL Server 2016, SQL Server 2017, SQL Server 2019
Cause
Transaction log files store all of the transactions and the database modifications made by these transactions as updates and changes to the database occur.
As more and more transactions modify the database and there is more transaction activity than maintenance activity on the transaction log, these logs will grow.
In particular, if there was some unusual amount of activity on the database, this would cause the logs to grow quickly. Another possible cause could be that the Automatic grow file option has been set too high such that the transaction log file will grow by x% resulting in an exponential file growth.
Resolution
*Warning: Irreparable database damage can occur. This procedure should only be performed by users familiar with SQL Server Management Studio. Databases should be backed up prior to performing this procedure.*
If you know that a transaction log file contains unused space that you will not need, you can reclaim the excess space by reducing the size of the transaction log. This process is known as shrinking the log file. Shrinking data files recovers space by moving pages of data from the end of the file to unoccupied space closer to the front of the file. When enough free space is created at the end of the file, data pages at the end of the file can deallocated and returned to the file system.
- In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance.
- Expand Databases and then right-click the database that you want to shrink.
- Point to Tasks, point to Shrink and then click Files.
- Select the file type and file name.
- Optionally, select the Release unused space check box. Selecting this option causes any unused space in the file to be released to the operating system and shrinks the file to the last allocated extent. This reduces the file size without moving any data.
- Optionally, select the Reorganize files before releasing the unused space check box. If this is selected, the Shrink file to value must be specified. By default, the option is cleared. Selecting this option causes any unused space in the file to be released to the operating system and tries to relocate rows to unallocated pages.
- Optionally, enter the maximum percentage of free space to be left in the database file after the database has been shrunk. Permissible values are between 0 and 99. This option is only available when Reorganize files before releasing unused space is activated.
- Optionally, select the Empty file by migrating the data to other files in the same filegroup check box. Selecting this option moves all data from the specified file to other files in the filegroup. The empty file can then be deleted. This option is the same as executing DBCC SHRINKFILE with the EMPTYFILE option.
- Click OK.