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
- How to reduce size of SQL server table?
- Shrink database table after deleting fixed length columns
- How to reduce size of SQL Server table that grew from a datatype change
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).