SQL Server: SQL-Getting rid of unused space in table


Database sizing is one of the most critical tasks for any DBA.  One of the DBA’s tasks is to also ensure optimum performance. Database/table size is one of the key factors impacting the performance.

Hence, it becomes essential to keep close eye on data volumes entering and departing the databases.


I am sure most of the DBA’s would be aware of Microsoft’s undocumented SP’s.

‘sp_spaceused’ in one of them and proves to be very handy for DBA’s.

This SP displays the usage statistic for a table or a database.

I deal with lots of databases that are part of ETL process. Given the vibrant nature of ETL systems, loads of data is crunched on frequent basis. This leads to huge fragmentation and page splits. Most of the ETL databases are index light. And tables without indexes are known as Heaps (very trivial but just thought to put one liner).

Here is the output of sp_spaceused of one of the huge tables in my environment.

Syntax: sp_spaceused ‘TableABC’

Name Rows Reserved data index_size unused
TableABC 3767252 77409456 KB 23722280 KB 56288 KB 53630888 KB

Total size of table is around 77 GB. However, 53 GB is free. Question is how to get rid of this unused space.

Answer is very simple. Just rebuild the heap, here is the syntax

Alter table ‘TableABC’ Rebuild

This will free up all the unused space in the table. In effect the database size would also be drastically reduced improving overall performance.

Note: Alter table <tablename> Rebuild, besides heap the command will also rebuild all the indexes (if any) on the table.



Ritesh Medhe

Like us on FaceBook Follow us on Twitter

Join the fastest growing SQL Server group on FaceBook


6 Comments on “SQL Server: SQL-Getting rid of unused space in table”

  1. Ouch!

    The posting is quite accurate. However, you did not point out any of the pain associated with such an action. Depending on the verison of SQL Server you may actually lock activity on a table during the re-indexing process.

    For a large table, this action is no small thing. It is the correct thing to do. But, beware…you can bring a production server to it’s knees.

  2. Hey Ben, Point taken.

    I performed this activity on a heap. The table size was around 77 GB out of which 53 GB was free. And to my surprise the rebuild for heap took less than 5 minutes.

    But yes if the table have CI/NCI then it would be a pain. However, such activities should be carried out during off hours or low traffic:)

  3. Hmmm..Interesting..

    So what about the forwarding pointers in the heap table ? Also can it be used on clustered tables ?

  4. Hi Sachin, Yes heap rebulid will take care of forwarding pointers. FWD pointers will be removed, in effect will improve the performance.

    Yes this can be used on clustered tables and as i mentioned

    Alter table Rebuild, besides heap the command will also rebuild all the indexes (if any) on the table [CI & NCI]

  5. So basically if I have a forwarding pointer in a table that is not a heap so will it still build the NC index ? And also what about the clustered index on it will that to also be rebuilt ?

Leave a Reply

Your email address will not be published.