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

前端 未结 5 1602
刺人心
刺人心 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:30

    I had a similar problem here SQL Server, Converting NTEXT to NVARCHAR(MAX) that was related to changing ntext to nvarchar(max).

    I had to do an UPDATE MyTable SET MyValue = MyValue in order to get it to resize everything nicely.

    This obviously takes quite a long time with a lot of records. There were a number of suggestions as how better to do it. They key one was a temporary flag indicated if it had been done or not and then updating a few thousand at a time in a loop until it was all done. This meant I had "some" control over how much it was doing.

    On another note though, if you really want to shrink the database as much as possible, it can help if you turn the recovery model down to simple, shrink the transaction logs, reorganise all the data in the pages, then set it back to full recovery model. Be careful though, shrinking of databases is generally not advisable, and if you reduce the recovery model of a live database you are asking for something to go wrong.

提交回复
热议问题