在测试数据库中,我希望:
- 删除聚集索引(它们是对我们来说超级无用的行上的主键聚集约束。)
- 创建新的聚集索引
- 将主键约束重新创建为非聚集索引
- 重建所有其他非聚集索引。
我的工作流程也和上面一样,除了在删除聚集索引之前禁用所有非聚集索引。
由于删除聚集约束索引需要将表保存为 HEAP,因此此过程在我们的 45m 行表上花费的时间是巨大的。约束的下降一直持续到 1:17:00,似乎只有大约 31m(基于 Spotlight for the Session 中的逻辑读取)。
有没有更有效的方法来处理这个工作流程?也许是一种删除约束索引并重建为新的聚集索引而不是 HEAP 的方法?
谢谢,韦斯
DDL 语句:
表结构
CREATE TABLE [dbo].[hist](
[prrowid] [varchar](36) NOT NULL,
[part] [varchar](30) NULL,
[date] [datetime] NULL,
[per_date] [datetime] NULL,
[type] [varchar](80) NULL,
[loc] [varchar](80) NULL,
[loc_begin] [decimal](28, 10) NULL,
[begin_qoh] [decimal](28, 10) NULL,
[qty_req] [decimal](28, 10) NULL,
[qty_chg] [decimal](28, 10) NULL,
[qty_short] [decimal](28, 10) NULL,
[um] [varchar](30) NULL,
[last_date] [datetime] NULL,
[nbr] [varchar](30) NULL,
[so_job] [varchar](80) NULL,
[ship_type] [varchar](30) NULL,
[addr] [varchar](80) NULL,
[rmks] [varchar](80) NULL,
[xdr_acct] [varchar](80) NULL,
[xcr_acct] [varchar](80) NULL,
[mtl_std] [decimal](28, 10) NULL,
[lbr_std] [decimal](28, 10) NULL,
[bdn_std] [decimal](28, 10) NULL,
[price] [decimal](28, 10) NULL,
[trnbr] [int] NULL,
[gl_amt] [decimal](28, 10) NULL,
[xdr_cc] [varchar](30) NULL,
[xcr_cc] [varchar](30) NULL,
[lot] [varchar](80) NULL,
[sub_std] [decimal](28, 10) NULL,
[gl_date] [datetime] NULL,
[qty_loc] [decimal](28, 10) NULL,
[userid] [varchar](80) NULL,
[serial] [varchar](50) NULL,
[effdate] [datetime] NULL,
[prod_line] [varchar](30) NULL,
[xslspsn1] [varchar](80) NULL,
[xslspsn2] [varchar](80) NULL,
[xcr_proj] [varchar](80) NULL,
[xdr_proj] [varchar](80) NULL,
[line] [int] NULL,
[user1] [varchar](80) NULL,
[user2] [varchar](80) NULL,
[curr] [varchar](30) NULL,
[ex_rate] [decimal](28, 10) NULL,
[rev] [varchar](30) NULL,
[time] [int] NULL,
[ovh_std] [decimal](28, 10) NULL,
[site] [varchar](80) NULL,
[status] [varchar](80) NULL,
[grade] [varchar](30) NULL,
[expire] [datetime] NULL,
[assay] [decimal](28, 10) NULL,
[xgl_ref] [varchar](30) NULL,
[_chr01] [varchar](80) NULL,
[_chr02] [varchar](80) NULL,
[_chr03] [varchar](80) NULL,
[_chr04] [varchar](80) NULL,
[_chr05] [varchar](80) NULL,
[_chr06] [varchar](80) NULL,
[_chr07] [varchar](80) NULL,
[_chr08] [varchar](80) NULL,
[_chr09] [varchar](80) NULL,
[_chr10] [varchar](80) NULL,
[_chr11] [varchar](80) NULL,
[_chr12] [varchar](80) NULL,
[_chr13] [varchar](80) NULL,
[_chr14] [varchar](80) NULL,
[_chr15] [varchar](80) NULL,
[_dte01] [datetime] NULL,
[_dte02] [datetime] NULL,
[_dte03] [datetime] NULL,
[_dte04] [datetime] NULL,
[_dte05] [datetime] NULL,
[_dec01] [decimal](28, 10) NULL,
[_dec02] [decimal](28, 10) NULL,
[_dec03] [decimal](28, 10) NULL,
[_dec04] [decimal](28, 10) NULL,
[_dec05] [decimal](28, 10) NULL,
[_log01] [bit] NULL,
[_log02] [bit] NULL,
[ref] [varchar](80) NULL,
[msg] [int] NULL,
[program] [varchar](30) NULL,
[ord_rev] [int] NULL,
[ref_site] [varchar](80) NULL,
[rsn_code] [varchar](80) NULL,
[vend_lot] [varchar](30) NULL,
[vend_date] [datetime] NULL,
[daycode] [varchar](80) NULL,
[for] [varchar](30) NULL,
[slspsn##1] [varchar](82) NULL,
[slspsn##2] [varchar](82) NULL,
[slspsn##3] [varchar](82) NULL,
[slspsn##4] [varchar](82) NULL,
[fsm_type] [varchar](80) NULL,
[upd_isb] [bit] NULL,
[auto_install] [bit] NULL,
[ca_int_type] [varchar](80) NULL,
[covered_amt] [decimal](28, 10) NULL,
[fcg_code] [varchar](80) NULL,
[batch] [varchar](30) NULL,
[fsc_code] [varchar](80) NULL,
[sa_nbr] [varchar](80) NULL,
[sv_code] [varchar](80) NULL,
[eng_area] [varchar](30) NULL,
[sys_prod] [varchar](30) NULL,
[svc_type] [varchar](30) NULL,
[ca_opn_date] [datetime] NULL,
[cprice] [decimal](28, 10) NULL,
[eng_code] [varchar](80) NULL,
[wod_op] [int] NULL,
[enduser] [varchar](80) NULL,
[ship_inv_mov] [varchar](80) NULL,
[ship_date] [datetime] NULL,
[ship_id] [varchar](30) NULL,
[ex_rate2] [decimal](28, 10) NULL,
[ex_ratetype] [varchar](80) NULL,
[exru_seq] [int] NULL,
[promise_date] [datetime] NULL,
[fldchg_cmtindx] [int] NULL,
[SrcPDB] [varchar](12) NULL,
CONSTRAINT [hist_PK] PRIMARY KEY CLUSTERED
(
[prrowid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
当前指数
ALTER TABLE [dbo].[hist] ADD CONSTRAINT [hist_PK] PRIMARY KEY CLUSTERED ( [prrowid] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##addr_eff] ON [dbo].[hist] ( [addr], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##batch] ON [dbo].[hist] ( [batch] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##date_trn] ON [dbo].[hist] ( [date], [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##eff_trnbr] ON [dbo].[hist] ( [effdate], [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##nbr_eff] ON [dbo].[hist] ( [nbr], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##part_eff] ON [dbo].[hist] ( [part], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##part_trn] ON [dbo].[hist] ( [part], [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##ref_filter] ON [dbo].[hist] ( [ref] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##serial] ON [dbo].[hist] ( [serial] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##trnbr] ON [dbo].[hist] ( [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##type] ON [dbo].[hist] ( [type], [effdate] ) WITH (FILLFACTOR=100);
所需的索引
CREATE UNIQUE CLUSTERED INDEX [hist##date_trn_CX] ON [dbo].[hist] ( [date], [trnbr] ) WITH (FILLFACTOR=100);
ALTER TABLE [dbo].[hist] ADD CONSTRAINT [hist_PK] PRIMARY KEY NONCLUSTERED ( [prrowid] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##addr_eff] ON [dbo].[hist] ( [addr], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##batch] ON [dbo].[hist] ( [batch] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##eff_trnbr] ON [dbo].[hist] ( [effdate], [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##part_eff] ON [dbo].[hist] ( [part], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##part_trn] ON [dbo].[hist] ( [part], [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##ref_filter] ON [dbo].[hist] ( [ref] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##serial] ON [dbo].[hist] ( [serial] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##trnbr] ON [dbo].[hist] ( [trnbr] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##nbr_eff] ON [dbo].[hist] ( [trnbr], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##trnbr_char] ON [dbo].[hist] ( [trnbr_char] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##type] ON [dbo].[hist] ( [type], [effdate] ) WITH (FILLFACTOR=100);
CREATE INDEX [hist##vend_lot] ON [dbo].[hist] ( [vend_lot] ) WITH (FILLFACTOR=100);
-- 注意 -- 下面的答案对我来说非常有效。我确实必须添加一个音量。我在新驱动器上创建了第二个文件组和一个数据文件。此外,还有另一个日志文件。
理想情况下,你会做这样的事情:
DROP_EXISTING = ON
使用选项集在新列上重新创建聚集索引。这将跳过将表转换为堆的步骤。不幸的是,步骤 1在 SQL Server 中似乎是不可能的。
此外,BOL对使用以下内容更改主键有这样的说法
DROP_EXISTING = ON
:据我所知,您能做的最好的事情就是通过创建表的副本并将所有数据移到那里来避免堆转换。无论如何,删除或添加聚集索引都会创建数据的内部副本,因此它不需要更多空间。这里有一些提示可以加快速度:
SELECT INTO
. 这会将数据复制到堆中,这是您要避免的步骤。但是,SELECT INTO
聚集索引的创建和创建都符合 SQL Server 2014 中的并行性。INSERT INTO... SELECT
您需要TABLOCK
针对目标表的提示才能获得最少的日志记录。SORT_IN_TEMPDB = ON
如果为它调整了 tempdb 的大小,请使用该选项。顺便说一句,如果您想看看第 2 步的实际效果(我是),这里有一些示例代码,显示了如何跳过堆转换步骤: