简体   繁体   English

什么时候应该在SQL Server中对日志文件使用自动收缩?

[英]When should one use auto shrink on log files in SQL Server?

I have had a few problems with log files growing too big on my SQL Servers (2000). 我的SQL Server(2000)上的日志文件过大,遇到了一些问题。 Microsoft doesn't recommend using auto shrink for log files, but since it is a feature it must be useful in some scenarios. Microsoft不建议对日志文件使用自动收缩,但是由于它是一项功能,因此在某些情况下必须有用。 Does anyone know when is proper to use the auto shrink property? 有谁知道什么时候该使用自动收缩属性?

Your problem is not that you need to autoshrink periodically but that you need to backup the log files periodically. 您的问题不是您需要定期自动收缩,而是需要定期备份日志文件。 (We back ours up every 15 minutes.) Backing up the database itself is not sufficient, you must do the log as well. (我们每15分钟备份一次。)仅备份数据库本身是不够的,您还必须进行日志记录。 If you do not back up the transaction log, it will grow until it takes up all the space on the drive. 如果不备份事务日志,它将一直增长,直到占用驱动器上的所有空间。 If you back it up, it frees the space to be reused (you will still probably need to shrink after the first backup to get the log down to a more reasonable size). 如果您对其进行备份,则可以释放空间以供重用(在第一次备份后,您可能仍需要收缩以将日志减小到更合理的大小)。 If you don't need to be able torecover from transactions (which you should need to be able to do unless your entire database consists of tables that are loaded from another source and can easily be re-loaded.), then set your log to simlpe recovery mode. 如果您不需要从事务中恢复(除非整个数据库都包含从另一个源加载的表并且可以轻松地重新加载,否则您应该能够执行此操作),然后将日志设置为simlpe恢复模式。

One reason why autoshrinking isn't so good an idea is that you will be growing the transaction log frequently which slows down performance. 自动收缩并不是一个好主意的原因之一是,您将频繁增长事务日志,这会降低性能。 IF you back up the log, one you get to a relatively stable size (the amount of space normally used by the transaction log in the time period between backups), then the log will only need to grow occasionally if there are an unusually heavy amount fo transactions. 如果您备份日志,则可以达到相对稳定的大小(在两次备份之间的时间段内事务日志通常使用的空间量),那么仅当日志量非常大时,才偶尔需要增加日志交易。

My take on this is that auto-shrink is useful when you have many fairly small databases that frequently get larger due to added data, and then have a lot of empty space afterwards. 我的看法是,当您有许多相当小的数据库(由于添加数据而经常变得更大),然后又有很多空白空间时,自动收缩非常有用。 You also need to not mind that the files will be fragmented on the disk when they frequently grow and shrink. 您也不必介意文件在频繁增长和收缩时会在磁盘上碎片化。 I'd never use auto-shrink on a critical database or one larger than 2 GB, as you never know when the shrink operation will kick in, and access to the database will be blocked until the shrink has completed. 我绝不会在关键数据库或大于2 GB的关键数据库上使用自动收缩,因为您永远不知道何时会执行收缩操作,并且在收缩完成之前将无法访问数据库。

You should never have autoshrink turned on. 您永远不应该打开自动收缩功能。 It causes performance degradation in several ways. 它以多种方式导致性能下降。 The file-system and indexes become fragmented and it is very resource intensive. 文件系统和索引变得碎片化,并且非常耗费资源。 It is also not necessary if you manage your backups correctly. 如果正确管理备份,也没有必要。

Read this answer from Paul Randal on Server Fault and Just Say No To Auto-Shrink !! 请阅读Paul Randal的有关服务器故障的答案,拒绝自动收缩

I used to use it when we had a demo version of a huge database that took up a lot of space on the laptop, so we used it to keep the size down. 当我们有一个巨大的数据库的演示版占用笔记本电脑上很多空间时,我曾经使用它,因此我们使用它来减小规模。

The key is to use it only when the data is basically throw away. 关键是仅在基本丢弃数据时才使用它。

You should truncate the logs periodically as a part of your backup strategy. 您应该定期截断日志,作为备份策略的一部分。

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

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