SqlServer 性能将列从非空更改为空

标签 sql sql-server sql-server-2008

出于某种原因,我需要将我的表列之一从“NOT NULL”更新为“NULL”。 命令很简单:

ALTER TABLE TBLOGDOCMESSAGE ALTER COLUMN PROCESSID BIGINT NULL

问题在于该命令运行时间过长。我的表包含大约 30M 个寄存器(但我的数据中心可能有超过 120M 个寄存器)。 该列上有一个非聚集索引并且不是 FK。

有一种方法可以加快 SQLServer 2008 及更高版本中的命令速度吗?

最佳答案

一个重要的教训是 NULL 位图的存在。

A NULL bitmap will always be there in a table (heap or clustered table) irrespective of whether the table has NULLable columns or NOT. Note that we defined a UNIQUE constraint on LastName + FirstName in Demo 2 and UNIQUE constraint is enforced using a UNIQUE INDEX on those columns. NULL bitmap will NOT be present in a NON-CLUSTERED INDEX if all the columns part of the index definition are defined as NOT NULL. In our case both LastName + FirstName are defined as NOT NULL in the table and that's why NULL bitmap wasn't there initially. When we ALTERed the table definition, in this case the operation has to touch each and every row of the table and that's why it is an expensive operation. (link)

因为您的列也有一个非聚集索引,所以 NULL 位图还不存在。
所以我认为删除非聚集索引,然后发出ALTER TABLE语句并重新创建索引在这里可能更快,或者至少值得尝试(在安全的环境中:)。

关于SqlServer 性能将列从非空更改为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23911774/

相关文章:

sql - 如何获取不考虑日期的按时间的点击次数?

sql-server - SQL 更改 : add multiple FKs?

sql - 为架构和数据生成脚本

sql - 甲骨文 SQL。我应该使用什么声明

sql - 将函数返回的记录拆分为多列

c# - Entity Framework 可以在 SQL 中生成 UPDATE ... WHERE 语句吗?

sql-server - SQL Server 高可用性本地 - 云

php - 打印多维数组 PHP

sql - 将 SQL 查询转换为关系代数

sql - 将具有嵌套连接的高级 SQL 查询转换为 Linq-to-sql