DBCC SHRINKFILE in the log file does not reduce size even after BACKUP LOG TO DISK

I have a database, [My DB], which has the following information:
SQL Server 2008
MDF Size: 30 GB
LDF size: 67 GB.

I wanted to compress the log file as much as possible, and so I started my search to figure out how to do this. Caution: I am not a database administrator (or even approaching a database administrator), and progress has been made through this quest.

Firstly, I just went into SSMS, database properties, files, and edited the Initial Size (MB) value to 10. This reduced the log file to 62 GB (not exactly the 10 MB I entered). So, I connected SQL Profiler and saw that DBCC SHRINKFILE is being called. Then I entered this command into the query editor and here are the results.

DBCC SHRINKFILE (N'My DB_Log' , 10) 

And the result was:

 Cannot shrink log file 2 (My DB_Log) because the logical log file located at the end of the file is in use. DbId FileId CurrentSize MinimumSize UsedPages EstimatedPages ------ ----------- ----------- ----------- ----------- -------------- 8 2 8044104 12800 8044104 12800 (1 row(s) affected) DBCC execution completed. If DBCC printed error messages, contact your system administrator. 

Then I did some research and found the following:

http://support.microsoft.com/kb/907511

Which tells me that I need to back up the log file in front of the shrink file so that the virtual log files are released and the shrinkfile can do its job - I don't know what that means ... I'm just rephrasing here :)

So, I decided that I would try to back up the log file and then make DBCC SHRINKFILE (and I changed the size of the new log file to 12800, because it was the minimum size specified in the output of the previous DBCC SHRINKFILE command)

 BACKUP LOG [My DB] TO DISK = 'D:\SQLBackup\20110824-MyDB-Log.bak' GO DBCC SHRINKFILE (N'My DB_Log' , 12800) GO 

The result was the same as the first. I can only get the log file up to 62 GB.

I am not sure what I am doing wrong, and what I should try next.

+64
sql-server backup transaction-log dbcc
Aug 25 2018-11-15T00:
source share
8 answers

In addition to the steps that you have already taken, you will need to set recovery mode to simple before you can compress the log.

THIS IS NOT RECOMMENDED PRACTICE for production systems ... You will lose the ability to recover at a certain point in time from previous backup / log files.

See Example B on this DBCC SHRINKFILE (Transact-SQL) msdn page for an example and explanation.

+33
Aug 25 '11 at 16:09
source share

Well, here is a solution to reduce the physical size of a transaction file, but without changing the recovery mode to simple.

Locate the file_id of the log file in your database using the following query.

 SELECT * FROM sys.database_files; 

In my case, the log file is file_id 2. Now we want to find the virtual logs used and do this with the following command.

 DBCC LOGINFO; 

Here you can see if any virtual logs are being used by looking at whether the status is 2 (used) or 0 (free). When compressing files, empty virtual logs are physically deleted from the end of the file until it reaches the first used state. This is why compressing a transaction log file sometimes partially reduces it, but does not delete all free virtual logs.

If you notice state 2, which appears after 0, this blocks the compression from completely compressing the file. To work around this, make another backup of the transaction log and run these commands immediately, specifying the file_id above and the size by which you want to reduce the size of the log file.

DBCC SHRINKFILE (file_id, LogSize_MB)

 DBCC SHRINKFILE (2, 100); DBCC LOGINFO; 

Then the distribution of the virtual log files will be shown, and I hope you will notice that it has decreased somewhat. Since virtual log files are not always distributed in order, you may need to back up the transaction log a couple of times and run this last request again; but I can usually reduce it in a backup or two.

+105
Feb 17 '14 at 3:38
source share

I am using this script on SQL Server 2008 R2.

 USE [db_name] ALTER DATABASE [db_name] SET RECOVERY SIMPLE WITH NO_WAIT DBCC SHRINKFILE([log_file_name]/log_file_number, wanted_size) ALTER DATABASE [db_name] SET RECOVERY FULL WITH NO_WAIT 
+13
Apr 08 '14 at 9:36 on
source share

try it

 ALTER DATABASE XXXX SET RECOVERY SIMPLE use XXXX declare @log_File_Name varchar(200) select @log_File_Name = name from sysfiles where filename like '%LDF' declare @i int = FILE_IDEX ( @log_File_Name) dbcc shrinkfile ( @i , 50) 
+6
Jul 29 '13 at 14:25
source share
+4
Aug 21 2018-12-12T00:
source share

Log file reduction

For log files, the Database Engine uses target_size to calculate the target size for the entire log; therefore, target_size is the amount of free space in the log after the shrink operation. The target size for the entire log is then converted to the target size for each log file. DBCC SHRINKFILE attempts to compress each physical log file to its target size immediately.

However, if part of the logical log is located in virtual logs outside the target size, the Database Engine frees up as much space as possible and then issues an informational message.

The message describes what actions are required to move the logical output from the virtual logs at the end of the file. After completing the steps, DBCC SHRINKFILE can be used to free up the remaining space.

Because the log file can only be reduced to the edge of the virtual log file, it may not be possible to reduce the log file to a size smaller than the size of the virtual log file, even if it is not used . The size of the virtual log file is dynamically selected using the Database Engine when log files are created or expanded.

  • Troubleshooting: the file is not compressed

If the shrink operation is performed without errors, but the file has not changed in size, make sure that the file has sufficient free space for deletion by performing one of the following operations:

Run the following query.

SELECT name, size / 128.0 - CAST (FILEPROPERTY (name, 'SpaceUsed') AS int) /128.0 AS AvailableSpaceInMB FROM sys.database_files;

Run the DBCC SQLPERF command to return the space used in the transaction log.

  • If there is not enough free space, the compression operation cannot reduce the file size.

  • This is usually a log file that is not compressed. This is usually the result of a log file that has not been truncated.

  • You can trim the log by installing the database recovery model in SIMPLE or backing up the log , and then run DBCC SHRINKFILE again.

Example:

Reduce the log file to the specified target size

The following example reduces the log file in the AdventureWorks database to 1 MB. In order for the DBCC SHRINKFILE command to compress the file, the file is first truncated, setting the database recovery model to SIMPLE.

Transact-sql

USE AdventureWorks2012;
GO
- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE AdventureWorks2012
INSTALL RECOVERY SIMPLE | GO
- Compression of the truncated log file to 1 MB.
DBCC SHRINKFILE (AdventureWorks2012_Log, 1);
GO
- Reset database recovery model.
ALTER DATABASE AdventureWorks2012
SET RECOVERY FULL,
GO

When you use DBCC SHRINKFILE (Logfile, size), it only truncates from the end of the log file as much as possible. When it reaches the highest virtual log that is still in use, it cannot shrink. This is described in SQL Server Books Online at:

http://technet.microsoft.com/en-us/library/ms189493.aspx

So, as soon as the top end of the magazine becomes clear, it can be reduced in size. Again, this will depend on how much of the log is still in use. The log can be cleared by backups, but backups will not clear incomplete transactions, so the log can remain in high-performance VLF even after repeated backups.

As for increasing and decreasing VLF, how large was the log file that was originally created, and what is the parameter for growing the log file? If he grows just a small amount, he will create more VLFs than anyone wants.

A common template for shortening a log file is CHECKPOINT, BACKUP, SHRINKFILE, CHECKPOINT, BACKUP, SHRINKFILE, etc., until you get the results. There are many reasons why a journal cannot be shrinkable, including very large rollbacks.

Switching from Simple to Full has a problem:

There are rules and exceptions. We will talk about long transactions in depth below.

But one caveat regarding the full recovery mode: if you simply switch to full recovery mode but never take the initial full backup, SQL Server will not honor your request in the Full Recovery model. Your transaction log will continue to work as it did in Simpleuntil when you switch to the full recovery model and perform the first full backup.

A complete recovery model without log backups is bad:

So what is the most common cause of uncontrolled magazine growth? Answer: Be in full recovery mode without any log backups.

This happens all the time for people.

Why such a common mistake?

Why is this happening all the time? As each new database gets the initial setup of the recovery model by looking at the model database.

The parameter of the original recovery model is always a full recovery model - until someone changes it. Thus, you can say that the "Default Recovery Model" is full. Many people are unaware of this and have their databases in the full recovery model without backing up logs and, therefore, the transaction log file is much larger than necessary. This is why it is important to change the default settings when they do not work for your organization and its needs)

+4
Feb 07 '17 at 7:44
source share

I tried many ways, but it works.

Sample code is available in DBCC SHRINKFILE

 USE DBName; GO -- Truncate the log by changing the database recovery model to SIMPLE. ALTER DATABASE DBName SET RECOVERY SIMPLE; GO -- Shrink the truncated log file to 1 MB. DBCC SHRINKFILE (DBName_log, 1); --File name SELECT * FROM sys.database_files; query to get the file name GO -- Reset the database recovery model. ALTER DATABASE DBName SET RECOVERY FULL; GO 
+2
Jul 19 '17 at 6:30
source share

Thanks to @ user2630576 and @ Ed.S.

The following has been processed:

 BACKUP LOG [database] TO DISK = 'D:\database.bak' GO ALTER DATABASE [database] SET RECOVERY SIMPLE use [database] declare @log_File_Name varchar(200) select @log_File_Name = name from sysfiles where filename like '%LDF' declare @i int = FILE_IDEX ( @log_File_Name) dbcc shrinkfile ( @i , 50) ALTER DATABASE [database] SET RECOVERY FULL 
-one
Jan 21 '14 at 0:02
source share



All Articles