简体   繁体   English

为什么即使在备份之后我也无法收缩事务日志文件?

[英]Why can't I shrink a transaction log file, even after backup?

I have a database that has a 28gig transaction log file. 我有一个具有28gig事务日志文件的数据库。 Recovery mode is simple. 恢复模式很简单。 I just took a full backup of the database, and then ran both: 我刚刚对数据库进行了完整备份,然后运行了两个:

backup log dbmcms with truncate_only

DBCC SHRINKFILE ('Wxlog0', TRUNCATEONLY)

The name of the db is db_mcms and the name of the transaction log file is Wxlog0. db的名称是db_mcms,事务日志文件的名称是Wxlog0。

Neither has helped. 两者都没有帮助。 I'm not sure what to do next. 我不知道下一步该做什么。

Thank you to everyone for answering. 谢谢大家的回答。

We finally found the issue. 我们终于找到了这个问题。 In sys.databases, log_reuse_wait_desc was equal to 'replication'. 在sys.databases中,log_reuse_wait_desc等于'replication'。 Apparently this means something to the effect of SQL Server waiting for a replication task to finish before it can reuse the log space. 显然,这意味着SQL Server在重用日志空间之前等待复制任务完成的效果。

Replication has never been used on this DB or this server was toyed with once upon a time on this db. 从未在此数据库上使用 复制, 或者此服务器 在此数据库上 玩过一次。 We cleared the incorrect state by running sp_removedbreplication. 我们通过运行sp_removedbreplication清除了错误的状态。 After we ran this, backup log and dbcc shrinkfile worked just fine. 运行之后,备份日志和dbcc shrinkfile工作得很好。

Definitely one for the bag-of-tricks. 绝对是一个包的技巧。

Sources: 资料来源:

http://social.technet.microsoft.com/Forums/pt-BR/sqlreplication/thread/34ab68ad-706d-43c4-8def-38c09e3bfc3b http://social.technet.microsoft.com/Forums/pt-BR/sqlreplication/thread/34ab68ad-706d-43c4-8def-38c09e3bfc3b

http://www.eggheadcafe.com/conversation.aspx?messageid=34020486&threadid=33890705 http://www.eggheadcafe.com/conversation.aspx?messageid=34020486&threadid=33890705

You may run into this problem if your database is set to autogrow the log & you end up with lots of virtual log files. 如果您的数据库设置为自动增长日志并且最终会有大量虚拟日志文件,则可能会遇到此问题。
Run DBCC LOGINFO('databasename') & look at the last entry, if this is a 2 then your log file wont shrink. 运行DBCC LOGINFO('databasename')并查看最后一个条目,如果这是2,则日志文件不会缩小。 Unlike data files virtual log files cannot be moved around inside the log file. 与数据文件不同,虚拟日志文件无法在日志文件中移动。

You will need to run BACKUP LOG and DBCC SHRINKFILE several times to get the log file to shrink. 您需要多次运行BACKUP LOG和DBCC SHRINKFILE才能使日志文件缩小。

For extra bonus points run DBBC LOGINFO in between log & shirks 对于额外的奖励积分,在日志和推卸之间运行DBBC LOGINFO

'sp_removedbreplication' didn't solve the issue for me as SQL just returned saying that the Database wasn't part of a replication... 'sp_removedbreplication'没有为我解决问题,因为SQL刚回来说数据库不是复制的一部分......

I found my answer here: 我在这里找到了答案:

Basically I had to create a replication, reset all of the replication pointers to Zero; 基本上我必须创建一个复制,将所有复制指针重置为Zero; then delete the replication I had just made. 然后删除我刚刚制作的复制。 ie

Execute SP_ReplicationDbOption {DBName},Publish,true,1
GO
Execute sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1
GO
DBCC ShrinkFile({LogFileName},0)
GO
Execute SP_ReplicationDbOption {DBName},Publish,false,1
GO

This answer has been lifted from here and is posted here in case the other thread gets deleted: 这个答案已从这里解除,并在此处发布以防其他线程被删除:

The fact that you have non-distributed LSN in the log is the problem. 您在日志中包含非分布式LSN的事实是个问题。 I have seen this once before not sure why we dont unmark the transaction as replicated. 我之前看过这个,不确定为什么我们不将交易标记为已复制。 We will investigate this internally. 我们将在内部进行调查。 You can execute the following command to unmark the transaction as replicated 您可以执行以下命令以将事务取消标记为已复制

EXEC sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1

At this point you should be able to truncate the log. 此时,您应该能够截断日志。

Don't you need this 你不需要这个

DBCC SHRINKFILE ('Wxlog0', 0)

Just be sure that you are aware of the dangers: see here: Do not truncate your ldf files! 请确保您了解危险:请参阅此处: 不要截断您的ldf文件!

And here Backup Log with Truncate_Only: Like a Bear Trap 这里备份日志与Truncate_Only:像熊陷阱

I've had the same issue in the past. 我过去也遇到过同样的问题。 Normally a shrink and a trn backup need to occur multiple times. 通常,收缩和trn备份需要多次发生。 In extreme cases I set the DB to "Simple" recovery and then run a shrink operation on the log file. 在极端情况下,我将数据库设置为“简单”恢复,然后对日志文件运行收缩操作。 That always works for me. 这对我来说总是有用的。 However recently I had a situation where that would not work. 然而,最近我遇到了无法奏效的情况。 The issue was caused by a long running query that did not complete, so any attempts to shrink were useless until I could kill that process then run my shrink operations. 这个问题是由一个没有完成的长时间运行的查询引起的,所以任何收缩的尝试都是无用的,直到我可以杀死该进程然后运行我的收缩操作。 We are talking a log file that grew to 60 GB and is now shrunk to 500 MB. 我们正在谈论一个增长到60 GB的日志文件,现在缩小到500 MB。

Remember, as soon as you change from FULL to Simple recovery mode and do the shrink, dont forget to set it back to FULL. 请记住,只要您从FULL更改为Simple恢复模式并进行缩小,就不要忘记将其设置回FULL。 Then immediately afterward you must do a FULL DB backup. 然后,您必须立即执行完整数据库备份。

I know this is a few years old, but wanted to add some info. 我知道这已经有几年了,但是想补充一些信息。

I found on very large logs, specifically when the DB was not set to backup transaction logs (logs were very big), the first backup of the logs would not set log_reuse_wait_desc to nothing but leave the status as still backing up. 我发现非常大的日志,特别是当数据库没有设置为备份事务日志(日志非常大)时,日志的第一个备份不会将log_reuse_wait_desc设置为空,而是将状态保持为仍然备份。 This would block the shrink. 这会阻止收缩。 Running the backup a second time properly reset the log_reuse_wait_desc to NOTHING, allowing the shrink to process. 第二次运行备份正确地将log_reuse_wait_desc重置为NOTHING,允许收缩进行处理。

If you set the recovery mode on the database in 2005 (don't know for pre-2005) it will drop the log file all together and then you can put it back in full recovery mode to restart/recreate the logfile. 如果您在2005年在数据库上设置恢复模式(2005年之前不知道),它会将日志文件全部丢弃,然后您可以将其恢复为完全恢复模式以重新启动/重新创建日志文件。 We ran into this with SQL 2005 express in that we couldn't get near the 4GB limit with data until we changed the recovery mode. 我们使用SQL 2005 express遇到了这个问题,因为在我们更改恢复模式之前,我们无法接近数据的4GB限制。

I tried all the solutions listed and none of them worked. 我尝试了列出的所有解决方案,但没有一个有效。 I ended up having to do a sp_detach_db, then deleting the ldf file and re-attaching the database forcing it to create a new ldf file. 我最终不得不做一个sp_detach_db,然后删除ldf文件并重新附加数据库,强制它创建一个新的ldf文件。 That worked. 那很有效。

I had the same problem. 我有同样的问题。 I ran an index defrag process but the transaction log became full and the defrag process errored out. 我运行了索引碎片整理过程,但事务日志已满,并且碎片整理过程出错了。 The transaction log remained large. 事务日志仍然很大。

I backed up the transaction log then proceeded to shrink the transaction log .ldf file. 我备份了事务日志,然后继续收缩事务日志.ldf文件。 However the transaction log did not shrink at all. 但是,事务日志根本没有缩小。

I then issued a "CHECKPOINT" followed by "DBCC DROPCLEANBUFFER" and was able to shrink the transaction log .ldf file thereafter 然后我发出了一个“CHECKPOINT”,然后是“DBCC DROPCLEANBUFFER”,然后能够缩小事务日志.ldf文件

Have you tried from within SQL Server management studio with the GUI. 您是否已使用GUI在SQL Server管理工作室内尝试过。 Right click on the database, tasks, shrink, files. 右键单击数据库,任务,缩小文件。 Select filetype=Log. 选择filetype = Log。

I worked for me a week ago. 一周前我为我工作过。

Try to use target size you need insted of TRUNCATEONLY in DBCC: 尝试在DBCC中使用需要TRUNCATEONLY的目标大小:

DBCC SHRINKFILE ('Wxlog0', 1) DBCC SHRINKFILE('Wxlog0',1)

And check this to articles: 并查看文章:

http://msdn.microsoft.com/en-us/library/ms189493(SQL.90).aspx http://msdn.microsoft.com/en-us/library/ms189493(SQL.90).aspx

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

Edit: 编辑:

You can try to move allocated pages to the beginning of the log file first with 您可以尝试首先将已分配的页面移动到日志文件的开头

DBCC SHRINKFILE ('Wxlog0', NOTRUNCATE) DBCC SHRINKFILE('Wxlog0',NOTRUNCATE)

and after that 在那之后

DBCC SHRINKFILE ('Wxlog0', 1) DBCC SHRINKFILE('Wxlog0',1)

Try creating another full backup after you backup the log w/ truncate_only (IIRC you should do this anyway to maintain the log chain). 在备份日志后尝试创建另一个完整备份w / truncate_only(IIRC你应该这样做以保持日志链)。 In simple recovery mode, your log shouldn't grow much anyway since it's effectively truncated after every transaction. 在简单恢复模式下,您的日志不应该增长太多,因为它在每次事务后都会被有效地截断。 Then try specifying the size you want the logfile to be, eg 然后尝试指定您希望日志文件的大小,例如

-- shrink log file to c. 1 GB
DBCC SHRINKFILE (Wxlog0, 1000);

The TRUNCATEONLY option doesn't rearrange the pages inside the log file, so you might have an active page at the "end" of your file, which could prevent it from being shrunk. TRUNCATEONLY选项不会重新排列日志文件中的页面,因此您可能在文件的“末尾”有一个活动页面,这可能会阻止它缩小。

You can also use DBCC SQLPERF(LOGSPACE) to make sure that there really is space in the log file to be freed. 您还可以使用DBCC SQLPERF(LOGSPACE)来确保要释放的日志文件中确实存在空间。

Put the DB back into Full mode, run the transaction log backup (not just a full backup) and then the shrink. 将数据库重新置于完全模式,运行事务日志备份(不仅仅是完整备份),然后进行收缩。

After it's shrunk, you can put the DB back into simple mode and it txn log will stay the same size. 缩小后,您可以将数据库恢复为简单模式,并且日志将保持相同的大小。

您不能缩小小于其最初创建的大小的事务日志。

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

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