Ms SQL SERVER log growth is very fast, after a long time, log files will be very large, occupying a lot of hard disk space, so you need to regularly clear the log, you can use the following four methods:
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 then 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!