在前面的实验中,完成了分区表的建置,分区合并与删除的操作 ,最近计划做sql server的读写分离操作,用订阅发布的方法。

在订阅发布的过程中,需要订阅发布的表要有主键,于是我想当然的加了一个自增id列做主键,本以为是一个很简单的操作,结合却碰到了困难。

系统系统提示在建立主键,唯一索引,聚集索引的时候,分区列必须包含在其中。

1.查看表结构,发现没有建主键

exec sp_help [PRODUCT_WORKORDER_PROCESS_DEFECT_DETAIL];

 

2. 给表格加一个主键data id (自增列)

alter table product_workorder_process_defect_detail
add dataid bigint identity(1,1)  primary key.

 系统报错,主键建立失败。

 

3.于是,我就先将自增列加上再说,不管主键

alter table product_workorder_process_defect_detail
add dataid bigint identity(1,1)

这一部执行成功

4. 我试着执行了一句如下语句,太慢,执行了一分钟左右后取消了,分析是因为没有index的情况下,做全表扫描,所以很慢
select max(dataid) from product_workorder_process_defect_detail

 

5,按2中的提示,我尝试将createDT列加入主键中

 alter table product_workorder_process_defect_detail
add  constraint pk_product_workorder_process_defect_detail  primary key (createDT,dataid);

结果还是报错,提示createDT上nullable为yes,不能做为primary key column.

 

6.我想当然的想改掉createDT上的nullable 为no

alter table product_workorder_process_defect_detail
alter column createDT datetime not null;

结果不行,提示createDT列已被其它对象引用,不能执行此操作

7.我想可能是因为分区表的原因,一不做二不休,将分区表变回普通表,先删掉上面的分区聚集index

   drop index cidx_product_workorder_process_defect_detail_createDT on product_workorder_process_defect_detail

8, 又试着执行了一下如下语句,还是不成功

alter table product_workorder_process_defect_detail
alter column createDT datetime not null;

 9, 老老实实的重新生成普通clustered index on primary,将分区表变回普通表

CREATE CLUSTERED INDEX [cidx_createDT] ON [dbo].[PRODUCT_WORKORDER_PROCESS_DEFECT_DETAIL]
(
[CreateDT]
)
ON[PRIMARY];

 执行完成后再检查,发现变回了普通表

 

10 此时虽然不是分区表了,但因为针对createDT建立了index,所以还是不能执行alter colum的操作,下面的语句还是执行失败

alter table product_workorder_process_defect_detail
alter column createDT datetime not null;

 

11.索性将这个clustered index也删除了,这一步操作成功

drop index [cidx_createDT] ON [dbo].[PRODUCT_WORKORDER_PROCESS_DEFECT_DETAIL]

 

12.现在没有任何对象参照到createDT列,再将它改为not null

alter table product_workorder_process_defect_detail
alter column createDT datetime not null;

操作终于成功了

 

13 .再次加主键,操作成功

alter table product_workorder_process_defect_detail
add constraint pk_product_workorder_process_defect_detail
primary key (createDT,dataid);

13,重建分区表,这一步又失败了,提示只能有一个clustered index.

create clustered index cidx_createDT ON product_workorder_process_defect_detail
(
CreateDT
)
with (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF)
on [PS_PFUN_DATETIME]([CreateDT])

无法对 表 'product_workorder_process_defect_detail' 创建多个聚集索引。请在创建新聚集索引前删除现有的聚集索引 'pk_product_workorder_process_defect_detail'。

 

14.我想drop掉主键上的index,保留主键,不行,哈 哈 .

drop index pk_product_workorder_process_defect_detail on product_workorder_process_defect_detail

不允许对索引 'product_workorder_process_defect_detail.pk_product_workorder_process_defect_detail' 显式地使用 DROP INDEX。该索引正用于 PRIMARY KEY 约束的强制执行

 

15 分析了一下,上一步create primary key 的时候弄错了,应该用no clustered.,只能先drop掉主键

     alter table product_workorder_process_defect_detail
     drop constraint pk_product_workorder_process_defect_detail

 

16. 然后再建立分区索引

create clustered index cidx_createDT ON product_workorder_process_defect_detail
(
CreateDT
)
with (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF)
on [PS_PFUN_DATETIME]([CreateDT])

 

17. 再次建 立主键

alter table product_workorder_process_defect_detail
add constraint pk_product_workorder_process_defect_detail
primary key (createDT,dataid);

 

posted on 2020-07-14 00:12  湖东  阅读(196)  评论(0编辑  收藏  举报