How to solve the problem of excessive SQL log files

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.