Shrinking MS SQL Server Tables

One of my application logs User Agent identifiers to a database table which, over the years, reached several gigabytes in size.

Trying to reduce the size without losing the information, I created a lookup table for these strings, calculated the foreign keys, and then set the identifiers to NULL.

But, no combination of DBCC SHRINKFILE or DBCC CLEANTABLE would actually cause the .mdf file to reduce its size.

This being the internets, I finally found the solution that worked for me in these posts

I tried

ALTER INDEX Index_name ON Table_Name REORGANIZE WITH (LOB_COMPACTION=ON)

and it worked. The operation needs to be executed on the CLUSTERED index, which is, unless another index created as clustered, the primary key by default. The alternative

ALTER INDEX ALL ON dbo.Mytable REBUILD

may achieve the same effect (I haven’t tried).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.