Unable to relcaim disk space after deleting the records from tables that has column QUERY_BLOB

Kranthi DBA 221 Reputation points
2021-06-04T12:26:08.567+00:00

We are seeing an issue wherein millions of records are deleted from the tables that have column QUERY_BLOB(data type ntext), but the table usage is still the same. We have shrunk the files, rebuild indexes using the offline option on those tables, but still unable to see any free space that is released.

Ex: Table1 has totalspaceMB as 50000 MB.

Even after deleting the records, the totalspaceMB is the same.

Please advise if any one has come across this issue and how to fix it.

Thanks in Advance!

SQL Server | Other
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Olaf Helper 47,516 Reputation points
    2021-06-04T14:12:28.367+00:00

    Is that a Heap Table = table without a clustered index?
    Then you can rebuild the table to get back the unused space. During rebuild the complete table is locked and if it fails there is a risk of lossing data, so better test it before you do it in prod.

    ALTER TABLE dbo.YourTable WITH REBUILD;
    

  2. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-06-04T21:36:52.217+00:00

    You may want to try ALTER INDEX REORGANIZE which does LOB_COMPACTION by default. REBUILD does not do that.

    Beware through that shrink operations can run very slow when you have LOB data, so you may end up with having to copy the data to a new database.


  3. Cris Zhan-MSFT 6,661 Reputation points
    2021-06-07T06:32:01.07+00:00

    Hello,

    After clearing a large amount of data through the delete operation, rebuild or create a new clustered index on the table to rearrange the data pages, which can effectively release the pages used by the table.

    But for a table that contains LOB data types (image, ntext, text), SQL Server uses a separate page to store these data. Index rebuilding will not affect them, and you may need to rebuild the table.

    Official documents (ntext, text, and image (Transact-SQL)) have stated:

    IMPORTANT! ntext, text, and image data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

    0 comments No comments

  4. Alexander Schäpper 96 Reputation points
    2021-10-21T12:29:11.993+00:00

    I ran this and it worked like a charm:

    DBCC SHRINKDATABASE (N'yourdatabase');  
    

    According:
    https://learn.microsoft.com/en-us/azure/azure-sql/database/file-space-manage#reclaim-unused-allocated-space

    0 comments No comments

  5. Tom Phillips 17,771 Reputation points
    2021-10-21T19:01:32.667+00:00
    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.