I dropped a few (decimal) columns from my table. Truncated the table and then re-inserted the data as before, barring the dropped columns. However I don't notice any perceptible difference in the space occupied by the table. I had expected it to decrease. Why did it not?
Below is the query I'm using to determine the space occupied:
SELECT
b.groupname AS 'File Group',
Name,
[Filename],
CONVERT (Decimal(15,2),ROUND(a.Size/128.000,2)) [Currently Allocated Space (MB)],
CONVERT (Decimal(15,2),ROUND(FILEPROPERTY(a.Name,'SpaceUsed')/128.000,2)) AS [Space Used (MB)],
CONVERT (Decimal(15,2),ROUND((a.Size-FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS [Available Space (MB)],
'DBCC SHRINKFILE ('''+Name+''','+CAST(CONVERT (Decimal(15,2),ROUND((FILEPROPERTY(a.Name,'SpaceUsed'))/128.000,2)) AS VARCHAR(10))+')' QUERY
FROM dbo.SYSFILES a (NOLOCK)
JOIN SYSFILEGROUPS b (NOLOCK)
ON a.groupid = b.groupid
ORDER BY b.groupname
PS. I deemed Rebuilding the table and its indexes unnecessary, as the table was truncated
Best Answer
The first thing I'd ask is are you sure the columns you dropped are decimals?
The reason why I ask is that with variable columns (which Decimal is not), then SQL Server won't automatically reduce space used when these are dropped. To resolve this you can run DBCC CLEANTABLE. You might want to try this anyway.
However, if the columns that you dropped are only decimals, then this probably won't have an effect.
Another possibility is that the decimal columns were just not taking up that much space - depending on the precision, a decimal column can be quite small. You say that there is no perceptible difference, is it just a case that the decimal columns were taking up relatively little space compared to other columns in the table (and rest of the database)?
Check the space used of the actual table before and after the change. I used this query to check table spaced used
Reference: I got this originally from this SO post.
To note/warning: I'm assuming you're doing this in some sort of development or test environment, doing this sort of thing in production without testing is really not advisable. This earlier post is similar to yours (but is specifically about variable column lengths).
SpaceUsed Your query to find space used looks fine, but there is an inbuilt stored procedure sp_spaceused that you might find easier (but probably doesn't make much of a difference.