前幾天也碰到日志文件過大的問題,數(shù)據(jù)庫(kù)實(shí)際大小為600m, 日志文件實(shí)際大小為33m, 但日志文件占用空間為2.8g!!!
試了多種方式,shirnk database, truncate log file, 都沒辦法將文件縮小。無論如何,這應(yīng)該算sql server的一個(gè)bug吧。
后來找到下面的代碼,就可以將日志文件縮小到自己想要的大小了。把代碼copy到查詢分析器里,,然后修改其中的3個(gè)參數(shù)(數(shù)據(jù)庫(kù)名,日志文件名,和目標(biāo)日志文件的大小),運(yùn)行即可(我已經(jīng)用過多次了)
-----
set nocount on
declare @logicalfilename sysname,
        @maxminutes int,
        @newsize int
use     marias             -- 要操作的數(shù)據(jù)庫(kù)名
select  @logicalfilename = 'marias_log',  -- 日志文件名
@maxminutes = 10,               -- limit on time allowed to wrap log.
        @newsize = 100                  -- 你想設(shè)定的日志文件的大小(m)
-- setup / initialize
declare @originalsize int
select @originalsize = size 
  from sysfiles
  where name = @logicalfilename
select 'original size of ' + db_name() + ' log is ' + 
        convert(varchar(30),@originalsize) + ' 8k pages or ' + 
        convert(varchar(30),(@originalsize*8/1024)) + 'mb'
  from sysfiles
  where name = @logicalfilename
create 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 not expired
      and @originalsize = (select size from sysfiles where name = @logicalfilename)  
      and (@originalsize * 8 /1024) > @newsize  
  begin -- outer loop.
    select @counter = 0
    while  ((@counter < @originalsize / 16) and (@counter < 50000))
      begin -- update
        insert dummytrans values ('fill log')  
        delete dummytrans
        select @counter = @counter + 1
      end   
    exec (@trunclog)  
  end   
select 'final size of ' + db_name() + ' log is ' +
        convert(varchar(30),size) + ' 8k pages or ' + 
        convert(varchar(30),(size*8/1024)) + 'mb'
  from sysfiles 
  where name = @logicalfilename
drop table dummytrans
set nocount off 
新聞熱點(diǎn)
疑難解答
圖片精選