Ways to remove bulk-logged SQL Server

Source: Internet
Author: User
Tags empty file size getdate rtrim backup

Editor: Like our server to use tools to the log 1M programming, not too troublesome to see the manual method:

1: Delete Log

1: Detach Database Enterprise Manager-> Server-> database-> right key-> Detach database

2: Delete log file

3: Additional database Enterprise Manager-> Server-> database-> right key-> attached database

This method generates a new log with a size of more than 520 K

And then set this database to automatically shrink

or in code:

The following example separates 77169database and then attaches a file in 77169database to the current server.

EXEC sp_detach_db @dbname = 77169database

EXEC sp_attach_single_file_db @dbname = 77169database,

@physname = C:Program FilesMicrosoft SQL servermssqldata77169database.mdf

2: Empty the Log

DUMP TRANSACTION Library name with NO_LOG

Again

Enterprise Manager--right key to the database you want to compress--all tasks--shrink the database--Shrink the file--select log file--in the contraction way to choose to shrink to XXM, here will give a allow to shrink to the minimum m number, directly enter this number, OK

3: Don't Let it grow

Enterprise Manager-> Server-> database-> properties-> transaction log-> limit file growth to 2M

Automatically shrink the log, or you can use the following statement

ALTER database name

SET Auto_shrink on

The failover model is simple, with the statement

Use MASTER

Go

ALTER database name SET RECOVERY simple

Go

---------------------------------------------------------------------------------

Truncate transaction log:

BACKUP LOG {database_name | @database_name_var}

{

[With

{no_log | TRUNCATE_ONLY}]

}

--Compressed log and database file size

/*--Special attention

Please follow the steps and do not follow the steps above.

Otherwise, you may damage your database.

--*/

1. Empty log

DUMP TRANSACTION Library name with NO_LOG

2. Truncate the transaction log:

BACKUP LOG database name with NO_LOG

3. Shrink the database file (if not compressed, the database file will not be reduced

Enterprise Manager--right--the database you want to compress--all tasks--shrink the database--Shrink the file

--Select Log file--in the shrinkage method to choose to shrink to XXM, here will give a allow to shrink to the minimum m number, directly enter this number, you can determine

--Select the data file--in the contraction way to choose to shrink to XXM, here will give a allow to shrink to the minimum m number, directly enter this number, OK

You can also use SQL statements to complete

--Shrinking the database

DBCC shrinkdatabase (Customer information)

--Shrink the specified data file, 1 is the file number, you can query through this statement: SELECT * from Sysfiles

DBCC Shrinkfile (1)

4. In order to maximize the reduction of log files (if it is SQL 7.0, this step can only be done in Query Analyzer)

A. Detaching a database:

Enterprise Manager--server--database--right key--detach database

B. Delete log files on my Computer

C. Additional databases:

Enterprise Manager--server--database--right Key--additional database

This method will generate a new log with a size of more than 500 k

or in code:

The following example separates 77169database and then attaches a file in 77169database to the current server.

A. Separation

EXEC sp_detach_db @dbname = 77169database

B. Deleting a log file

C. Additional

EXEC sp_attach_single_file_db @dbname = 77169database,

@physname = C:Program FilesMicrosoft SQL servermssqldata77169database.mdf

5. In order to be able to automatically shrink later, do the following settings:

Enterprise Manager--server--right key database--Properties--Options--select "Auto Shrink"

--sql Statement setting:

EXEC sp_dboption database name, autoshrink, TRUE

6. If you want to not let it grow too big log

Enterprise Manager--server--right key database--attribute--transaction log

--Limit file growth to XM (x is the maximum data file size you allow)

How the--sql statement is set:

ALTER DATABASE name modify file (name= logical filename, maxsize=20)

-------------------------------------------------------------------------------------------

/*--Common stored procedures for compressed databases

Compress log and database file size

Because you want to detach the database

So stored procedures cannot be created in a compressed database/*

--Invoke the example

EXEC p_compdb Test

--*/

Use master-Note that this stored procedure is built in the master database

Go

if exists (SELECT * from dbo.sysobjects WHERE id = object_id (n[dbo].[ P_COMPDB]) and OBJECTPROPERTY (ID, nisprocedure) = 1)

drop procedure [dbo]. [P_compdb]

Go

Create proc P_compdb

@dbname sysname--The name of the database to compress

@bkdatabase bit=1, because the database may be corrupted in the steps of separating the logs, so you can choose whether to automatically database

@bkfname nvarchar (260) =--Backup file name, if not specified, automatically backed up to the default backup directory, backup file name: Database name + Date Time

As

--1. Empty log

EXEC (DUMP TRANSACTION [+ @dbname +] with no_log)

--2. Truncate the transaction log:

EXEC (BACKUP LOG [+ @dbname +] with no_log)

--3. Shrink the database file (if not compressed, the database file will not be reduced

EXEC (DBCC shrinkdatabase ([+ @dbname +]))

--4. Set Auto Shrink

EXEC (exec sp_dboption + @dbname +,autoshrink,true)

--there is a certain danger behind the steps, and you can choose whether these steps should

--5. Detach the database

If @bkdatabase =1

Begin

If IsNull (@bkfname,) =

Set @bkfname = @dbname +_+convert (varchar,getdate (), 112)

+replace (CONVERT (Varchar,getdate (), 108),:,)

Select message = BACKUP database to SQL default backup directory, Backup filename: + @bkfname

EXEC (Backup database [+ @dbname +] to disk=+ @bkfname +)

End

--Separate processing

CREATE table #t (fname nvarchar), type int)

EXEC (INSERT into #t select filename,type=status&0x40 from [+ @dbname +] ... Sysfiles)

EXEC (sp_detach_db + @dbname +)

--Delete log files

declare @fname nvarchar (@s), varchar (8000)

Declare TB cursor Local to select fname from #t where type=64

Open TB

FETCH NEXT from TB into @fname

While @ @fetch_status =0

Begin

Set @s=del "+rtrim (@fname) +"

EXEC master.. xp_cmdshell @s,no_output

FETCH NEXT from TB into @fname

End

Close TB

Deallocate TB

--Additional databases

Set @s=

Declare TB cursor Local to select fname from #t where type=0

Open TB

FETCH NEXT from TB into @fname

While @ @fetch_status =0

Begin

Set @s=@s+,+rtrim (@fname) +

FETCH NEXT from TB into @fname

End

Close TB

Deallocate TB

EXEC (sp_attach_single_file_db + @dbname ++@s)

Go

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.