How to clear MSSQL transaction log files

Source: Internet
Author: User
Three methods: 1. Delete LOG1): Detach Database Enterprise Manager-> server-> database-> right-click-> detach database 2): delete LOG file 3 ): attach Database Enterprise Manager-> server-> database-> right-click-> attach database this method to generate a new LOG with a size of more than 500 K and then set the database to automatically contract 2. Clear the day

Three methods: 1. Delete LOG 1): Detach Database Enterprise Manager-> server-> database-> right-click-> detach database 2): delete LOG file 3 ): attach Database Enterprise Manager-> server-> database-> right-click-> attach database this method to generate a new LOG with a size of more than 500 K and then set the database to automatically contract 2. Clear the day

Three methods:

1. Delete LOG

1): Choose "detach Database"> "server"> "Database"> "right-click"> "detach Database ".
2): delete LOG files.
3): attach the database to the Enterprise Manager. Choose Server> database> right-click and choose attach database.

This method generates a new LOG with a size of more than 500 kb.

Then set the database to automatically contract

2. Clear logs

Dump transaction database name WITH NO_LOG

Again: Enterprise Manager -- Right-click the database you want to compress -- all tasks -- contract database -- contract file -- Select log file -- select to 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.

3. If you want to prevent it from increasing in the future

Enterprise Manager-> server-> database-> properties-> transaction log-> limit file growth to 2 MB


Quick solutions for full SQL server database logs

First, a complex method is provided to compress logs and database files as follows:

1. Clear logs
Dump transaction database name WITH NO_LOG
2. truncate transaction logs:
Backup log database name WITH NO_LOG
3. Compress database files (if not compressed, the database files will not be reduced
Enterprise Manager -- Right-click the database you want to compress -- all tasks -- contract database -- contract file
-- Select log file -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to be shrunk is displayed. Enter this number directly and click OK.
-- Select data file -- select to shrink to XXM in the contraction mode. Here, a minimum number of MB allowed to be shrunk is displayed. Enter this number directly and click OK.
You can also use SQL statements to complete
-- Shrink Database
Dbcc shrinkdatabase (customer profile)
-- Contract the specified data file. 1 is the file number. You can use this statement to query: select * from sysfiles
Dbcc shrinkfile (1)
4. To minimize log files (for SQL 7.0, this step can only be performed in the query analyzer)
A. Separate the database:
Enterprise Manager -- server -- database -- Right-click -- detach Database
B. Delete LOG files in my computer
C. Additional database:
Enterprise Manager -- server -- database -- Right-click -- attach Database
This method generates a new LOG with a size of more than 500 K.
Or use the code:
The following example separates pubs and attaches a file in pubs to the current server.
A. Separation
Exec sp_detach_db @ dbname = 'pubs'
B. Delete log files
C. Attach
Exec sp_attach_single_file_db @ dbname = 'pubs ',
@ Physname = 'C:/Program Files/Microsoft SQL Server/MSSQL/Data/pubs. mdf'
5. In order to automatically contract in the future, make the following settings:
Enterprise Manager -- server -- Right-click Database -- Property -- option -- select "auto contract"
-- SQL statement setting method:
Exec sp_dboption 'database name', 'autowrite', 'true'
6. If you want to prevent the log from increasing too much in the future
Enterprise Manager -- server -- Right-click Database -- properties -- transaction log
-- Limit file growth to xM (x is the maximum data file size you allow)
-- SQL statement settings:
Alter database name modify file (name = logical file name, maxsize = 20)
Note:
Follow these steps. Do not follow these steps.
Otherwise, your database may be damaged.
Generally, steps 4 and 6 are not recommended.
Step 3 is not safe, and may damage the database or lose data
If the log reaches the upper limit in step 1, the subsequent database processing will fail and the log can be restored after being cleared.

In addition, we provide a simpler method, which I have tried and tried repeatedly. We recommend that you use it.
Simpler Method:
1. Right-built Database Properties window-fault recovery model-set to simple
2. Right-built database all tasks-shrink Database
3. Right-built Database Properties window-fault recovery model-set as large-capacity log records


Backup log xzfw_v7_maogang with NO_LOG;
Backup log xzfw_v7_maogang with TRUNCATE_ONLY;
Dbcc shrinkdatabase (xzfw_v7_maogang );


2. Use the Transact-SQL command to compress the database
You can use the dbcc shrinkdatabase and dbcc shrinkfile commands to compress the database. The dbcc shrinkdatabase command compresses the database, and the dbcc shrinkfile command compresses the specified files in the database.

(1) DBCC SHRINKDATABASE
The command syntax of dbcc shrinkdatabase is as follows:
Dbcc shrinkdatabase (database_name [, target_percent]
[, {NOTRUNCATE | TRUNCATEONLY}])
Parameters are described as follows:


Target_percent indicates the percentage of unused space in the database size after the database is compressed. If the specified percentage is too large and exceeds the proportion of space not used before compression, the database will not be compressed. In addition, the compressed database cannot be smaller than the initial capacity set by the database.
NOTRUECATE
After the database is reduced, the remaining space is retained in the database and will not be returned to the operating system. If this option is not selected, the remaining space is returned to the operating system.
TRUNCATEONLY
After the database is reduced, the remaining space is returned to the operating system. When using this command, SQL Server reduces the file to the last file allocation, but does not move any data files. If this option is selected, the target_percent option is invalid.
Example 6-14: the unused space of the compressed database mytest is 20% of the database size.
Dbcc shrinkdatabase (mytest, 20)
The running result is as follows:
DBCC execution completed. If DBCC printed error messages, contact your system administrator.

(2) DBCC SHRINKFILE
The dbcc shrinkfile command compresses files in the current database. The syntax is as follows:
Dbcc shrinkfile ({file_name | file_id}
{[, Target_size] |
[, {EMPTYFILE | NOTRUNCATE | TRUNCATEONLY}]})
Parameters are described as follows:

File_id
ID of the file to be compressed ). The file ID can be obtained through the FILE_ID () function or the Sp_helpdb system stored procedure described earlier in this chapter.
Target_size
Specifies the size of the compressed file. In MB. If this option is not specified, SQL Server will minimize the file size as much as possible.
EMPTYFILE
Indicates that this file is no longer used and all data in this file will be moved to other files in the same file group. After the command with this parameter is executed, the file can be deleted using the alter database command.
Other parameters NOTRUNCATE and TRUNCATEONLY have the same meaning as those in the dbcc shrinkdatabase command.
Example 6-15: compress the database file mydb_data2 in the database mydb to 1 MB. Use mydb dbcc shrinkfile (mydb_data2, 1)

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.