【IT168 技术文档】在此练习中,您将实现一个可调窗口应用场景,通过将分区切换入表中以及从表中将分区切换出去实现此应用场景。在大多数系统中,最常使用的数据都是最新的数据。在非常大的表中,定期对时间最早的数据进行存档很有用,这样可以改善性能,对给新数据创建空间也是很有必要的。使用 SQL Server 2008 中分区表的 SPLIT、SWITCH 和 MERGE 功能,您可以极快地执行存档任务。
注意: 您可以复制此练习中所用的脚本,这些脚本位于 C:\SQLHOLS\Partitioning\Solution\Partition Processing 文件夹中的 Partition Processing.ssmssln 解决方案中。
1.为存档表创建分区函数
(1)在解决方案资源管理器中,右键单击该连接,然后单击新建查询。
(2)右键单击 SQLQuery1.sql,然后单击重命名。
(3)键入 Create Archive Partition Function.sql,然后按 Enter。
(4)键入下面的代码。
CREATE PARTITION FUNCTION pf_OrderDateKeyArchive(int)
AS RANGE RIGHT
FOR VALUES(185)
GO
5.单击执行。
2.为存档表创建分区方案
(1)在解决方案资源管理器中,右键单击该连接,然后单击新建查询。
(2)右键单击 SQLQuery1.sql,然后单击重命名。
(3)键入 Create Archive Partition Scheme.sql,然后按 Enter。
(4)键入下面的代码。
CREATE PARTITION SCHEME ps_OrderDateKeyArchive
AS PARTITION pf_OrderDateKeyArchive
TO (fg2001,fg2002,fg2003)
GO
(5)单击执行。
3.为存档数据创建分区表
(1)在解决方案资源管理器中,右键单击该连接,然后单击新建查询。
(2)右键单击 SQLQuery1.sql,然后单击重命名。
(3)键入 Create Archive Table.sql,然后按 Enter。
(4)键入下面的代码。
CREATE TABLE [dbo].[FactInternetSalesArchive]
(
[InternetSalesID] [int] IDENTITY(1,1) NOT NULL,
[ProductKey] [int] NOT NULL,
[OrderDateKey] [int] NOT NULL,
[DueDateKey] [int] NOT NULL,
[ShipDateKey] [int] NOT NULL,
[CustomerKey] [int] NOT NULL,
[PromotionKey] [int] NOT NULL,
[CurrencyKey] [int] NOT NULL,
[SalesTerritoryKey] [int] NOT NULL,
[SalesOrderNumber] [nvarchar](20) NOT NULL,
[OrderQuantity] [smallint] NULL,
[UnitPrice] [money] NULL,
CONSTRAINT [PK_ FactInternetSalesArchive] PRIMARY KEY CLUSTERED
(
[InternetSalesID],
[ProductKey],
[OrderDateKey],
[DueDateKey],
[ShipDateKey],
[CustomerKey],
[PromotionKey],
[CurrencyKey],
[SalesTerritoryKey]
)
)
ON ps_OrderDateKeyArchive(OrderDateKey)
GO
(5)单击执行。