简体   繁体   English

如何缩小 89.5% 未使用的日志文件

[英]How to shrink a log file where 89.5% is unused

I currently have a log file at 27.6GB where 89.5 is unused.我目前有一个 27.6GB 的日志文件,其中 89.5 未使用。 10.5% is used.使用了 10.5%。 What I don't know is what value to use after the DataFile1 example below.我不知道在下面的 DataFile1 示例之后使用什么值。 Any help or recommendation's is welcomed.欢迎任何帮助或建议。

USE UserDB;
GO
DBCC SHRINKFILE (DataFile1, 7);
GO

Use the name of the log file you want to shrink, which you can discover like this:使用您要缩小的日志文件的名称,您可以像这样发现:

select file_id, type_desc, name, physical_name
from sys.database_files

A database will typically have only one log file.一个数据库通常只有一个日志文件。 eg例如

file_id     type_desc                                                    name
----------- ------------------------------------------------------------ --------------------------------------------------------------------------------------------------------------------------------
1           ROWS                                                         AdventureWorksDW2017
2           LOG                                                          AdventureWorksDW2017_log

(2 rows affected)

Then然后

DBCC SHRINKFILE (N'AdventureWorksDW2017_log' , 0, TRUNCATEONLY)

will attempt to shrink the log file to its initial creation size.将尝试将日志文件缩小到其初始创建大小。 If there are used log segments near the end of the file, or the total used space is greater, it won't shrink that much.如果文件末尾附近有使用过的日志段,或者总使用空间更大,它不会缩小那么多。

You shouldn't do this regularly, though, because you don't want the log file to grow while while the database is active.但是,您不应该定期执行此操作,因为您不希望日志文件在数据库处于活动状态时增长。 Log file growth is expensive, as the file must be zeroed, and all sessions that need to commit transactions will have to wait for the the operation to complete.日志文件增长代价高昂,因为文件必须清零,并且所有需要提交事务的会话都必须等待操作完成。

Here is the script I use to get the log file size.这是我用来获取日志文件大小的脚本。

IF OBJECT_ID('tempdb..#tmplogspace') IS NOT NULL DROP TABLE #tmplogspace

CREATE table #TmpLOGSPACE  
(
DatabaseName varchar(100),
LOGSIZE_MB decimal(18, 9),
LOGSPACE_USED decimal(18, 9),
LOGSTATUS decimal(18, 9)
) 

INSERT INTO #TmpLOGSPACE(DatabaseName, LOGSIZE_MB, LOGSPACE_USED, LOGSTATUS) 
EXEC('DBCC SQLPERF(LOGSPACE);')

SELECT * FROM #TmpLOGSPACE
ORDER BY LOGSIZE_MB DESC

Look at the size and percentage for the DB in question.查看相关数据库的大小和百分比。 Then run this:然后运行这个:

USE My_Database
GO

DBCC SHRINKFILE (2,20000)  -- 2 is for log file, last number is 20G, which should be a safe start.  Change as desired MB size

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM