Sql-Server

在表中的分區之間移動數據

  • November 27, 2018

有一張帶分區的表,其中分區函式和方案定義如下:

CREATE PARTITION FUNCTION DateRangePF (CHAR(8))
 AS RANGE RIGHT FOR VALUES ('20180101','20190101')

CREATE PARTITION SCHEME DateRangePS 
 AS PARTITION DateRangePF TO (Y2018FG, Y2019FG);

分區中有大量數據,邊界值為“20190101”,我們決定將其劃分為 4 個部分,具有以下邊界值和關聯的文件組:

  • 帶有文件組 Y012019FG 的“20190101”,
  • 帶有文件組 Y042019FG 的“20190401”,
  • 帶有文件組 Y072019FG 的“20190701”,
  • 帶有文件組 Y102019FG 的“20191001”。

現在,將數據從邊界值為“20190101”的舊單個分區切換到這四個分區的最佳策略是什麼?

現在,將數據從邊界值為“20190101”的舊單個分區切換到這四個分區的最佳策略是什麼?

假設您的分區表是對齊的,請使用類似分區的臨時表進行分區維護。這將避免在拆分非空分區時代價高昂的數據移動​​和日誌記錄。

以下是從您的問題中的 DDL 收集的範例。請注意,您的分區方案 DDL 不正確,因為具有 2 個邊界的分區函式創建了 3 個分區,第一個分區用於 2018 年之前的數據。所有 3 個分區必須映射到一個文件組,因此我PRIMARY在此腳本中使用。不知道為什麼選擇CHAR(8)而不是DATE分區列數據類型,因為DATE與 8 個相比,每行只需要 4 個字節。

--1) create a staging partition function, scheme, and aligned staging table like the original but with different names:

CREATE PARTITION FUNCTION DateRangePF_Staging (CHAR(8))
   AS RANGE RIGHT FOR VALUES ('20180101','20190101');

CREATE PARTITION SCHEME DateRangePS_Staging
   AS PARTITION DateRangePF_Staging TO ([PRIMARY], [Y2018FG], [Y2019FG]);

CREATE TABLE YourTable_Staging (
     PartitioningColumn CHAR(8) NOT NULL
   , OtherColumn int NOT NULL
) ON DateRangePS_Staging(PartitioningColumn);

CREATE CLUSTERED INDEX cidx 
   ON YourTable_Staging(PartitioningColumn) 
   ON DateRangePS_Staging(PartitioningColumn);
CREATE NONCLUSTERED INDEX ncidx 
   ON YourTable_Staging(OtherColumn)
   ON DateRangePS_Staging(PartitioningColumn);
GO


--2) `SWITCH` the '20190101' partition into the staging table:

ALTER TABLE YourTable
   SWITCH PARTITION $PARTITION.DateRangePF('20190101')
   TO YourTable_Staging PARTITION $PARTITION.DateRangePF_Staging('20190101');

--3) split the original partition function to create new boundaries (the split partition is empty after `SWITCH`):

ALTER PARTITION SCHEME DateRangePS
   NEXT USED Y2019FG;
ALTER PARTITION FUNCTION DateRangePF()
   SPLIT RANGE('20190401');
ALTER PARTITION SCHEME DateRangePS
   NEXT USED Y2019FG;
ALTER PARTITION FUNCTION DateRangePF()
   SPLIT RANGE('20190701');
ALTER PARTITION SCHEME DateRangePS
   NEXT USED Y2019FG;
ALTER PARTITION FUNCTION DateRangePF()
   SPLIT RANGE('20191001');
GO

--4) repartition the staging table and indexes using the original partition scheme:    

CREATE CLUSTERED INDEX cidx 
   ON YourTable_Staging(PartitioningColumn) 
   WITH(DROP_EXISTING=ON)    
   ON DateRangePS(PartitioningColumn);
CREATE NONCLUSTERED INDEX ncidx 
   ON YourTable_Staging(OtherColumn)
   WITH(DROP_EXISTING=ON)    
   ON DateRangePS(PartitioningColumn);

ALTER TABLE YourTable_Staging
   SWITCH PARTITION $PARTITION.DateRangePF('20190101')
   TO YourTable PARTITION $PARTITION.DateRangePF('20190101');
ALTER TABLE YourTable_Staging
   SWITCH PARTITION $PARTITION.DateRangePF('20190401')
   TO YourTable PARTITION $PARTITION.DateRangePF('20190401');
ALTER TABLE YourTable_Staging
   SWITCH PARTITION $PARTITION.DateRangePF('20190701')
   TO YourTable PARTITION $PARTITION.DateRangePF('20190701');
ALTER TABLE YourTable_Staging
   SWITCH PARTITION $PARTITION.DateRangePF('20191001')
   TO YourTable PARTITION $PARTITION.DateRangePF('20191001');
GO            

--5) update stats after SWITCH
UPDATE STATISTICS YourTable;    
GO

--6) drop staging objects
DROP TABLE YourTable_Staging;
DROP PARTITION SCHEME DateRangePS_Staging;
DROP PARTITION FUNCTION DateRangePF_Staging;
GO

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