Joe Webb

Musing and observations about SQL Server, other technogies, and sometimes just life in general
posts - 67, comments - 163, trackbacks - 0

My Links

SQLTeam.com Links

News

Add to Technorati Favorites

Search this Blog
 




Archives

Post Categories

About me

Reclaiming unused space from a table

In a recent post, I demonstrated how SQL Server will reuse space made available when rows are deleted from a table. This occurs automatically without any outside intervention on our part.

However under certain circumstances, SQL Server does not automatically reclaim space once used. If a table definition is altered to drop one or more variable length columns, the space consumed by those columns is not immediately made available for reuse by SQL Server. But that's to say that the space is forever lost. We can employee a DBCC utility reclaim the newly-freed space.

To illustrate this behavior and the aforementioned DBCC utility, let's consider an example. Let's create a table with three columns and populate it with some test data as shown in the following code.

USE tempdb ;
GO

--create a test table
CREATE TABLE dbo.Test
(
col1 INT
,col2 VARCHAR(50)
,col3 VARCHAR(MAX)
) ;

--create some test data
DECLARE @cnt INT ;
SET @cnt = 0 ;
WHILE 1 = 1
BEGIN
SELECT
@cnt = @cnt + 1 ;
INSERT
dbo.Test ( col1,col2,col3 )
VALUES (
@cnt
,'test row # ' + CAST(@cnt AS VARCHAR(100))
,REPLICATE('A',@cnt)
) ;
IF @cnt = 1000
BREAK ;
END

 

Now let's view the table to make sure we have what we think we have.

--view the table 
SELECT
*
FROM
dbo.Test ;

Cleantable-2008-01-14a

 

Using a Dynamic Management View, let's see how much space our newly created table is consuming. Note that since this script employs the use of a DMV, it will not work with SQL Server 2000.

--check the size of the table 
SELECT
alloc_unit_type_desc
,page_count
,avg_page_space_used_in_percent
,record_count
FROM
sys.dm_db_index_physical_stats(
DB_ID()
,OBJECT_ID(N'dbo.Test')
,NULL
,NULL,'Detailed') ;

 

On my system, it indicates that 84 data pages are used to store the 1000 rows. Each data page is, on average, 78.4% full.

Cleantable-2008-01-14b

Now let's drop the third column, the one that consumes the most space, and check the space used once again.

--drop the last column
ALTER TABLE dbo.Test DROP COLUMN col3 ;

--check the space used again
SELECT
alloc_unit_type_desc
,page_count
,avg_page_space_used_in_percent
,record_count
FROM
sys.dm_db_index_physical_stats(
DB_ID()
,OBJECT_ID(N'dbo.Test')
,NULL
,NULL
,'Detailed') ;

 

We get the same results - 84 data pages, storing 1000 rows, each 78.4% full - even after dropping the column that consumed the most space.

Cleantable-2008-01-14c

Now, let's reclaim the space using the DBCC CLEANTABLE command and recheck the space consumed.

--reclaim the space from the table
DBCC CLEANTABLE('tempdb', 'dbo.Test') ;

--check the space used one more time
SELECT
alloc_unit_type_desc
,page_count
,avg_page_space_used_in_percent
,record_count
FROM
sys.dm_db_index_physical_stats(
DB_ID()
,OBJECT_ID(N'dbo.Test')
,NULL
,NULL
,'Detailed') ;

 

This time, considerable less space is consumed; the average page is only filled 4.5% full!

Cleantable-2008-01-14d

 

Finally let's clean up.

--clean up
DROP TABLE dbo.Test ;

 

For more information, check out DBCC CLEANTABLE in Books Online.

Cheers!

Joe

kick it on DotNetKicks.com

Print | posted on Monday, January 14, 2008 5:23 PM

Feedback

# re: Reclaiming unused space from a table

How can I do this in sql 2000?
1/17/2008 8:15 AM | BobS

# re: Reclaiming unused space from a table

The DBCC CLEANTABLE command is available in SQL Server 2000. It' only the query that calls the DMV that won't work in SQL Server 2000 since DMV's weren't introduced until 2005.
1/17/2008 9:47 AM | Joe Webb

# re: Reclaiming unused space from a table

Thanks for the post. What I found was that this does not shrink the physical .mdf file size. I am facing a problem in production with an audit log table taking up huge space. Tried truncating the table, dbcc shrinkfile etc. But, I am not able to reclaim the physical space.

Do you have any info on this or point me to some resources on the web?

Thanks,
Gulzar
2/27/2008 11:49 AM | Gulzar

# re: Reclaiming unused space from a table

Very didatic... congratulations...
10/1/2008 8:27 PM | ronabrag

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 1 and 6 and type the answer here:

Powered by: