技术开发 频道

对SQL Server跨文件组的表进行分区

  创建分区函数

  1. 在解决方案资源管理器中,右键单击该连接,然后单击新建查询。

  2. 右键单击 SQLQuery1.sql,然后单击重命名。

  3. 键入 Create Partition Function.sql,然后按 Enter。

  4. 键入下面的代码。

USE AdventureWorksDW

  
CREATE PARTITION FUNCTION pf_OrderDateKey(int)

  
AS RANGE RIGHT

  
FOR VALUES(185,550)

  
GO

  5.单击执行。

  注意:分区函数提供了两个文件组之间的边界。在本例中,值是与 1 月 1 日对应的键。

  创建分区方案

  1. 在解决方案资源管理器中,右键单击该连接,然后单击新建查询。

  2. 右键单击 SQLQuery1.sql,然后单击重命名。

  3. 键入 Create Partition Scheme.sql,然后按 Enter。

  4. 键入下面的代码。单击执行。

 USE AdventureWorksDW

  
CREATE PARTITION SCHEME ps_OrderDateKey

  
AS PARTITION pf_OrderDateKey

  
TO (fg2001,fg2002,fg2003,fg2004)

  
GO

  注意:虽然分区函数中仅列出了两个边界,但却有四个文件组在分区函数中列出。第四个文件组是作为供将来的文件组拆分使用的下一个文件组提供的。

  创建已分区表

  1. 在解决方案资源管理器中,右键单击该连接,然后单击新建查询。

  2. 右键单击 SQLQuery1.sql,然后单击重命名。

  3. 键入 Create Table.sql,然后按 Enter。

  4. 键入下面的代码。

USE AdventureWorksDW

  
CREATE TABLE [dbo].[FactInternetSalesPartitioned]

  (

  
[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_ FactInternetSalesPartitioned] PRIMARY KEY CLUSTERED

  (

  
[InternetSalesID],

  
[ProductKey],

  
[OrderDateKey],

  
[DueDateKey],

  
[ShipDateKey],

  
[CustomerKey],

  
[PromotionKey],

  
[CurrencyKey],

  
[SalesTerritoryKey]

  )

  )

  
ON ps_OrderDateKey(OrderDateKey)

  
GO

  5.单击执行。

  将数据插入已分区表中

  1. 在解决方案资源管理器中,右键单击该连接,然后单击新建查询。

  2. 右键单击 SQLQuery1.sql,然后单击重命名。

  3. 键入 Load Data.sql,然后按 Enter。

  4. 键入下面的代码。 

USE AdventureWorksDW

  
INSERT INTO [dbo].[FactInternetSalesPartitioned]

  (

  
[ProductKey],

  
[OrderDateKey],

  
[DueDateKey],

  
[ShipDateKey],

  
[CustomerKey],

  
[PromotionKey],

  
[CurrencyKey],

  
[SalesTerritoryKey],

  
[SalesOrderNumber],

  
[OrderQuantity],

  
[UnitPrice]

  )

  
SELECT

  
[ProductKey],

  
[OrderDateKey],

  
[DueDateKey],

  
[ShipDateKey],

  
[CustomerKey],

  
[PromotionKey],

  
[CurrencyKey],

  
[SalesTerritoryKey],

  
[SalesOrderNumber],

  
[OrderQuantity],

  
[UnitPrice]

  
FROM [dbo].[FactInternetSales]

  
GO

  5.单击执行。

  查看分区数据

  1. 在解决方案资源管理器中,右键单击该连接,然后单击新建查询。

  2. 右键单击 SQLQuery1.sql,然后单击重命名。

  3. 键入 View Partitioned Data.sql,然后按 Enter。

  4. 键入下面的代码。

USE AdventureWorksDW

  SELECTProductKey,

  OrderDateKey,

  $PARTITION.pf_OrderDateKey (OrderDateKey)
AS PartitionNo

  
FROM FactInternetSalesPartitioned

  
GO

  
SELECT $PARTITION.pf_OrderDateKey (OrderDateKey) AS PartitionNo,

  
COUNT(*) AS Rows FROM FactInternetSalesPartitioned

  
GROUP BY $PARTITION.pf_OrderDateKey (OrderDateKey)

  
ORDER BY PartitionNo

  
GO

  5. 单击执行。

  6. 待查询完成后,查看结果。

  注意:第一个结果集显示表中每行的产品密钥和订单日期密钥以及存储各行的相应分区。

  第二个结果集显示各分区中的行数。

  7.保持 SQL Server Management Studio 打开,下一个练习还要使用此程序。

0
相关文章