Why does DBCC shrinkfile run for a long time?

Source: Internet
Author: User

After the data compression function is enabled today, it takes a long time to compress the database using DBCC shrinkfile. I believe many friends who use SQL server have encountered this problem. Why is it so time-consuming for SQL Server to shrink files?

 

From msdn, we can see that "DBCC shrinkfile is single-threaded and may take a long time ToComplete" (http://msdn.microsoft.com/en-us/library/dd894051 (V = SQL .100). aspx) that is, SQL
Server is a single-thread running file shrinking, even if you have multiple CPU performance does not help.

 

Here I did a test to open two queries and run the shrinkfile command at the same time. The second statement will report an error:

File ID 1 of Database ID 17 cannot be shrunk as it is eitherbeing shrunk by another process or is empty.

Dbccexecution completed. If DBCC printed error messages, contact your systemadministrator.

 

Three steps for file contraction (you can see in the SYS. dm_exec_request command column)

Step

Command

Description

1

Dbccspacereclaim

Clean up deferred allocations and purge empty extents preparing for data moves.

2

Dbccfilescompact

Moves pages beyond the target to before the target and truncate file as required.

3

Dbcclobcompact

Compacting the lob data.

 

SQL Server executes DBCC shrinkfile and uses 32 pages as a transcation. When the page being processed in the transaction is moved to the targe space, the transcation is commit and a new transcation starts. This avoids excessive Log File growth due to a long time of transcation. After the current transcation is rollback/cancel, the shrink operation only rolls back the current transaction (up to 32 pages ). In this way, if the shrink operation cannot be completed within the maintenance period, it can be divided into multiple batches and processed in part each time.

 

I found an article on how to quickly shrink database files from the Internet:

Http://www.sqlservercentral.com/articles/SHRINKFILE/71414/

How it works: SQL Server 2005 DBCC shrink * may take longer than SQL Server 2000: http://blogs.msdn.com/ B /psssql/archive/2008/03/28/how-it-works-sql-server-2005-dbcc-shrink-may-take-longer-than-sql-server-2000.aspx

 

 

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.