question

subramanyam avatar image
subramanyam asked

Shrink Database file

Script used to shrink, SQL 2008

USE directory;
GO

-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE directory
SET RECOVERY SIMPLE;
GO

-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (2, 1);
-- here 2 is the file ID for trasaction log file,you can also mention the log file name dbname_log)
GO

-- Reset the database recovery model.
ALTER DATABASE directory
SET RECOVERY FULL;
GO
--Getting below error---
Script failed:Cannot shrink log file 2 (DirectoryLog) because the logical log file located at the end of the file is in use.
shrink-databaseshrink-log-file
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

SirSQL avatar image
SirSQL answered
The quick answer is that the transaction log is made up of multiple smaller virtual log files (VLFs) and that transaction log usage is circular. This means that you would be unable to shrink the file because the latest VLF is in use. I'd recommend running a transaction and then performing a checkpoint to hopefully go to the next VLF. For a proper and full explanation please read [Paul Randal's post on the matter][1] Of interest, why are you shrinking the transaction log on this system? By switching recovery models you are losing your point in time recovery capability which may severely impact your recovery point objectives. Also, your transaction log will grow again. You should be taking regular transaction log backups to properly manage growth. [1]: http://www.sqlskills.com/BLOGS/PAUL/post/Inside-the-Storage-Engine-More-on-the-circular-nature-of-the-log.aspx
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Pavel Pawlowski avatar image
Pavel Pawlowski answered
+1 to @SirSQL. And I only add, if you ara swithing the recovery model to simple and than back into full, than it probably means, that you do not need the point in time recovery and then you can keep the the Simple recovery mode. In case you really need the point in time recovery and FULL recovery mode, then schedule a periodic LOG backups. In case you have log backups scheduled, than probably the intarval between backups is too long and your log grows too much. Then you should shorten the intervals between log backups.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.