Shrinking Database or File not Reclaiming unused space

  • Hello,

    First time poster, long time lurker. I thought this was an interesting enough question to ask.

    Pretty often we bring our clients databases (we sell software which backs onto an SQL server database) back in house to cut into test environments. Something we do to these databases is drop all the BLOB data so we're not dealing with several hundred GB of essentially useless data. We just don't need it for testing and dev work. I've carried out the following steps and have tried to shrink the data file and database without getting any space reclaimed. I'm wondering why this is happening and would like to figure out a way to reclaim the space without having to backup and restore the database. Any advice or thoughts would be welcome, this strikes me as a bit of an oddity and I'm sure there's a good reason for the behavior buried under the hood somewhere.

    -Switched to simple recovery mode (to avoid log bloat, maybe pointless when dropping the column?)

    -Dropped stats on blob columns

    -Dropped Blob Columns

    -Rebuilt indexes on the tables containing the blobs

    -Recreated the blob columns

    -Switched back to full recovery mode

    -Attempted file shrink on data file (no change in size)

    -Attempted database shrink (no change in size)

  • Interesting one this, there is a known issue around the removal of LOB data in sql server. Dropped LOB data is not removed immediately, there is a ghost process that runs in the background and removes the ghosted records.

    Truncating the table as far as i remember will clear the table but i couldn't say for sure when just dropping the column.

    Once you drop the column have you tried querying the system catalogs to see what allocated, used and reserved space sql server thinks the table\index object has?

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Perry Whittle (4/16/2013)


    Once you drop the column have you tried querying the system catalogs to see what allocated, used and reserved space sql server thinks the table\index object has?

    Not directly after the drop, is it worth repeating the process and noting the stats? It's an overnight restore but I'm not strapped for time.

    Here are some figures after I've recreated the column, clearly without the LOB data:

    rows reserveddata index_sizeunused

    1770892 107544 KB86392 KB21048 KB104 KB

    Stats were reporting 0 ghost records present in the table and that the index rebuild didn't have any traction while the column was still dropped, after recreating the column and rebuilding again the fragmentation got cleared up. The below is prior to recreating the column:

    database_idobject_idindex_idpartition_numberindex_type_desc alloc_unit_type_descindex_depthindex_levelavg_fragmentation_in_percentfragment_countavg_fragment_size_in_pagespage_count

    5 2873400881 1 CLUSTERED INDEXIN_ROW_DATA 3 0 95.9996295953329 10389 1.03946481855809 10799

    5 2873400882 1 NONCLUSTERED INDEXIN_ROW_DATA 3 0 99.9604743083004 2530 1 2530

  • check results of the following query both before and after the drop

    SELECTOBJECT_NAME(i.object_id) AS TableName

    , ISNULL(i.name, 'HEAP') AS IndexName

    , i.index_id as IndexID

    , i.type_desc AS IndexType

    , p.partition_number AS PartitionNo

    , p.rows AS NumRows

    , au.type_desc AS InType

    , au.total_pages AS NumPages

    , au.total_pages * 8 AS TotKBs

    , au.used_pages * 8 AS UsedKBs

    , au.data_pages * 8 AS DataKBs

    FROM sys.indexes i INNER JOIN sys.partitions p

    ON i.object_id = p.object_id AND i.index_id = p.index_id

    INNER JOIN sys.allocation_units au ON

    CASE

    WHEN au.type IN (1,3) THEN p.hobt_id

    WHEN au.type = 2 THEN p.partition_id

    END = au.container_id

    WHERE OBJECT_NAME(i.object_id) = 'yourtable'

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Before:

    IndexNameIndexIDIndexTypePartitionNoNumRowsInTypeNumPagesTotKBsUsedKBsDataKBs

    Pk__Key1CLUSTERED11770892LOB_DATA845812936766503446765902800

    Pk__Key1CLUSTERED11770892IN_ROW_DATA19946159568159448157520

    Pk__Key1CLUSTERED11770892ROW_OVERFLOW_DATA0000

    DK_2NONCLUSTERED11770892IN_ROW_DATA5834466724655244912

    And after:

    Pk__Key1CLUSTERED11770892LOB_DATA845812936766503446765902800

    Pk__Key1CLUSTERED11770892IN_ROW_DATA19946159568159448157520

    Pk__Key1CLUSTERED11770892ROW_OVERFLOW_DATA0000

    DK_2NONCLUSTERED11770892IN_ROW_DATA5834466724655244912

    And after the index rebuild:

    Pk__Key1CLUSTERED11770892IN_ROW_DATA10915873208727286392

    DK_2NONCLUSTERED11770892IN_ROW_DATA2546203682031220240

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply