Unable to shrink Data File in SQL Server (Taking too much time)

Yassir 201 Reputation points
2023-11-09T20:53:23.6166667+00:00

I am using :

SQL Server 2017 Standard edition

Windows Server 2016 (Standard)

Database Size: 1.2TB

Used space from the Primary Data File: 300GB

Unused space in a Primary Data File : 874GB

Recovery Model: Simple

Capture

I am trying to shrink the Data file to 600GB by using a following Command:

DBCC SHRINKFILE (N'TestDB' , 614400)

And there is no blocking and no other activities are happening on this database.

The above shrink operation is been in the process since last 9hrs and still not completed. So can anyone tell me what needs to be done at this point of time.

How much time should be taken by this shrink operation?

Any best solution to do operation ?

Thanks for help

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,158 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,666 questions
{count} votes

4 answers

Sort by: Most helpful
  1. AniyaTang-MSFT 12,446 Reputation points Microsoft Vendor
    2023-11-10T05:14:57.6633333+00:00

    Hi @Yassir

    I found this similar thread for you: https://stackoverflow.com/questions/43510333/unable-to-shrink-data-file-in-sql-server-taking-too-much-time, maybe you can use it as a reference.

    If the answer is helpful, please click Accept Answer and Up-Vote for the same. If you have any questions, please feel free to let me know.

    Best regards,

    Aniya

    0 comments No comments

  2. Olaf Helper 45,366 Reputation points
    2023-11-10T06:36:49.4+00:00

    The above shrink operation is been in the process since last 9hrs

    Shrinking can take some time, that's normal. Data pages must be rearranged & moved around, before the end of the file can be truncated.

    Shrink the file in smaller steps, e.g. shrink the file always minus 500 MB

    0 comments No comments

  3. Bruce (SqlWork.com) 68,306 Reputation points
    2023-11-17T21:42:40.2666667+00:00

    shrinking a db is very expensive. the moving one page must be done as an atomic and logged operation. moving one page requires updating all pages that reference it also. this updates need to committed to log before they can be committed to disk. so your talking millions of random database reads and writes.

    0 comments No comments

  4. RahulRandive 9,976 Reputation points
    2023-11-17T22:42:34.5433333+00:00

    Hi @Yassir

    Please refer to a similar thread where you can find a detailed answer.

    https://learn.microsoft.com/en-us/answers/questions/1353942/shrinking-a-large-data-file

    Thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.