Sql-Server

在不屬於目前聚集索引的列上對錶進行分區

  • April 1, 2017

我正在對下表進行分區,3 億行,從 2011 年到今天的數據(測試系統中到 2017 年 3 月 24 日)

我想將我的表分區到一個名為的列上,該列具有以下格式的日期,OrderDateSID int例如:20170401``April 1st 2017

這是我的表定義,它很長,還包括所有目前索引:

IF OBJECT_ID('[Facts].[FactOrder]') IS NOT NULL 
DROP TABLE [Facts].[FactOrder] 
GO
CREATE TABLE [Facts].[FactOrder] ( 
[FactOrderSID]              BIGINT                           NOT NULL,
[OrderNo]                   VARCHAR(20)                          NULL,
[OrderItemSeqNo]            SMALLINT                             NULL,
[OrderNoItemSeqNo]          VARCHAR(30)                          NULL,
[OrderDetailsSID]           INT                                  NULL,
[ChannelSID]                INT                                  NULL,
[OrderDateSID]              INT                                  NULL,
[OrderTypeSID]              INT                                  NULL,
[CoreStatusSID]             INT                                  NULL,
[ProductSubSeasonID]        INT                                  NULL,
[OrderSubSeasonID]          INT                                  NULL,
[MarketSID]                 INT                                  NULL,
[ProductSID]                INT                                  NULL,
[CurrencyCodeSID]           INT                                  NULL,
[LocalOrderDateSID]         INT                                  NULL,
[FullSellingPrice]          NUMERIC(18,4)                        NULL,
[PriceSetPrice]             NUMERIC(18,4)                        NULL,
[ActualSellingPrice]        NUMERIC(18,4)                        NULL,
[ActualTaxableAmount]       NUMERIC(18,4)                        NULL,
[CostofGoodsAmount]         NUMERIC(18,4)                        NULL,
[ShipFullPrice]             NUMERIC(18,4)                        NULL,
[ShipActualPrice]           NUMERIC(18,4)                        NULL,
[ShipActualTaxAmount]       NUMERIC(18,4)                        NULL,
[BCFullSellingPrice]        NUMERIC(18,4)                        NULL,
[BCPriceSetPrice]           NUMERIC(18,4)                        NULL,
[BCActualSellingPrice]      NUMERIC(18,4)                        NULL,
[BCActualTaxableAmount]     NUMERIC(18,4)                        NULL,
[BCCostofGoodsAmount]       NUMERIC(18,4)                        NULL,
[BCShipFullPrice]           NUMERIC(18,4)                        NULL,
[BCShipActualPrice]         NUMERIC(18,4)                        NULL,
[BCShipActualTaxAmount]     NUMERIC(18,4)                        NULL,
[UnitsSold]                 INT                                  NULL,
[LoadID]                    INT                                  NULL,
[ActualProductSID]          INT                                  NULL,
[AccountSID]                INT                              NOT NULL,
[PriceDiscount]             NUMERIC(18,4)                        NULL,
[BCPriceDiscount]           NUMERIC(18,4)                        NULL,
[PromotionalDiscount]       NUMERIC(18,4)                        NULL,
[BCPromotionalDiscount]     NUMERIC(18,4)                        NULL,
[GrossDemand]               NUMERIC(18,4)                        NULL,
[BCGrossDemand]             NUMERIC(18,4)                        NULL,
[UKFSPIncVAT]               NUMERIC(18,4)                        NULL,
[UKFSPExVAT]                NUMERIC(18,4)                        NULL,
[BCTotalDiscount]           NUMERIC(18,4)                        NULL,
[TotalDiscount]             NUMERIC(18,4)                        NULL,
[OrderNoItemSeqNoSID]       INT                              NOT NULL,
[OfferSID]                  INT                                  NULL,
[MarkdownStatusSID]         INT                              NOT NULL,
[AdvertisedPrice]           NUMERIC(18,4)                        NULL,
[BCAdvertisedPrice]         NUMERIC(18,4)                        NULL,
[CataloguePriceSetTypeSID]  INT                              NOT NULL,
[UserSID]                   INT                                  NULL,
CONSTRAINT   [PK_FactOrder]  
PRIMARY KEY CLUSTERED    ([FactOrderSID] asc) WITH FILLFACTOR = 100)

GO

CREATE NONCLUSTERED INDEX [Facts_FactOrder_LoadID] 
  ON [Facts].[FactOrder] ([LoadID] asc)
  INCLUDE ([FactOrderSID])
  WITH FILLFACTOR = 100
CREATE NONCLUSTERED INDEX [Facts_FactOrder_OrderDateSID] 
  ON [Facts].[FactOrder] ([OrderDateSID] asc)
  INCLUDE ([FactOrderSID])
  WITH FILLFACTOR = 100
CREATE NONCLUSTERED INDEX [Facts_FactOrder_OrderNoItemSeqNoSID] 
  ON [Facts].[FactOrder] ([OrderNoItemSeqNoSID] asc)
  INCLUDE ([FactOrderSID], [OrderDateSID])
  WITH FILLFACTOR = 100
CREATE NONCLUSTERED INDEX [IDX_Facts_FactOrder_Channel_INC_AccountSID] 
  ON [Facts].[FactOrder] ([AccountSID] asc)
  INCLUDE ([ChannelSID])
  WITH FILLFACTOR = 100
CREATE NONCLUSTERED INDEX [IX_Facts_FactOrder_OrderDetailsSID] 
  ON [Facts].[FactOrder] ([OrderDetailsSID] asc)
  WITH FILLFACTOR = 100
CREATE NONCLUSTERED INDEX [IX_Facts_FactOrder_UKFSPExVAT__INCLU] 
  ON [Facts].[FactOrder] ([UKFSPExVAT] asc)
  INCLUDE ([ActualProductSID], [MarketSID])
  WITH FILLFACTOR = 100

我已經創建了分區函式

----------------------------------------------------------------------------
--- create a partition function
----------------------------------------------------------------------------

use radhe
go


IF EXISTS (SELECT * FROM sys.partition_functions
   WHERE name = 'PF_Facts_FactOrder_OrderDateSID')
DROP PARTITION FUNCTION PF_Facts_FactOrder_OrderDateSID ;
GO

CREATE PARTITION FUNCTION PF_Facts_FactOrder_OrderDateSID (int)
AS RANGE LEFT FOR VALUES (  20110101,
                           20120101,
                           20130101,
                           20140101,
                           20150101,
                           20160101,
                           20170101);

我已經創建了分區模式:

----------------------------------------------------------------------------
--- Create a Partition Scheme
----------------------------------------------------------------------------
use radhe
go
DROP PARTITION SCHEME PSC_OrderDateSID 
GO


CREATE PARTITION SCHEME PSC_OrderDateSID AS 
PARTITION PF_Facts_FactOrder_OrderDateSID   TO 
([F0],[F1], [F2], [F3], [F4], [F5], [F6], [F7]) 
GO

該表具有不包含分區列但作為 PRIMARY KEY 或 UNIQUE 約束的一部分創建的聚集索引

我的問題是:

我無法在這兩個選項之間下定決心:

1)

更改集群主鍵,將分區列添加到其中

2)

更改主鍵以NONCLUSTERED 創建此索引CLUSTEREDFacts_FactOrder_OrderDateSID

這兩個選項的優缺點是什麼?我將如何四處選擇哪一個專門用於此目的(需要根據OrderDateSID列改進刪除)?

基於您希望改進基於 OrderDateSID 的刪除(通過可能利用分區消除),我根本不認為選項 2 是一個選項。

分區方案中涉及的所有唯一約束必須將分區列定義為基本索引定義(索引鍵的子集)的一部分 - (不僅僅是包含的列)。換句話說,您的主鍵(即使定義為非集群)也無法參與此分區方案,因為基本索引定義不包括分區鍵列。

如果您嘗試根據分區方案創建主鍵

ALTER TABLE [Facts].[FactOrder] ADD  CONSTRAINT [PK_FactOrder] PRIMARY KEY NONCLUSTERED 
(
   [FactOrderSID] ASC
)On PSC_OrderDateSID(OrderDateSID)
GO

你會收到這個錯誤

消息 1908,級別 16,狀態 1,第 3 行列“OrderDateSID”是索引“PK_FactOrder”的分區列。唯一索引的分區列必須是索引鍵的子集。消息 1750,級別 16,狀態 1,行 3 無法創建約束或索引。請參閱以前的錯誤。

現在,您當然可以創建不同的分區函式和方案來支持主鍵的分區(可能有助於將索引重建為分區級別),但它不會與其他分區對齊,因此不允許您進行分區切換 - 也不會參與分區消除,因為 OrderDateSID 不是基本索引定義的一部分。

我認為您唯一的選擇是選項1。

引用自:https://dba.stackexchange.com/questions/168828