I just want to know:
- How SQL Server reuses reserved space after dropping a 1GB table? (call the table T1)
- How SQL Server reuses reserved space after truncating a table? (call this table T2)
Both are clustered tables (not heaps).
database-internalsdisk-spacesql server
I just want to know:
Both are clustered tables (not heaps).
Best Answer
Quite simply a
DROP
orTRUNCATE
statement marks any allocated pages that were associated with the table (regardless if it was a heap or a clustered table) as no longer being allocated and the space is reused by the next operation that requires additional space (e.g. new records, a page split, etc.).A
TRUNCATE TABLE
operation is effectively the same thing as aDROP TABLE
statement, but it comes with the bonus of recreating the table immediately after it has been dropped. This is why aTRUNCATE TABLE
statement is a Data Definition Language (DDL) statement and not a Data Manipulation Language statement such as aDELETE
.If you want to look at how the engine deallocates and reuses space, you can dig into the
DBCC PAGE
undocumented function. Paul Randal has a few articles (e.g. ref1, ref2, ref3, ref4) about its use that you can read through if you're so inclined.If you've got a sandbox environment, you can also run the following series of scripts which will show the pages being allocated and deallocated in the same manner, regardless if a
TRUNCATE TABLE
orDROP TABLE
operation is performed:For more information on the GAM, SGAM, extents, and the basics to the storage engine, start with the Pages and Extents Architecture Guide by Microsoft. Hopefully this answers your question, though I feel you'll have more questions to follow-up with.