简体   繁体   English

如何在不先备份的情况下缩小日志文件?

[英]How to shrink a log file without backuping first?

I have a SQL Server 2008 database with a .mdf file with 1 GB and a .ldf file (log) with 70 GB.我有一个 SQL Server 2008 数据库,其中包含一个 1 GB 的.mdf文件和一个 70 GB 的.ldf文件(日志)。 I really don't know what took my log file to be so big in a week and to stop to increase, but my main issue is to fix this problem.我真的不知道是什么让我的日志文件在一周内变得如此大并停止增加,但我的主要问题是解决这个问题。

I'm used to reduce the log file shrinking it, but I can only shrink IF I backup it first.我习惯于减少日志文件缩小它,但如果我先备份它,我只能缩小它。 If I try to shrink without backuping first (using SSMS), nothing happens, even with SSMS showing that available free space is big.如果我在不先备份的情况下尝试缩小(使用 SSMS),则不会发生任何事情,即使 SSMS 显示可用空间很大。 I can try shrinking many times but it will work only if I backup first.我可以尝试缩小很多次,但只有在我先备份时它才会起作用。

The problem is that I can't backup it this time because I don't have free space (the total size of my HD is 120 GB).问题是这次我无法备份它,因为我没有可用空间(我的 HD 总大小为 120 GB)。

Note 1: my database is set to use the full recovery model because I need to be able to do point-in-time recoveries.注 1:我的数据库设置为使用完整恢复模型,因为我需要能够进行时间点恢复。

Note 2: I know that shrink increases the index fragmentation.注 2:我知道收缩会增加索引碎片。 After shrinking, I can use REBUILD in indexes to avoid this.缩小后,我可以在索引中使用REBUILD来避免这种情况。

You can temporarily set recovery model to simple and truncate log您可以临时将恢复模式设置为简单并截断日志

you will lose point-in-time recovery ability in time period between last successful log backup and end of the next differential backup that you can take after log cleanup.在上次成功的日志备份和日志清理后可以进行的下一次差异备份结束之间的时间段内,您将失去时间点恢复能力。 But point in time recovery possible after backup time onwards但是在备份时间之后可以进行时间点恢复

You also need to find the long running transaction that is active and find the root cause您还需要找到活动的长时间运行的事务并找到根本原因

You can see here http://blog.sqlxdetails.com/transaction-log-survival-guide-shrink-100gb-log/你可以在这里看到http://blog.sqlxdetails.com/transaction-log-survival-guide-shrink-100gb-log/

With the help of below command you can clear transaction log file.借助以下命令,您可以清除事务日志文件。 command is well commented.命令有很好的注释。

-- see the log size
DBCC SQLPERF(LOGSPACE);

--taking backup for log file before shrink it
BACKUP LOG MyTestDB
TO DISK = 'E:\PartProcForOld_log_backup\MyTestDB.TRN'
GO

-- this command will tell you the log file name
SELECT name
FROM sys.master_files
WHERE database_id = db_id()
  AND type = 1


--- these below command will alter database with actual shrink
GO
-- Truncate the log by changing the database recovery model to SIMPLE.
ALTER DATABASE MyTestDB

SET RECOVERY SIMPLE;
GO
-- Shrink the truncated log file to 1 MB.
DBCC SHRINKFILE (MyTestDB_log, 1);
GO
-- Reset the database recovery model.
ALTER DATABASE MyTestDB
SET RECOVERY FULL;
GO

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

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