Compressing ms SQL Server 2000 database logs [network collection]

Source: Internet
Author: User
Reposted from the msdn Forum
/* -- Pay special attention

 

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.
--

*/

 

 

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

1 . Clear logs
Dump Transaction Mydpc With No_log

 

 

2 . Truncate transaction log:
Backup Log Mydpc With No_log

 

 

3 . Shrink 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 shrink 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 shrink is displayed. Enter this number directly and click OK.

You can also use SQL statements to complete
-- Shrink Database
DBCC Shrinkdatabase (database name)

 

 

-- Shrink the specified data file. 1 indicates the file number. You can use this statement to query: Select * From sysfiles
DBCC Shrinkfile ( 1 )

 

 

4 . To maximize the log file size (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 useCode:
The following example separates pubs and attaches a file in pubs to the current server.

A. Separation

Exec Sp_detach_db @ Dbname = ' Database Name '

 

B. Delete log files

C. Attach

Exec Sp_attach_single_file_db @ Dbname = ' Database Name ' ,
@ Physname = ' C: \ Program Files \ Microsoft SQL Server \ MSSQL \ data \ database name. MDF '

 

 

5 In order to automatically contract in the future, make the following settings:
Enterprise Manager -- Server -- Right-click Database -- properties -- options -- select "auto contract"

-- SQL statement setting method:
Exec Sp_dboption ' Database Name ' , ' Autoshrink ' , ' True '

 

 

6 . If you want to avoid increasing the log size 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 Database Name modify File (Name = Logical file name, maxsize = 20 )


Bytes ----------------------------------------------------------------------------------------------------------------------
 Step 1: Check the log size before shrinking:
Select *
From Sysfiles
Where Name Like ' % Log % '
Go

 

Step 2: Set the database recovery mode to "simple ":

Alter Database Database Name Set Recovery simple
Go

 

Step 3: run the checkpoint command to write the dirty page to the database:

Checkpoint
Go

Step 4: truncate logs:
Backup Log Database Name With No_log
Go

 

Step 5: record the log name to prepare for the next step:

 

Select Name
From Sysfiles
Where Name Like ' % Log '
Go

Step 6: contract the log file and release unused space to the operating system:
DBCC Shrinkfile (file name, size required)
Go

 

Step 7: Check whether the log size has reached the required size:

Select *
From Sysfiles
Where Name Like ' % Log % '
Go

 

Subsequent steps:

1 ) Check your main database to see if the log growth is out of control;
2 ) View the above Code and make necessary modifications and tests to meet your database requirements;
3 ) Regular database shrinking -- -- (Note: use it with caution !! It is not recommended to use it in the production environment .)
4 ) Continue to monitor the database size and the available space on the server.

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.