Several ways to delete log files from SQL Server 2005

Source: Internet
Author: User
Tags system log

Friends who use the MS database will know a problem, C disk space is very easy to fill up. Because the system disk on the partition is relatively small, this is because the database during the use of the log files will increase, so that the performance of the database decreased, and occupy a lot of disk space. The SQL Server database has log files, log files that record user actions to modify the database. You can clear the database log by simply deleting the log file and emptying the log.

One, delete log

1, separate the database. Before separating the database, you must make a full backup of the database, choose the database--------task--separation.

Tick Delete connection

The detached database will not be visible in the database list after separation.

2. Delete log file

3, attach the database, the additional time will be reminded that the log file can not be found.

LDF file to delete database information information:

After attaching the database, a new log file log is generated and the size of the new log file is 504K.

You can also do this by using a command:

Use master;

exec sp_detach_db @dbname = ' TestDB ';

exec sp_attach_single_file_db @dbname = ' TestDB ', @physname = ' D:program filesmicrosoft SQL ServerMSSQL10.SQL2008MSSQLDATATestDB.mdf '

Second, empty the log

This command is supported in SQL Server 2005 and 2000, and SQL Server 2008 does not support this command.

DUMP TRANSACTION TestDB with no_log

Third, shrink the database file

DBCC shrinkfile (' Testdb_log ', 1)

Iv. Truncate the transaction log

BACKUP LOG TestDB with no_log

This command is also not supported in SQL Server 2008 and can be used in SQL Server 2005 and 2000.

Clear the SQLServer2005 log file

--it is best to back up the log and later recover the data through the log ...

The following are log processing methods

Generally do not recommend to do 4th, 62 steps

4th step is unsafe, it is possible to corrupt the database or lose data

6th step if the log reaches the upper limit, subsequent database processing will fail, and the log will not be restored until it is cleaned.

--*/

--All of the following library names refer to the library name of the database you are working on

1. Empty log

DUMP TRANSACTION Library name with NO_LOG

2. Truncate the transaction log:

BACKUP LOG Library 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 (library name)

--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 pubs and attaches a file in pubs to the current server.

A. Separation

EXEC sp_detach_db @dbname = ' Library name '

B. Deleting a log file

C. Additional

EXEC sp_attach_single_file_db @dbname = ' Library name ',

@physname = ' C:Program filesmicrosoft SQL servermssqldata library name. 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 ' library 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)

SQL Server database use for a long time will cause log files become large, want to back up the database, it is difficult to send to anyone

Run the following statement to clear the log file only about 1M of space left.

DUMP TRANSACTION database name with NO_LOG

DBCC shrinkdatabase (' database name ', truncateonly)

Do not restart the SQL service, remove SQL Server system log

SQL Server's system log is too large, which can cause a series of problems, such as the inability of the SQL Servers to start. Today, I encountered this problem, online search for a moment, the solution is to delete it, but the current errorlog is being used by SQL can not delete ah, to delete can only stop SQL Server, is there no way to do it?

The answer is yes: Use the following stored procedure: EXEC sp_cycle_errorlog

Comments

Each time you start SQL Server, the current error log is renamed to Errorlog.1;errorlog.1 as Errorlog.2,errorlog.2 becomes errorlog.3, and so on. Sp_cycle_errorlog allows you to loop through the error log file without having to stop and then start 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.