Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Shrinking Database or File not Reclaiming unused space Expand / Collapse
Author
Message
Posted Monday, April 15, 2013 7:54 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 7:46 PM
Points: 18, Visits: 232
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)
Post #1442559
Posted Tuesday, April 16, 2013 12:25 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:50 PM
Points: 6,191, Visits: 13,340
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"
Post #1442608
Posted Tuesday, April 16, 2013 1:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 7:46 PM
Points: 18, Visits: 232
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	        reserved	data	        index_size	unused
1770892 107544 KB 86392 KB 21048 KB 104 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_id	object_id	index_id	partition_number	index_type_desc	        alloc_unit_type_desc	index_depth	index_level	avg_fragmentation_in_percent	fragment_count	avg_fragment_size_in_pages	page_count
5 287340088 1 1 CLUSTERED INDEX IN_ROW_DATA 3 0 95.9996295953329 10389 1.03946481855809 10799
5 287340088 2 1 NONCLUSTERED INDEX IN_ROW_DATA 3 0 99.9604743083004 2530 1 2530

Post #1442626
Posted Tuesday, April 16, 2013 1:44 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 1:50 PM
Points: 6,191, Visits: 13,340
check results of the following query both before and after the drop

SELECT		OBJECT_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"
Post #1442628
Posted Tuesday, April 16, 2013 7:40 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, July 17, 2014 7:46 PM
Points: 18, Visits: 232
Before:

IndexName	IndexID	IndexType	PartitionNo	NumRows	InType	NumPages	TotKBs	UsedKBs	DataKBs
Pk__Key 1 CLUSTERED 1 1770892 LOB_DATA 84581293 676650344 676590280 0
Pk__Key 1 CLUSTERED 1 1770892 IN_ROW_DATA 19946 159568 159448 157520
Pk__Key 1 CLUSTERED 1 1770892 ROW_OVERFLOW_DATA 0 0 0 0
DK_ 2 NONCLUSTERED 1 1770892 IN_ROW_DATA 5834 46672 46552 44912

And after:

Pk__Key	1	CLUSTERED	1	1770892	LOB_DATA	84581293	676650344	676590280	0
Pk__Key 1 CLUSTERED 1 1770892 IN_ROW_DATA 19946 159568 159448 157520
Pk__Key 1 CLUSTERED 1 1770892 ROW_OVERFLOW_DATA 0 0 0 0
DK_ 2 NONCLUSTERED 1 1770892 IN_ROW_DATA 5834 46672 46552 44912

And after the index rebuild:
Pk__Key	1	CLUSTERED	1	1770892	IN_ROW_DATA	10915	87320	87272	86392
DK_ 2 NONCLUSTERED 1 1770892 IN_ROW_DATA 2546 20368 20312 20240

Post #1443068
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse