Вы находитесь на странице: 1из 2

SHRINKFILE and TRUNCATE Log File in SQL Server 2008

Introduction

You know there is always an issue - the log file growing very fast and big. If you have
plenty of storage, then this might not be a problem for you. Anyway, this is no exception in
the latest version of SQL, we still have to do something to truncate and shrink these files.

Implementation

1) Lets first check the log file size.

SELECT
--DB_NAME(database_id) AS DatabaseName,
--Physical_Name,
Name AS Logical_Name,
(size*8)/1024 SizeMB
FROM
sys.master_files
WHERE
DB_NAME(database_id) = 'tempdb'
GO

Output

2) Now truncate the log file.

USE tempdb;
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE tempdb
SET RECOVERY SIMPLE WITH NO_WAIT;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE(tempdb_log, 1); --file_name is the logical name of the file
to be shrink
GO
-- Reset the database recovery model.
ALTER DATABASE tempdb
SET RECOVERY FULL WITH NO_WAIT;
GO

3) Lets check the log file size.

SELECT
--DB_NAME(database_id) AS DatabaseName,
--Physical_Name,
Name AS Logical_Name,
(size*8)/1024 SizeMB
FROM
sys.master_files
WHERE
DB_NAME(database_id) = 'tempdb'
GO