添加顺序标识列时进度缓慢
我们有 800 万行表,我们需要向其中添加一个顺序 id 列。它用于数据仓库。
通过测试,我们知道,如果删除所有索引(包括主键索引),添加新的顺序 id 列的速度会快 10 倍。我仍然不明白为什么删除索引将有助于添加标识列。
以下是添加标识列的 SQL:
ALTER TABLE MyTable ADD MyTableSeqId BIGINT IDENTITY(1,1)
但是,相关表具有依赖性,因此除非删除所有 FK 约束,否则我无法删除主键索引。结果添加了标识列。
- 是否有其他方法可以提高添加标识列时的速度,从而最大限度地减少客户端停机时间?
或者
- 是否有一种方法可以在不锁定表的情况下添加标识列,以便可以访问该表,或者至少可以查询该表?
数据库是SQL Server 2005标准版。
We have 8 million row table and we need to add a sequential id column to it. It is used for data warehousing.
From testing, we know that if we remove all the indexes, including the primary key index, adding a new sequential id column was like 10x faster. I still haven't figure out why dropping the indexes would help adding a identity column.
Here is the SQL that add identity column:
ALTER TABLE MyTable ADD MyTableSeqId BIGINT IDENTITY(1,1)
However, the table in question has dependencies, thus I cannot drop the primary key index unless I remove all the FK constraints. As a result adding identity column.
- Is there other ways to improve the speed when adding a identity column, so that client down time is minimal?
or
- Is there a way to add an identity column without locking the table, so that table can be access, or at least be queried?
The database is SQL Server 2005 Standard Edition.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
向表添加新列将获取 Sch-M(架构修改)锁,这会阻止在操作期间对该表的所有访问。
在操作期间将数据库切换到大容量日志模式或简单模式可能会给您带来一些好处,但当然,只有当您意识到这会对您的备份/恢复策略产生影响时才这样做。
Adding a new column to a table will acquire a Sch-M (schema modification) lock, which prevents all access to the table for the duration of the operation.
You may get some benefit from switching the database into bulk-logged or simple mode for the duration of the operation, but of course, do so only if you're aware of the effects this will have on your backup / restore strategy.