简体   繁体   English

更改具有相关索引的表

[英]Changing table with dependent index

As part of a system upgrade, we need to be able to update a database structure to conform to the newest version. 作为系统升级的一部分,我们需要能够更新数据库结构以符合最新版本。

I'm writing a tool to that end, but am stuck on the correct procedure for updating a column which is used in an index. 我为此目的正在编写一个工具,但仍停留在更新索引中使用的列的正确过程上。

If a column which is in an index must be changed, which approach is likely to be the least problematic: 如果必须更改索引中的列,则该方法可能是问题最少的方法:

1) Disable the index, alter the column, re-enable the index 1)禁用索引,更改列,然后重新启用索引

2) Drop the index, alter the column, re-create the index 2)删除索引,更改列,重新创建索引

There are a number of instances where this change must be applied, and I would like to reduce the overall time as much as possible, hence my preference for not recreating the index if it can be avoided. 在许多情况下,必须应用此更改,并且我想尽可能地减少总时间,因此,我倾向于不重新创建索引(如果可以避免的话)。

I did some tests,it seems you cannot alter index columns . 我做了一些测试,看来您不能更改索引列。

test data:
create table idd
(
id int identity(1,1),
name char(33),
name2 varchar(40) null
)

create unique clustered index nci_id on idd(id)
create index nci_test1 on idd(name2)

--disable index
alter index nci_test1 on idd disable

--alter column
alter table idd
alter column test1 varchar(100) not null

below is the error:
Msg 5074, Level 16, State 1, Line 36
The index 'nci_test1' is dependent on column 'test1'.
Msg 4922, Level 16, State 9, Line 36
ALTER TABLE ALTER COLUMN test1 failed because one or more objects access this column.

This is obvious since I have clustered key.so what happens if I drop clustered key and then do an alter operation on non clustered index key column,Result is same.We can alter index columns only after dropping them 这是显而易见的,因为我已经建立了聚簇键。因此,如果我删除聚簇键然后对非聚簇索引键列进行更改操作,结果将是相同的。只有在删除索引列后才能更改索引列

drop index  [nci_id] on idd

--alter column
alter table idd
alter column test1 varchar(100) not null 

I think you got some idea on what is the impact 我想您对影响有一些了解

1.We have to drop clustered key first ..heavy tlog writes,since non clustered key also have to change there pointers 1.我们必须先删除集群键..大量的tlog写入,因为非集群键也必须更改指针
2.Again we have to rebuild indexes 2.再次我们必须重建索引

You can only drop them.Further I would suggest you go ahead with this approach(since either way you have to drop clustered index) of 您只能删除它们。此外,我建议您继续使用这种方法(因为无论哪种方式都必须删除聚簇索引)

1.Drop index 1.下降指数
2.Alter column datatype 2.改变列数据类型
3.recreate index 3.重新创建索引

Further try changing database recovery model to simple,so as to minimize TLOG writes prior to this operation and also add nocheck option .Below questions has some interesting answers which may help you 进一步尝试将数据库恢复模型更改为简单,以便在执行此操作之前最大程度地减少TLOG写入,并添加nocheck选项。以下问题提供了一些有趣的答案,可能会对您有所帮助

https://dba.stackexchange.com/questions/48872/quickly-change-null-column-to-not-null https://dba.stackexchange.com/questions/48872/quickly-change-null-column-to-not-null

How do you add a NOT NULL Column to a large table in SQL Server? 如何在SQL Server中的大型表中添加NOT NULL列?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM