SQL2005 compression to clear logs and database file size

Source: Internet
Author: User

The following database names refer to the names of the databases you want to process.

1. Clear logs

Open the query analyzer and enter the command dump transaction database name WITH NO_LOG2.
Open the Enterprise Manager and right-click the database you want to compress -- all tasks -- shrink database -- shrink file -- select Log File -- select to shrink to XXM in the shrink mode,
Here we will provide a minimum number of M that can be reduced. Enter this number directly and click OK.

The code is as follows: Copy code

Dump transaction database name WITH NO_LOG


2. Truncate transaction logs:

The code is as follows: Copy code

Backup log Library 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

The code is as follows: Copy code

Dbcc shrinkdatabase (database name)

-- Contract the specified data file. 1 is the file number. You can use this statement to query: select * from sysfiles

The code is as follows: Copy code

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

The code is as follows: Copy code
EXEC sp_detach_db @ dbname = 'database name'

B. Delete log files

C. Attach

The code is as follows: Copy code
EXEC sp_attach_single_file_db @ dbname = 'database name ',
@ Physname = 'C: Program FilesMicrosoft SQL ServerMSSQLData database name. 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:

The code is as follows: Copy code
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)

Suddenly an error occurred in SQL 2005: The transaction log of the database 'mybase _ db' is full. To find out why the space in the log cannot be reused, see the log_reuse_wait_desc column in sys. databases.

The code is as follows: Copy code


-- Back up the database first
-- Truncate transaction logs
Backuplog mybase_dbwithno_log
Go
-- Shrink database
Dbccshrinkdatabase (mybase_db)
Go
OK.
----------------
-- SQL2005 automatic backup script
Declare @ DBName varchar (200)
Set @ DBName = 'reportserver $ SQL2005'
-- Truncate logs
Dump transaction @ DBName WITH NO_LOG
-- Shrink database
Dbcc shrinkdatabase (@ DBName, TRUNCATEONLY)
-- Backup database
USE master
Declare @ Version varchar (20)
Declare @ DateAppend varchar (20)
Declare @ BasePath varchar (200)
Declare @ BakPath varchar (200)
-- Set the basic directory for backup
Set @ BasePath = 'F: tmp'
-- Set the version. The backup of each version is stored in different places.
Set @ Version = 'v6. 1'
-- Set the full backup path
Set @ BakPath = @ BasePath + ''+ @ Version + 'DB. Bak'
USE master
-- Creates a backup device. If yes, you do not need to create a backup device.
If exists (select * from sysdevices where name = 'ctos _ DB_Bak ')
Begin
EXEC sp_dropdevice 'ctos _ DB_Bak'
Declare @ tmpcmd varchar (100)
Set @ tmpcmd = 'del' + @ BakPath
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'XP _ your shell', 1
RECONFIGURE
Exec master .. xp_mongoshell @ tmpcmd
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
EXEC sp_configure 'XP _ your shell', 0
RECONFIGURE
End
EXEC sp_addumpdevice 'disk', 'ctos _ DB_Bak ', @ BakPath
-- Backup database
Backup database @ DBName TO CTOS_DB_Bak

Compressed logs
1: truncate transaction logs:
Backup log database name WITH NO_LOG
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: delete LOG
1: detach Database Enterprise Manager-> server-> database-> right-click-> detach database
2: delete LOG files
3: attach the database Enterprise Manager-> server-> database-> right-click-> attach database
This method generates a new LOG with a size of more than 500 KB.
Then set the database to automatically contract

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.