/* 本文由微软新闻组摘录下来的。一段非常有用的脚本。*/
前几天也碰到日志文件过大的问题,数据库实际大小为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
