Two Methods for clearing SQL Server logs

Source: Internet
Author: User

Two Methods for clearing SQL Server logs

Method 1

In general, the contraction of the SQL database does not greatly reduce the size of the database. Its main function is to shrink the log size. This operation should be performed regularly to avoid excessive database logs.

1. Set database mode to simple mode: Open the SQL Enterprise Manager, in the root directory of the console, open Microsoft SQL Server --> SQL Server group --> double-click your Server --> double-click to open the database directory --> select your database name (such as the Forum database) forum) --> then right-click and select Properties --> Select Options --> select "simple" in the fault recovery mode, and click OK to save

2. Right-click the current database to view the shrinking database in all tasks. Generally, the default settings in the database do not need to be adjusted. Click OK directly.

3. After shrinking the database, we recommend that you set your database attributes to the standard mode. The operation method is the same as the first one, because logs are often an important basis for restoring the database in case of exceptions.

Method 2

Set nocount on declare @ LogicalFileName sysname,

@ MaxMinutes INT,

@ NewSize INT

USE tablename -- Name of the database to be operated

SELECT @ LogicalFileName = 'tablename _ log', -- log File Name

@ MaxMinutes = 10, -- Limit on time allowed to wrap log.

@ NewSize = 1 -- 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

Related Article

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.