简体   繁体   English

如何控制SQL Server数据库日志文件大小?

[英]How to control SQL Server database log file size?

We are using MSSQL Server and our application has heavy data exchange every day; 我们正在使用MSSQL Server,我们的应用程序每天都要进行大量的数据交换。 approx. 大约 20K rows new data per day and the database “Log file size” keeps increasing likewise. 每天有2万行新数据,数据库“日志文件大小”也在不断增加。 We delete the data after processing to keep the DB size under control, however the “DB Log file” keeps on increasing. 为了控制数据库大小,我们在处理后删除了数据,但是“数据库日志文件”一直在增加。

We are manually executing following script to shrink “DB Log file” size. 我们正在手动执行以下脚本来缩小“数据库日志文件”的大小。 Also only DB owner can execute this process. 此外,只有数据库所有者才能执行此过程。

Is there permanent fix to auto-control the “DB Log file” size? 是否有永久性修复程序可以自动控制 “数据库日志文件”的大小? Let's say, we want it to allocate 4GB max. 假设,我们希望它最多分配4GB。 It should auto-wipe out the older logs . 它应该自动清除较旧的日志

USE db1;
GO

ALTER DATABASE db1
SET RECOVERY SIMPLE;
GO
--first parameter is log file name and second is size in MB
DBCC SHRINKFILE (db1_log, 999);

ALTER DATABASE db1
SET RECOVERY FULL;
GO

Regards, Raj 问候,拉吉

I see this: 我看到这个:

SET RECOVERY FULL;

In FULL recovery mode, a full database backup does not include the transaction log . 在完全恢复模式下,完整的数据库备份不包括事务日志 Additionally, the server will not under any circumstances remove log entries that are not backed up. 此外,服务器在任何情况下都不会删除未备份的日志条目。 This means the log file will just keep growing an growing. 这意味着日志文件将不断增长。

Is there permanent fix to auto-control the “DB Log file” size? 是否有永久性修复程序可以自动控制“数据库日志文件”的大小?

Yes. 是。 To fix this problem, you need to do separate Transaction Log backups... and do them often . 要解决此问题,您需要单独进行事务日志备份... ,并经常进行备份。 This will allow the log file to stay at a reasonable size... probably much less even than your 4GB. 这将使日志文件保持合理的大小,甚至可能比您的4GB小得多。

Be careful. 小心。 The first time you take a backup after making the change, you can get a rather large backup file that might take a while to finish and put significant load on the database. 进行更改后第一次进行备份时,您可能会获得一个相当大的备份文件,该文件可能需要一段时间才能完成,并给数据库带来巨大的负载。 But after that first run, things will quiet down if you've chosen a reasonable schedule. 但是,在第一次运行之后,如果您选择了合理的时间表,一切都会平静下来。

Additionally, once you complete your first transaction log backup on the new schedule, you can finally shrink your transaction log file. 此外,一旦您按照新的时间表完成了第一次事务日志备份,就可以最终收缩事务日志文件。 There is no point in shrinking the file before then... it's still using all that space, and even if it weren't it would just grow large again. 在此之前,缩小文件没有意义……它仍在使用所有空间,即使不是,它也会再次变大。 Once you'd done this initial shrink, you really shouldn't do it again . 一旦完成了初始收缩, 您就不应该再做一次 You want your maintenance schedule set up so you don't ever need to manually shrink the file, and you only even shrink it this time to get back to a reasonable state after the earlier mistake. 您希望设置维护计划,这样就无需手动收缩文件,而这次甚至只需收缩文件即可在先前的错误后恢复到合理的状态。

There are 2 ways how SQL Server clear the t-log: SQL Server清除t-log的方法有两种:

  1. database using SIMPLE recovery model - when a checkpoint occurs, log will clear 使用SIMPLE恢复模型的数据库-发生检查点时,日志将清除

  2. database using FULL or BULK_LOGGED recovery model - when you issue a log backup 使用FULL或BULK_LOGGED恢复模型的数据库-发出日志备份时

The only time the t-log will shrink if you issue SHRINKFILE. 如果您发出SHRINKFILE,则t-log只会缩小一次。

You can pre-size your log file to 4GB and auto-growth to 4GB example. 例如,您可以将日志文件的大小预先设置为4GB,然后自动增长为4GB。

USE [master]
GO
ALTER DATABASE [DB] MODIFY FILE ( NAME = N'DB_log', SIZE = 4096000KB , FILEGROWTH = 4096000KB )
GO

Your file growth should be set to In Megabytes and not In Percent . 您的文件增长应设置为In Megabytes而不是In Percent 在此处输入图片说明

you can limit the file growth to a fix size but I would not recommended to set this. 您可以将文件增长限制为固定大小,但我不建议设置此大小。 this might stop the application from inserting once you reach the size. 一旦达到大小,这可能会阻止应用程序插入。

If you can tolerate 1 day of downtime/data loss, SIMPLE recovery will work for you. 如果您可以忍受1天的停机时间/数据丢失,那么SIMPLE恢复将为您服务。 But if you have a 24/7 shop and needs to recover point-in-time, you need to set your database to FULL recovery (log backups needed to cleanup your t-logs) and ask you business about RPO/RTO requirements. 但是,如果您有一家24/7的商店,并且需要恢复时间点,则需要将数据库设置为完全恢复(清理t-log所需的日志备份),并向您的企业询问有关RPO / RTO的要求。

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

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