How to clear logs in the SQL Server database

Source: Internet
Author: User

Method 1:
1. Open the query analyzer and enter the command
Backup log database_name WITH NO_LOG
2. Choose "Enterprise Manager"> "right-click the database to be compressed"> "all tasks"> "shrink Database"> "contract file"> "select" Log File ">" contract to xxm "in the contract mode, here we will provide a minimum number of m that can be reduced. Enter this number directly and click OK.

Method 2:
Set checkpoints and automatically truncate logs

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 to open your Server --> double-click to open the database directory --> select your database name (such as the user database) cwbase1) --> 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 3: compress logs by SQL

Copy the code to the query analyzer, modify the three parameters (Database Name, log file name, and target log file size), and run

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

Method 4: delete a log file.

This method is risky because the log files of SQL server are not immediately written to the master database file. improper processing may cause data loss. 1. Disconnect all databases before the operation.
2. Database Separation
Database separation: Enterprise Manager-> server-> database-> cwbase1-> right-click-> database Separation
After separation, the cwbase1 database is deleted, but the data files and log files are retained.
3. delete a log physical file
Delete the physical LOG file and attach the database: Enterprise Manager> Server> database> right-click and choose attach database.
This method generates a new log with a size of more than 500 k.

Note: The first method is recommended. Before the operation, make sure that all operators have launched the system to disconnect the database.

Before performing the preceding operations, back up data!

1. SQL server 2005 clear log statements

Dump transaction database name with no_log
Backup log database name with no_log
Dbcc shrinkdatabase (database name)

2. SQL server 2008 clear log statements

Sp_dboption database name, "trunc. log on chkpt.", true
Checkpoint
Sp_dboption database name, "autoshrink", true

To clear the SQL Server database log files, follow these steps:

1. Uninstall the database first:
EXEC sp_detach_db 'database _ name', 'true'
Delete the Log file corresponding to the database;
Finally, register the database to the system:

EXEC sp_attach_db @ dbname = n' database _ name ',
@ Filename1 = n'e: \ mssql7 \ data \ database_name_data.mdf'

2. Right-click the database and choose "all tasks"> "shrink Database"> "contract file"> "LOG.

3. How to clear SQL Server database logs:

* ****** The following is the forwarded email *****

The shrinking of log files is not immediate in SQL Server 7.0.
Shrinking of log files does not occur until the active portion of
Log moves. As updates are saved med on the database, the shrink
Operation occurs at checkpoints or transaction log backups. Each log
File is marked with the target_percent for the shrink operation. Each
Subsequent log backup or log truncation attempts to shrink the file
Bring its size as close to the target_percent as possible. Because a log
File can be shrunk only to a virtual log file boundary, it may not be
Possible to shrink a log file to a size smaller than the size of
Virtual log file even if it is not being used. Please refer to SQL Book
Online for the details.

RESOLUTION

Below script will help to shrink the log file immediately, pls keep it
Running for 3 ~ 4 minutes and then stop it manually.

\ * Run "select fileid, name, filename from.. sysfiles" to get
The fileid which you want to shrink *\

Use
Go
Dbcc shrinkfile (fileid, notruncate)
Dbcc shrinkfile (fileid, truncateonly)
Create table t1 (char1 char (4000 ))
Go
Declare @ I int
Select @ I = 0
While (1 = 1)
Begin
While (@ I <100)
Begin
Insert into t1 values ('A') select @ I = @ I + 1
End
Truncate table t1
Backup log with truncate_only
End
Go

***** End of forwarding content *****

SQL Server database log cleanup clear sqlserver2005 log

Sometimes, when the system runs for a long time, we restore the backup database and find that the data files and log files in the Database become large, especially log files. You can use either of the following methods to clear SQL Server database logs:

Method 1: manually clear sqlserver2005 logs

1. Right-click the database to which logs are cleared, such as "TestDB", and click [new query (Q)]
2. Enter the following SQL statement. "TestDB" indicates the database name.
Dump transaction TestDB WITH NO_LOG
3. Execute the SQL statement. After successful execution, continue with the following operations:
4. Right-click the database node and choose [task (T)]-> [shrink (S)]-> [file (F)]
5. in the displayed "shrink file" dialog box, select "file type (T)" as "log ", select "shrink operation" and "re-organize the page (O) before releasing unused space )"
6. In the "shrink file to (K)" text box, enter the minimum size value as prompted, and click [OK.

Method 2: use the tool software SqlServer log to clear the database logs of various versions from SQL Server 3.0 to SQL Server 6.5. The method is very simple; sqlServer log cleanup expert Green Edition V3.5:

Http://www.jb51.net/softs/21840.html

Method 1 is relatively difficult to operate, but the log size can be customized. After the log is cleared, the corresponding database data file will also become smaller and the data will not be lost. method 2 is more convenient, the log files in the database can be cleared to 1 MB;

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.