Sql-server – the most efficient way to alter a column definition in a table with millions of rows

database-designsql-server-2008

I need to change a column from NOT NULL to NULL in a table that contains millions of rows. I've tried a simple

alter table Table1 ALTER COLUMN Column1 XML NULL

but it takes forever. So here are my questions:

  1. Why does it take so long to apply the alter?
  2. Is there a better way to do it?

Best Answer

1) One would need more info on the complete structure of the table + non clustered indexes to correctly figure out what's happening but my suspicion is something to do with the NULL bitmap.

Please refer to for more details on the topic. http://www.sqlskills.com/BLOGS/PAUL/post/Misconceptions-around-null-bitmap-size.aspx

http://www.sqlskills.com/BLOGS/PAUL/post/A-SQL-Server-DBA-myth-a-day-(630)-three-null-bitmap-myths.aspx

2) Yes, provided you have the storage space, create a new table with correct nullability and transfer the data in multiple batches to avoid excessive log growth and switch the table using the technique listed below. I have done this several times with little to no downtime at all.

http://jahaines.blogspot.com/2009/12/sql-server-2005-how-to-move-10-millions.html