在现有分区 table 上创建列存储索引,在 SQL Server 2017 上有 800 多万行

Creating columnstore Index on existing partitioned table with 800+ million rows on SQL Server 2017

我有分区 table(日期),其中包含超过 8 亿行的 B 树聚簇索引。

我想在此 table 上创建一个聚集列存储索引来代替现有的聚集索引,什么是最有效的方法?

这会影响我使用 B 树聚簇索引创建的现有主键吗?

我还需要做些什么来使我的列存储索引与 table 的现有分区对齐吗?

请指导。

 CREATE TABLE [dbo].[ORDHDR](
        [DATE_DWID] [bigint] NOT NULL,
        [VERSION] [bigint] NOT NULL,
        [LOCATION_DWID] [bigint] NOT NULL,
        [START_LOC_DWID] [bigint] NOT NULL,
        [DESTINATION_LOC_DWID] [bigint] NOT NULL,
        [XFY_ID] [bigint] NOT NULL,
        [START_DWID] [bigint] NOT NULL,
        [END_DWID] [bigint] NOT NULL,
        [START_REQ_DWID] [bigint] NOT NULL,
        [END_IYF_DWID] [bigint] NOT NULL,
        [CREATED_AT_DWID] [bigint] NOT NULL,
        [TIME_OF_IPB_DWID] [bigint] NOT NULL,
        [DATAREC_NUM] [int] NOT NULL,
        [REQUEST_FOR_DATA_TRANSFER] [varchar](30) NULL,
        [DATAPCKT_NUM] [varchar](6) NOT NULL,
        [INTERNAL_NUM_FOR_SUPPLY] [varchar](30) NULL,
        [SOURCE_SUPPLY] [varchar](60) NULL,
        [RECORD_MODE] [varchar](1) NULL,
        [ORD_TYPE] [varchar](3) NULL,
        [APO_ORD] [varchar](12) NULL,
        [APO_APPLICATION] [int] NULL,
        [SUPPLY_CATEGORY] [varchar](12) NULL,
        [CONVERTABLE_ORD] [varchar](1) NULL,
        [ORDSTATUS_OUTPUT] [varchar](1) NULL,
        [ORDSTATUS_INPUT] [varchar](1) NULL,
        [PARTIAL_DELIVERY_STATUS] [varchar](1) NULL,
        [FINAL_DELIVERY_INDICATOR] [varchar](1) NULL,
        [STATUS_DEALLOCATED] [varchar](1) NULL,
        [STATUS_RELEASED] [varchar](1) NULL,
        [STATUS_FIXED] [varchar](1) NULL,
        [STATUS_STARTED] [varchar](1) NULL,
        [ORD_COMPONENT_ISSUED] [int] NULL,
        [PARTIALLY_CONFIRMED] [varchar](1) NULL,
        [FINAL_CONFORMATION] [varchar](1) NULL,
        [ORD_PLNG_TYPE] [int] NULL,
        [ORD_STATUS] [int] NULL,
        [START_TIME_OF_ACTIVITY] [varchar](15) NULL,
        [END_DATE_OF_LATEST_ACTIVITY] [varchar](15) NULL,
        [FLAG] [varchar](1) NULL,
        [EDW_CREATE_DATE] [datetime] NULL,
        [EDW_UPDATE_DATE] [datetime] NULL
    ) ON [ORD_PS]([DATE_DWID])

    GO

    CREATE UNIQUE CLUSTERED INDEX [ORD_HDR_PK] ON [dbo].[ORDHDR]
    (
        [DATE_DWID] ASC,
        [VERSION] ASC,
        [LOCATION_DWID] ASC,
        [START_LOC_DWID] ASC,
        [DESTINATION_LOC_DWID] ASC,
        [XFY_ID] ASC,
        [START_DWID] ASC,
        [END_DWID] ASC,
        [START_REQ_DWID] ASC,
        [END_IYF_DWID] ASC,
        [CREATED_AT_DWID] ASC,
        [TIME_OF_IPB_DWID] ASC,
        [DATAREC_NUM] ASC,
        [DATAPCKT_NUM] ASC
    )

由于聚集列存储索引只有列而没有键,您需要将现有的唯一聚集索引更改为聚集列存储索引,以将行存储 table 转换为列存储,然后创建一个新的非聚集 b 树索引以强制唯一性。

这可以通过 CREATE CLUSTERED COLUMNSTORE INDEXDROP_EXISTING=ON 子句完成,然后创建新索引。

--change existing clustered index to clustered columnstore
CREATE CLUSTERED COLUMNSTORE INDEX ORD_HDR_PK ON [dbo].[ORDHDR]
    WITH(DROP_EXISTING=ON) ON [ORD_PS]([DATE_DWID]);

--rename columnstore index to a more meaningful name
EXEC sp_rename 'dbo.ORDHDR.ORD_HDR_PK','ccidx_ORDHDR', 'INDEX';

--create new non-clustered unique index
CREATE UNIQUE NONCLUSTERED INDEX [ORD_HDR_PK] ON [dbo].[ORDHDR]
    (
        [DATE_DWID] ASC,
        [VERSION] ASC,
        [LOCATION_DWID] ASC,
        [START_LOC_DWID] ASC,
        [DESTINATION_LOC_DWID] ASC,
        [XFY_ID] ASC,
        [START_DWID] ASC,
        [END_DWID] ASC,
        [START_REQ_DWID] ASC,
        [END_IYF_DWID] ASC,
        [CREATED_AT_DWID] ASC,
        [TIME_OF_IPB_DWID] ASC,
        [DATAREC_NUM] ASC,
        [DATAPCKT_NUM] ASC
    ) ON [ORD_PS]([DATE_DWID]);