欢迎光临
我们一直在努力

『收藏』收缩日志文件的SQL脚本-数据库专栏,SQL Server

建站超值云服务器,限时71元/月

/* 本文由微软新闻组摘录下来的。一段非常有用的脚本。*/

前几天也碰到日志文件过大的问题,数据库实际大小为600m, 日志文件实际大小为33m,但日志文件占用空间为2.8g!!! 试了多种方式,shirnk database, truncate log file, 都没办法将文件缩小。无论如何,这应该算sql server的一个bug吧。

后来找到下面的代码,就可以将日志文件缩小到自己想要的大小了。把代码copy到查询分析器里,,然后修改其中的3个参数(数据库名,日志文件名,和目标日志文件的大小),运行即可(我已经用过多次了)—–set nocount ondeclare @logicalfilename sysname,@maxminutes int,@newsize int

use marias — 要操作的数据库名select @logicalfilename = marias_log, — 日志文件名@maxminutes = 10, — limit on time allowed to wrap log.@newsize = 100 — 你想设定的日志文件的大小(m)

— setup / initializedeclare @originalsize intselect @originalsize = sizefrom sysfileswhere name = @logicalfilenameselect original size of + db_name() + log is +convert(varchar(30),@originalsize) + 8k pages or +convert(varchar(30),(@originalsize*8/1024)) + mbfrom sysfileswhere name = @logicalfilenamecreate table dummytrans(dummycolumn char (8000) not null)

declare @counter int,@starttime datetime,@trunclog varchar(255)select @starttime = getdate(),@trunclog = backup log + db_name() + with truncate_only

dbcc shrinkfile (@logicalfilename, @newsize)exec (@trunclog)– wrap the log if necessary.while @maxminutes > datediff (mi, @starttime, getdate()) — time has notexpiredand @originalsize = (select size from sysfiles where name =@logicalfilename)and (@originalsize * 8 /1024) > @newsizebegin — outer loop.select @counter = 0while ((@counter < @originalsize / 16) and (@counter < 50000))begin — updateinsert dummytrans values (fill log)delete dummytransselect @counter = @counter + 1endexec (@trunclog)endselect final size of + db_name() + log is +convert(varchar(30),size) + 8k pages or +convert(varchar(30),(size*8/1024)) + mbfrom sysfileswhere name = @logicalfilenamedrop table dummytransset nocount off

 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 『收藏』收缩日志文件的SQL脚本-数据库专栏,SQL Server
分享到: 更多 (0)