How to regain disk space after dropping a column with MSSQL
You can regain the disk space only if you drop one of the following:
- varchar
- nvarchar
- varbinary
- text
- ntext
- image
- xml
Display the table structure & space used
- USE sql911;
- go
- SELECT CAST(col.name AS VARCHAR(20)) AS "Column Name",
- col.column_id,
- CAST(SCHEMA_NAME(typ.schema_id) AS VARCHAR(10)) AS "Schema",
- CAST(typ.name AS VARCHAR(12)) AS "Type",
- typ.is_user_defined,
- typ.is_assembly_type,
- col.max_length,
- col.PRECISION,
- col.scale
- FROM sys.columns AS col
- JOIN sys.types AS typ ON col.user_type_id=typ.user_type_id
- WHERE col.OBJECT_ID = OBJECT_ID('sales02')
- ORDER BY 2;
- go
Column Name column_id Schema Type is_user_defined is_assembly_type max_length precision scale -------------------- ----------- ---------- ------------ --------------- ---------------- ---------- --------- ----- sales_id 1 sys int 0 0 4 10 0 sales_date 2 sys datetime 0 0 8 23 3 sales_qty 3 sys decimal 0 0 5 9 2 sales_price 4 sys decimal 0 0 9 12 2 emp_short 5 sys varchar 0 0 50 0 0 vartext 6 sys varchar 0 0 500 0 0 (6 row(s) affected)
- SELECT CAST(OBJECT_NAME(id) AS VARCHAR(12)) AS 'Table',
- CAST(rowcnt AS VARCHAR(10)) AS '#Rows',
- reserved * 8 AS 'Disk - Kb',
- dpages * 8 AS 'Data - Kb',
- (SUM(used) * 8) - (dpages * 8) AS 'Index - Kb'
- FROM sysindexes
- WHERE indid IN (0,1)
- AND OBJECTPROPERTY(id, 'IsUserTable') = 1
- GROUP BY id, rowcnt, reserved, dpages
- ORDER BY 'Table';
- go
Table #Rows Disk - Kb Data - Kb Index - Kb ------------ ---------- ----------- ----------- ----------- DEPARTMENT 9 16 8 8 EMP_WORK 75 16 8 8 Emps 32 16 8 8 Nums 1048577 13576 13488 56 ORG 10 16 8 8 PROJECT 20 16 8 8 sales 57 16 8 8 sales02 32 40 24 16
Notice the disk for sales02: 40kb and the data: 24kb
Drop the vartext column & regain the space
- ALTER TABLE sales02
- DROP COLUMN vartext;
- go
- DBCC CLEANTABLE ('sql911','sales02',0);
- Go
- 0 means that DBCC will process the entire table as one transaction.
- DBCC CLEANTABLE places an exclusive lock on the table.
- DBCC CLEANTABLE is NOT supported for system tables and temp tables.
- DBCC CLEANTABLE is NOT be used as a routine maintenance task.
Verification
- UPDATE STATISTICS Sales02;
- go
- SELECT CAST(OBJECT_NAME(id) AS VARCHAR(12)) AS 'Table',
- CAST(rowcnt AS VARCHAR(10)) AS '#Rows',
- reserved * 8 AS 'Disk - Kb',
- dpages * 8 AS 'Data - Kb',
- (SUM(used) * 8) - (dpages * 8) AS 'Index - Kb'
- FROM sysindexes
- WHERE indid IN (0,1)
- AND OBJECTPROPERTY(id, 'IsUserTable') = 1
- GROUP BY id, rowcnt, reserved, dpages
- ORDER BY 'Table';
- go
Column Name column_id Schema Type is_user_defined is_assembly_type max_length precision scale -------------------- ----------- ---------- ------------ --------------- ---------------- ---------- --------- ----- sales_id 1 sys int 0 0 4 10 0 sales_date 2 sys datetime 0 0 8 23 3 sales_qty 3 sys decimal 0 0 5 9 2 sales_price 4 sys decimal 0 0 9 12 2 emp_short 5 sys varchar 0 0 50 0 0 vartext 6 sys varchar 0 0 500 0 0 (6 row(s) affected)
- The UPDATE STATISTICS is to ensure that the sys.indexes table is correctly updated right now. That would depend on the SQL Server load.
- SELECT CAST(OBJECT_NAME(id) AS VARCHAR(12)) AS 'Table',
- CAST(rowcnt AS VARCHAR(10)) AS '#Rows',
- reserved * 8 AS 'Disk - Kb',
- dpages * 8 AS 'Data - Kb',
- (SUM(used) * 8) - (dpages * 8) AS 'Index - Kb'
- FROM sysindexes
- WHERE indid IN (0,1)
- AND OBJECTPROPERTY(id, 'IsUserTable') = 1
- GROUP BY id, rowcnt, reserved, dpages
- ORDER BY 'Table';
- go
Table #Rows Disk - Kb Data - Kb Index - Kb ------------ ---------- ----------- ----------- ----------- DEPARTMENT 9 16 8 8 EMP_WORK 75 16 8 8 Emps 32 16 8 8 Nums 1048577 13576 13488 56 ORG 10 16 8 8 PROJECT 20 16 8 8 sales 57 16 8 8 sales02 32 24 8 8
- Notice the disk for sales02: 24kb and the data: 8kb
- You can regain the disk space only if you drop one of the following:
- varchar
- nvarchar
- varbinary
- text
- ntext
- image
- xml

