SQL Server 2012 花费很长时间进行简单的更改以添加 NULL 列

SQL Server 2012 takes long time on simple alter to add NULL columns

我在 SQL Server 2012 中更改 table 时遇到问题,将允许的 04 列 NULL 添加到带有 340 的大 table 需要很长时间列和大约 166M 行和 01 个非聚集索引

此问题仅在特定 table 恢复后发生。 我正在等待执行 10 个小时,但它还没有完成,所以我必须取消它以进行更多调查。很奇怪,因为脚本真的很简单,如下所示,我们之前已经成功完成了:

alter table sample_database.sample_schema.sample_table
add column_001 int null
   ,column_002 numeric(18,4) null
   ,column_003 nvarchar(500) null
   ,column_004 int null;

我的问题是:

非常感谢大家,

如果是2012年(根据标签),可能会出现这种情况:

http://rusanu.com/2012/02/16/adding-a-nullable-column-can-update-the-entire-table/

If adding a nullable column in SQL Server 2012 has the potential of increasing the row size over the 8060 size then the ALTER performs an offline size-of-data update to every row of the table to ensure it fits in the page. This behavior is new in SQL Server 2012.

@Anton解释了这背后的原因。 -页面拆分

对于解决方法,您可以按照以下步骤操作:

  1. 创建空行的克隆 table(例如 SELECT * INTO <New table> from sample_table WHERE 0=1
  2. 在 .
  3. 中添加新列
  4. 将数据从 sample_table 复制到 .
  5. 第 3 步完成后修改 sample_table 名称并重命名为 sample_table。

您还应该尝试添加具有 NOT NULL 值的列,一旦添加了列,然后相应地修改它。

这个案例发生在我们的UAT环境中。我们从最新的备份中再次恢复数据库后,这个问题就没有再发生了。改变在毫秒内完成。

我认为,自上次修复以来存在某些问题。

非常感谢大家,

我遇到了同样的问题,简单的服务器重启就成功了。