Sqlserver2005 log file is too large how to reduce _mssql2005

Source: Internet
Author: User
Tags getdate rtrim

The Sqlserver2005 log file is too large to reduce it by the following methods:

Run the following three lines dbname for the database name:

Backup LOG Dbnamewith No_log
Backup LOG Dbnamewith truncate_only
DBCC Shrinkdatabase (dbname)

Log file reduced to 1MB

SQL Server emptying log files 2011-04-06 23:05

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: If you want to not let it grow later

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}]
}

--How to ask Hovertree.com

--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 Database files (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

/*--calls 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, N "isprocedure") = 1)
drop procedure [dbo].[ P_COMPDB]
go create proc p_compdb
@dbname sysname, the name of the database to be compressed
@bkdatabase bit=1, because in the step of separating the logs, The database may be corrupted, so you can choose whether to automatically database
@bkfname nvarchar (260) = ""--backed up file name, if not specified, automatically back 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 does not reduce exec ("DBCC shrinkdatabase ([+ @dbname +])")--4. Set automatic shrink exec ("exec sp_dboption" "" + @dbname + "" 
"," "AutoShrink" "," "TRUE" "" "--there is a certain danger to the following steps, and you can choose whether these steps should be--5. Detach database If @bkdatabase =1 begin if IsNull (@bkfname," ") =" " Set @bkfname = @dbname + "_" +convert (Varchar,getdate (), 112) +replace (CONVERT (Varchar,getdate (), 108), ":", "") Select
hint = "BACKUP database to SQL default backup directory, Backup filename:" + @bkfname EXEC ("BACKUP database [" + @dbname + "] to disk=" "+ @bkfname +" "" ") end--Detach 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 file declare @fname nvarchar (i), @s varchar (8000) DECLARE TB cursor Local to select fname from #t where type=64 the open TB fetch next from TB to @fname while @ @fetch_status =0 begin set @s= Del "" "+rtrim (@fname) +" "" Exec master. xp_cmdshell @S,no_output fetch NEXT from TB to @fname end close TB deallocate TB--Append database set @s= "" Declare TB cursor local for SELECT FName from #t where type=0 the open TB fetch next from TB to @fname while @ @fetch_status =0 begin set @s=@s+, +rtrim (@fn  AME) + "" "" "" "fetch next from TB to @fname end close TB deallocate TB exec (" sp_attach_single_file_db "" "+ @dbname +" "" "+@s) go

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.