A few days ago, I also encountered the problem that the log file is too large. the actual size of the database is 600 mb, and the actual size of the log file is 33 MB, but the occupied space of the log file is 2.8 GB !!!
I tried a variety of methods, such as shirnk database and truncate log file. In any case, this should be a bug of SQL Server.
Then find the followingCodeTo reduce the size of the log file. Copy the code to the query analyzer and modify the three parameters (Database Name, log file name, and target log file size ), run the command (I have used it many times)
-----
Set nocount on
Declare @ logicalfilename sysname,
@ Maxminutes int,
@ Newsize int
Use Marias -- Name of the database to be operated
select @ logicalfilename = 'marias _ log', -- log file name
@ maxminutes = 10, -- limit on time allowed to wrap log.
@ newsize = 100 -- the size of the log file you want to set (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) + '8 K 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) + '8 K pages or '+
Convert (varchar (30), (size * 8/1024) + 'mb'
From sysfiles
Where name = @ logicalfilename
Drop table dummytrans
Set nocount off