出于某种原因,我需要将我的表列之一从“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/