--=========================================
-- 转换为分区表
--=========================================
-- 1. 创建分区函数
-- a. 适用于存储历史存档记录的分区表的分区函数
DECLARE@dtdatetime
SET@dt='20020101'
CREATE PARTITION FUNCTION PF_HistoryArchive(datetime)
AS RANGE RIGHT
FORVALUES(
@dt,
DATEADD(Year, 1, @dt))
-- b. 适用于存储历史记录的分区表的分区函数
--DECLARE @dt datetime
SET@dt='20000101'
CREATE PARTITION FUNCTION PF_History(datetime)
AS RANGE RIGHT
FORVALUES(
@dt,
DATEADD(Month, 1, @dt),
DATEADD(Month, 2, @dt),
DATEADD(Month, 3, @dt),
DATEADD(Month, 4, @dt),
DATEADD(Month, 5, @dt),
DATEADD(Month, 6, @dt),
DATEADD(Month, 7, @dt),
DATEADD(Month, 8, @dt),
DATEADD(Month, 9, @dt),
DATEADD(Month, 10, @dt),
DATEADD(Month, 11, @dt),
DATEADD(Month, 12, @dt))
GO
-- 2. 创建分区架构
-- a. 适用于存储历史存档记录的分区表的分区架构
CREATE PARTITION SCHEME PS_HistoryArchive
AS PARTITION PF_HistoryArchive
TO([PRIMARY], [PRIMARY], [PRIMARY])
-- b. 适用于存储历史记录的分区表的分区架构
CREATE PARTITION SCHEME PS_History
AS PARTITION PF_History
TO([PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY])
GO
-- 3. 删除索引
-- a. 删除存储历史存档记录的表中的索引
DROPINDEX Production.TransactionHistoryArchive.IX_TransactionHistoryArchive_ProductID
DROPINDEX Production.TransactionHistoryArchive.IX_TransactionHistoryArchive_ReferenceOrderID_ReferenceOrderLineID
-- b. 删除存储历史记录的表中的索引
DROPINDEX Production.TransactionHistory.IX_TransactionHistory_ProductID
DROPINDEX Production.TransactionHistory.IX_TransactionHistory_ReferenceOrderID_ReferenceOrderLineID
GO
-- 4. 转换为分区表
-- a. 将存储历史存档记录的表转换为分区表
ALTERTABLE Production.TransactionHistoryArchive
DROPCONSTRAINT PK_TransactionHistoryArchive_TransactionID
WITH(
MOVE TO PS_HistoryArchive(TransactionDate))
-- b.将存储历史记录的表转换为分区表
ALTERTABLE Production.TransactionHistory
DROPCONSTRAINT PK_TransactionHistory_TransactionID
WITH(
MOVE TO PS_History(TransactionDate))
GO
-- 5. 恢复主键
-- a. 恢复存储历史存档记录的分区表的主键
ALTERTABLE Production.TransactionHistoryArchive
ADDCONSTRAINT PK_TransactionHistoryArchive_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
-- b. 恢复存储历史记录的分区表的主键
ALTERTABLE Production.TransactionHistory
ADDCONSTRAINT PK_TransactionHistory_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
GO
-- 6. 恢复索引
-- a. 恢复存储历史存档记录的分区表的索引
CREATEINDEX IX_TransactionHistoryArchive_ProductID
ON Production.TransactionHistoryArchive(
ProductID)
CREATEINDEX IX_TransactionHistoryArchive_ReferenceOrderID_ReferenceOrderLineID
ON Production.TransactionHistoryArchive(
ReferenceOrderID,
ReferenceOrderLineID)
-- b. 恢复存储历史记录的分区表的索引
CREATEINDEX IX_TransactionHistory_ProductID
ON Production.TransactionHistory(
ProductID)
CREATEINDEX IX_TransactionHistory_ReferenceOrderID_ReferenceOrderLineID
ON Production.TransactionHistory(
ReferenceOrderID,
ReferenceOrderLineID)
GO
-- 7. 查看分区表的相关信息
SELECT
SchemaName = S.name,
TableName = TB.name,
PartitionScheme = PS.name,
PartitionFunction = PF.name,
PartitionFunctionRangeType =CASE
WHEN boundary_value_on_right =0THEN'LEFT'
ELSE'RIGHT'END,
PartitionFunctionFanout = PF.fanout,
SchemaID = S.schema_id,
ObjectID = TB.object_id,
PartitionSchemeID = PS.data_space_id,
PartitionFunctionID = PS.function_id
FROM sys.schemas S
INNERJOIN sys.tables TB
ON S.schema_id = TB.schema_id
INNERJOIN sys.indexes IDX
on TB.object_id= IDX.object_id
AND IDX.index_id <2
INNERJOIN sys.partition_schemes PS
ON PS.data_space_id = IDX.data_space_id
INNERJOIN sys.partition_functions PF
ON PS.function_id = PF.function_id
GO
--=========================================
-- 移动分区表数据
--=========================================
-- 1. 为存储历史存档记录的分区表增加分区, 并接受从历史记录分区表移动过来的数据
-- a. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_HistoryArchive
NEXT USED [PRIMARY]
-- b. 修改分区函数, 增加分区用以接受从历史记录分区表移动过来的数据
DECLARE@dtdatetime
SET@dt='20030901'
ALTER PARTITION FUNCTION PF_HistoryArchive()
SPLIT RANGE(@dt)
-- c. 将历史记录表中的过期数据移动到历史存档记录表中
ALTERTABLE Production.TransactionHistory
SWITCH PARTITION 2
TO Production.TransactionHistoryArchive PARTITION $PARTITION.PF_HistoryArchive(@dt)
-- d. 将接受到的数据与原来的分区合并
ALTER PARTITION FUNCTION PF_HistoryArchive()
MERGE RANGE(@dt)
GO
-- 2. 将存储历史记录的分区表中不包含数据的分区删除, 并增加新的分区以接受新数据
-- a. 合并不包含数据的分区
DECLARE@dtdatetime
SET@dt='20030901'
ALTER PARTITION FUNCTION PF_History()
MERGE RANGE(@dt)
-- b. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_History
NEXT USED [PRIMARY]
-- c. 修改分区函数, 增加分区用以接受新数据
SET@dt='20041001'
ALTER PARTITION FUNCTION PF_History()
SPLIT RANGE(@dt)
GO
--=========================================
-- 清除历史存档记录中的过期数据
--=========================================
-- 1. 创建用于保存过期的历史存档数据的表
CREATETABLE Production.TransactionHistoryArchive_2001_temp(
TransactionID intNOTNULL,
ProductID intNOTNULL,
ReferenceOrderID intNOTNULL,
ReferenceOrderLineID intNOTNULL
DEFAULT ((0)),
TransactionDate datetimeNOTNULL
DEFAULT (GETDATE()),
TransactionType nchar(1) NOTNULL,
Quantity intNOTNULL,
ActualCost moneyNOTNULL,
ModifiedDate datetimeNOTNULL
DEFAULT (GETDATE()),
CONSTRAINT PK_TransactionHistoryArchive_2001_temp_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
)
-- 2. 将数据从历史存档记录分区表移动到第步创建的表中
ALTERTABLE Production.TransactionHistoryArchive
SWITCH PARTITION 1
TO Production.TransactionHistoryArchive_2001_temp
-- 3. 删除不再包含数据的分区
DECLARE@dtdatetime
SET@dt='20020101'
ALTER PARTITION FUNCTION PF_HistoryArchive()
MERGE RANGE(@dt)
-- 4. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_HistoryArchive
NEXT USED [PRIMARY]
-- 5. 修改分区函数, 增加分区用以接受新数据
SET@dt='20040101'
ALTER PARTITION FUNCTION PF_HistoryArchive()
SPLIT RANGE(@dt)
-- 转换为分区表
--=========================================
-- 1. 创建分区函数
-- a. 适用于存储历史存档记录的分区表的分区函数
DECLARE@dtdatetime
SET@dt='20020101'
CREATE PARTITION FUNCTION PF_HistoryArchive(datetime)
AS RANGE RIGHT
FORVALUES(
@dt,
DATEADD(Year, 1, @dt))
-- b. 适用于存储历史记录的分区表的分区函数
--DECLARE @dt datetime
SET@dt='20000101'
CREATE PARTITION FUNCTION PF_History(datetime)
AS RANGE RIGHT
FORVALUES(
@dt,
DATEADD(Month, 1, @dt),
DATEADD(Month, 2, @dt),
DATEADD(Month, 3, @dt),
DATEADD(Month, 4, @dt),
DATEADD(Month, 5, @dt),
DATEADD(Month, 6, @dt),
DATEADD(Month, 7, @dt),
DATEADD(Month, 8, @dt),
DATEADD(Month, 9, @dt),
DATEADD(Month, 10, @dt),
DATEADD(Month, 11, @dt),
DATEADD(Month, 12, @dt))
GO
-- 2. 创建分区架构
-- a. 适用于存储历史存档记录的分区表的分区架构
CREATE PARTITION SCHEME PS_HistoryArchive
AS PARTITION PF_HistoryArchive
TO([PRIMARY], [PRIMARY], [PRIMARY])
-- b. 适用于存储历史记录的分区表的分区架构
CREATE PARTITION SCHEME PS_History
AS PARTITION PF_History
TO([PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY],
[PRIMARY], [PRIMARY], [PRIMARY])
GO
-- 3. 删除索引
-- a. 删除存储历史存档记录的表中的索引
DROPINDEX Production.TransactionHistoryArchive.IX_TransactionHistoryArchive_ProductID
DROPINDEX Production.TransactionHistoryArchive.IX_TransactionHistoryArchive_ReferenceOrderID_ReferenceOrderLineID
-- b. 删除存储历史记录的表中的索引
DROPINDEX Production.TransactionHistory.IX_TransactionHistory_ProductID
DROPINDEX Production.TransactionHistory.IX_TransactionHistory_ReferenceOrderID_ReferenceOrderLineID
GO
-- 4. 转换为分区表
-- a. 将存储历史存档记录的表转换为分区表
ALTERTABLE Production.TransactionHistoryArchive
DROPCONSTRAINT PK_TransactionHistoryArchive_TransactionID
WITH(
MOVE TO PS_HistoryArchive(TransactionDate))
-- b.将存储历史记录的表转换为分区表
ALTERTABLE Production.TransactionHistory
DROPCONSTRAINT PK_TransactionHistory_TransactionID
WITH(
MOVE TO PS_History(TransactionDate))
GO
-- 5. 恢复主键
-- a. 恢复存储历史存档记录的分区表的主键
ALTERTABLE Production.TransactionHistoryArchive
ADDCONSTRAINT PK_TransactionHistoryArchive_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
-- b. 恢复存储历史记录的分区表的主键
ALTERTABLE Production.TransactionHistory
ADDCONSTRAINT PK_TransactionHistory_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
GO
-- 6. 恢复索引
-- a. 恢复存储历史存档记录的分区表的索引
CREATEINDEX IX_TransactionHistoryArchive_ProductID
ON Production.TransactionHistoryArchive(
ProductID)
CREATEINDEX IX_TransactionHistoryArchive_ReferenceOrderID_ReferenceOrderLineID
ON Production.TransactionHistoryArchive(
ReferenceOrderID,
ReferenceOrderLineID)
-- b. 恢复存储历史记录的分区表的索引
CREATEINDEX IX_TransactionHistory_ProductID
ON Production.TransactionHistory(
ProductID)
CREATEINDEX IX_TransactionHistory_ReferenceOrderID_ReferenceOrderLineID
ON Production.TransactionHistory(
ReferenceOrderID,
ReferenceOrderLineID)
GO
-- 7. 查看分区表的相关信息
SELECT
SchemaName = S.name,
TableName = TB.name,
PartitionScheme = PS.name,
PartitionFunction = PF.name,
PartitionFunctionRangeType =CASE
WHEN boundary_value_on_right =0THEN'LEFT'
ELSE'RIGHT'END,
PartitionFunctionFanout = PF.fanout,
SchemaID = S.schema_id,
ObjectID = TB.object_id,
PartitionSchemeID = PS.data_space_id,
PartitionFunctionID = PS.function_id
FROM sys.schemas S
INNERJOIN sys.tables TB
ON S.schema_id = TB.schema_id
INNERJOIN sys.indexes IDX
on TB.object_id= IDX.object_id
AND IDX.index_id <2
INNERJOIN sys.partition_schemes PS
ON PS.data_space_id = IDX.data_space_id
INNERJOIN sys.partition_functions PF
ON PS.function_id = PF.function_id
GO
--=========================================
-- 移动分区表数据
--=========================================
-- 1. 为存储历史存档记录的分区表增加分区, 并接受从历史记录分区表移动过来的数据
-- a. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_HistoryArchive
NEXT USED [PRIMARY]
-- b. 修改分区函数, 增加分区用以接受从历史记录分区表移动过来的数据
DECLARE@dtdatetime
SET@dt='20030901'
ALTER PARTITION FUNCTION PF_HistoryArchive()
SPLIT RANGE(@dt)
-- c. 将历史记录表中的过期数据移动到历史存档记录表中
ALTERTABLE Production.TransactionHistory
SWITCH PARTITION 2
TO Production.TransactionHistoryArchive PARTITION $PARTITION.PF_HistoryArchive(@dt)
-- d. 将接受到的数据与原来的分区合并
ALTER PARTITION FUNCTION PF_HistoryArchive()
MERGE RANGE(@dt)
GO
-- 2. 将存储历史记录的分区表中不包含数据的分区删除, 并增加新的分区以接受新数据
-- a. 合并不包含数据的分区
DECLARE@dtdatetime
SET@dt='20030901'
ALTER PARTITION FUNCTION PF_History()
MERGE RANGE(@dt)
-- b. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_History
NEXT USED [PRIMARY]
-- c. 修改分区函数, 增加分区用以接受新数据
SET@dt='20041001'
ALTER PARTITION FUNCTION PF_History()
SPLIT RANGE(@dt)
GO
--=========================================
-- 清除历史存档记录中的过期数据
--=========================================
-- 1. 创建用于保存过期的历史存档数据的表
CREATETABLE Production.TransactionHistoryArchive_2001_temp(
TransactionID intNOTNULL,
ProductID intNOTNULL,
ReferenceOrderID intNOTNULL,
ReferenceOrderLineID intNOTNULL
DEFAULT ((0)),
TransactionDate datetimeNOTNULL
DEFAULT (GETDATE()),
TransactionType nchar(1) NOTNULL,
Quantity intNOTNULL,
ActualCost moneyNOTNULL,
ModifiedDate datetimeNOTNULL
DEFAULT (GETDATE()),
CONSTRAINT PK_TransactionHistoryArchive_2001_temp_TransactionID
PRIMARYKEYCLUSTERED(
TransactionID,
TransactionDate)
)
-- 2. 将数据从历史存档记录分区表移动到第步创建的表中
ALTERTABLE Production.TransactionHistoryArchive
SWITCH PARTITION 1
TO Production.TransactionHistoryArchive_2001_temp
-- 3. 删除不再包含数据的分区
DECLARE@dtdatetime
SET@dt='20020101'
ALTER PARTITION FUNCTION PF_HistoryArchive()
MERGE RANGE(@dt)
-- 4. 修改分区架构, 增加用以接受新分区的文件组
ALTER PARTITION SCHEME PS_HistoryArchive
NEXT USED [PRIMARY]
-- 5. 修改分区函数, 增加分区用以接受新数据
SET@dt='20040101'
ALTER PARTITION FUNCTION PF_HistoryArchive()
SPLIT RANGE(@dt)