SQL server 2008 SQL statement for log shrinking

Source: Internet
Author: User
Tags system log

Sql2008 contraction log is as follows:

The code is as follows: Copy code

Backup log dbname to disk = 'dbname. Log'
Dbcc shrinkfile ('dbname _ log', 1)

However, you must remember to perform the contraction only twice;

Shrink database files (if not compressed, the database files will not be reduced by the Enterprise Manager -- right-click the database you want to compress -- all tasks -- contract database -- contract files
-- 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
Dbcc shrinkdatabase (database name)

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

Of course, we can also directly clear database logs


SQL2008 contraction log

Because SQL2008 optimizes file and log management, the following statements can be run in SQL2005 but have been canceled in SQL2008:
(SQL2005)

The code is as follows: Copy code

Backup Log DNName with no_log
Go
Dump transaction DNName with no_log
Go
USE DNName
Dbcc shrinkfile (2)
Go
--------------------------------------------------------------
(SQL2008 ):
Log cleanup in SQL2008 must be performed in simple mode. After the cleanup is completed, the log is called back to full mode.
USE [master]
GO
Alter database DNName set recovery simple with NO_WAIT
GO
Alter database DNName set recovery simple -- SIMPLE mode
GO
USE DNName
GO
Dbcc shrinkfile (n'dnname _ log', 11, TRUNCATEONLY)
GO
USE [master]
GO

Alter database DNName set recovery full with NO_WAIT

GO

Alter database DNName set recovery full -- restore to FULL mode

GO

Advantage: It takes a short time to clear logs. A 90 GB log can be cleared in about minutes. After the log is cleared, make a full backup within minutes.
.
Disadvantage: However, it is best not to use this action frequently because its operation will cause system fragmentation. In normal state, logs and DIFF backups can be truncated.
The appropriate environment used by this statement: when the system LOG file is abnormally increased or the backup LOG time is too long, it may affect the production.

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.