cnblogs/dcrenl/SQL Server 2005与2008清空日志方法.html

1 line
2.0 KiB
HTML
Raw Normal View History

2024-09-24 12:43:01 +08:00
<p>SQL2008 的收缩日志&nbsp;<br />由于SQL2008对文件和日志管理进行了优化所以以下语句在SQL2005中可以运行但在SQL2008中已经被取消<br />SQL2005 清空日志的方法:<br />Backup&nbsp;Log&nbsp;DNName&nbsp;with&nbsp;no_log<br />go<br />dump&nbsp;transaction&nbsp;DNName&nbsp;with&nbsp;no_log<br />go<br />USE&nbsp;DNName&nbsp;<br />DBCC&nbsp;SHRINKFILE (2)<br />Go<br />--------------------------------------------------------------<br />SQL2008 清空日志的方法:<br />在SQL2008中清除日志就必须在简单模式下进行等清除动作完毕再调回到完全模式。<br />USE&nbsp;[master]<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br />&nbsp;&nbsp;&nbsp;&nbsp;ALTER&nbsp;DATABASE&nbsp;DNName&nbsp;SET&nbsp;RECOVERY SIMPLE&nbsp;WITH&nbsp;NO_WAIT<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br />&nbsp;&nbsp;&nbsp;&nbsp;ALTER&nbsp;DATABASE&nbsp;DNName&nbsp;SET&nbsp;RECOVERY SIMPLE&nbsp;&nbsp;&nbsp;--简单模式<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br />&nbsp;&nbsp;&nbsp;&nbsp;USE&nbsp;DNName&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br />&nbsp;&nbsp;&nbsp;&nbsp;DBCC&nbsp;SHRINKFILE (N'DNName_Log'&nbsp;,&nbsp;11, TRUNCATEONLY)<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br />&nbsp;&nbsp;&nbsp;&nbsp;USE&nbsp;[master]<br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;ALTER&nbsp;DATABASE&nbsp;DNName&nbsp;SET&nbsp;RECOVERY&nbsp;FULL&nbsp;WITH&nbsp;NO_WAIT<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;ALTER&nbsp;DATABASE&nbsp;DNName&nbsp;SET&nbsp;RECOVERY&nbsp;FULL&nbsp;&nbsp;--还原为完全模式<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;GO<br /><br />优点此清除日志所运行消耗的时间短90GB的日志在分钟左右即可清除完毕做完之后做个完全备份在分钟内<br />即可完成。<br />缺点: 不过此动作最好不要经常使用因为它的运行会带来系统碎片。普通状态下LOG和DIFF的备份即可截断日志。<br />此语句使用的恰当环境当系统的日志文件异常增大或者备份LOG时间太长可能影响生产的情况下使用。</p>