巧合的是,大约3个小时前,我不得不做一些非常相似的事情。该表是3500万行,它相当宽,并且花了很多时间才能做到这一点:
alter table myTable add myNewColumn int not null default 0;
这就是我最终得到的结果:
alter table myTable add myNewColumn int null;while 1=1begin update top (100000) myTable set myNewColumn = 0 where myNewColumn is null; if @@ROWCOUNT = 0 break;endalter table myTable alter column myNewColumn int not null;alter table myTable add constraint tw_def_myNewColumn default (0) for myNewColumn;
这次,这些
alter table陈述几乎是即时的。(在速度较慢的服务器上)花了大约7-8分钟的时间来执行更新批处理。我推测SQL
Server在原始查询中生成了还原 *** 作以恢复值,但我没想到会从头开始。
无论如何,就您而言,类似的事情可能会有所帮助。您可以尝试添加新的bigint列,分批更新新的列,然后在其上设置约束。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)