Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce size of SQL Server table that grew from a datatype change

Tags:

I have a table on SQL Server 2005 that was about 4gb in size.

(about 17 million records)

I changed one of the fields from datatype char(30) to char(60) (there are in total 25 fields most of which are char(10) so the amount of char space adds up to about 300)

This caused the table to double in size (over 9gb)

I then changed the char(60) to varchar(60) and then ran a function to cut extra whitespace out of the data (so as to reduce the average length of the data in the field to about 15)

This did not reduce the table size. Shrinking the database did not help either.

Short of actually recreating the table structure and copying the data over (that's 17 million records!) is there a less drastic way of getting the size back down again?

like image 365
MrVimes Avatar asked Apr 30 '09 15:04

MrVimes


People also ask

How do I reduce the size of a SQL table?

If you really need to reduce the size of the database then you need to reduce the amount of data. And you don't want to shrink your database. Since you are on SQL 2016 SP1 you can turn on PAGE compression, or store the data in a clustered Columnstore index.

How do I shrink a SQL Server data file?

To shrink a data or log file. In Object Explorer, connect to an instance of the SQL Server Database Engine and then expand that instance. Expand Databases and then right-click the database that you want to shrink. Point to Tasks, point to Shrink, and then select Files.

How do I find the database growth history in SQL Server?

SSMS Disk Usage ReportConnect to a SQL instance and right-click on a database for which we want to get details of Auto Growth and Shrink Events. It opens the disk usage report of the specified database. In this disk usage report, we get the details of the data file and log file space usage.


2 Answers

You have not cleaned or compacted any data, even with a "shrink database".

DBCC CLEANTABLE

Reclaims space from dropped variable-length columns in tables or indexed views.

However, a simple index rebuild if there is a clustered index should also do it

ALTER INDEX ALL ON dbo.Mytable REBUILD 

A worked example from Tony Rogerson

like image 150
gbn Avatar answered Sep 19 '22 13:09

gbn


Well it's clear you're not getting any space back ! :-)

When you changed your text fields to CHAR(60), they are all filled up to capacity with spaces. So ALL your fields are now really 60 characters long.

Changing that back to VARCHAR(60) won't help - the fields are still all 60 chars long....

What you really need to do is run a TRIM function over all your fields to reduce them back to their trimmed length, and then do a database shrinking.

After you've done that, you need to REBUILD your clustered index in order to reclaim some of that wasted space. The clustered index is really where your data lives - you can rebuild it like this:

ALTER INDEX IndexName ON YourTable REBUILD  

By default, your primary key is your clustered index (unless you've specified otherwise).

Marc

like image 39
marc_s Avatar answered Sep 19 '22 13:09

marc_s