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

前端 未结 5 1598
刺人心
刺人心 2020-12-05 04:58

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 <

5条回答
  •  一向
    一向 (楼主)
    2020-12-05 05:34

    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

提交回复
热议问题