使用SQL Server 2008实现可调窗口应用


在此练习中,您将实现一个可调窗口应用场景,通过将分区切换入表中以及从表中将分区切换出去实现此应用场景。在大多数系统中,最常使用的数据都是最新的数据。在非常大的表中,定期对时间最早的数据进行存档很有用,这样可以改善性能,对给新数据创建空间也是很有必要的。使用 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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)单击执行。

  4.查看分区数据

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

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

  (3)键入 View Archive Data.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  GO

  SELECT * FROM [dbo].[vw_InternetSales2001]

  GO

  SELECT * FROM [dbo].[FactInternetSalesArchive]

  GO
  5.单击执行。

  注意,FactInternetSalesPartitioned 表中包含 2001 年的数据,而 FactInternetSalesArchive 表中无数据。

  5.拆分存档分区函数以创建空分区

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

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

  (3)键入 Alter Archive Partition Function.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKeyArchive()

  SPLIT RANGE(550)

  GO
  (5)单击执行。

  注意:使用 SPLIT,您可以在分区函数中创建额外的边界。

  6.将数据切换到存档表中的空分区

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

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

  (3)键入 Switch Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER TABLE FactInternetSalesPartitioned SWITCH PARTITION 1 TO FactInternetSalesArchive PARTITION 1

  GO
  (5)单击执行。

  注意:使用 SWITCH,您可以将数据从一个表中的分区移动到另一个表中的分区。由于可能实际上并无数据移动,因而该移动过程可以非常迅速
7.合并分区函数

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

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

  (3)键入 Merge Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKey()MERGE RANGE (185)

  GO

  ALTER PARTITION FUNCTION pf_OrderDateKeyArchive ()MERGE RANGE (185)

  GO
  (5)单击执行。

  注意:使用 MERGE,您可以从分区函数删除边界。

  8.拆分分区函数以便为新数据创建分区

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

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

  (3)键入 Split Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKey()SPLIT RANGE (915)

  GO
  (5)单击执行。

  9.查看分区数据

  (1)返回到 View Archive Data.sql 查询窗口。

  (2)单击执行。

  (3)注意,FactInternetSalesPartitioned 表中没有 2001 年的数据,而 FactInternetSalesArchive 表中有数据。

  注意:使用可调窗口方法,您已将数据移动到存档表中并为下一年的数据提供了新的分区函数边界。

  (4)关闭 SQL Server Management Studio。如果收到保存作业的提示,请勿保存。

  (5)关闭 Virtual PC,放弃更改

在此练习中,您将实现一个可调窗口应用场景,通过将分区切换入表中以及从表中将分区切换出去实现此应用场景。在大多数系统中,最常使用的数据都是最新的数据。在非常大的表中,定期对时间最早的数据进行存档很有用,这样可以改善性能,对给新数据创建空间也是很有必要的。使用 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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  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)单击执行。

  4.查看分区数据

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

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

  (3)键入 View Archive Data.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  GO

  SELECT * FROM [dbo].[vw_InternetSales2001]

  GO

  SELECT * FROM [dbo].[FactInternetSalesArchive]

  GO
  5.单击执行。

  注意,FactInternetSalesPartitioned 表中包含 2001 年的数据,而 FactInternetSalesArchive 表中无数据。

  5.拆分存档分区函数以创建空分区

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

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

  (3)键入 Alter Archive Partition Function.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKeyArchive()

  SPLIT RANGE(550)

  GO
  (5)单击执行。

  注意:使用 SPLIT,您可以在分区函数中创建额外的边界。

  6.将数据切换到存档表中的空分区

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

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

  (3)键入 Switch Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER TABLE FactInternetSalesPartitioned SWITCH PARTITION 1 TO FactInternetSalesArchive PARTITION 1

  GO
  (5)单击执行。

  注意:使用 SWITCH,您可以将数据从一个表中的分区移动到另一个表中的分区。由于可能实际上并无数据移动,因而该移动过程可以非常迅速
7.合并分区函数

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

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

  (3)键入 Merge Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKey()MERGE RANGE (185)

  GO

  ALTER PARTITION FUNCTION pf_OrderDateKeyArchive ()MERGE RANGE (185)

  GO
  (5)单击执行。

  注意:使用 MERGE,您可以从分区函数删除边界。

  8.拆分分区函数以便为新数据创建分区

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

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

  (3)键入 Split Partition.sql,然后按 Enter。

  (4)键入下面的代码。


Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->  USE AdventureWorksDW

  ALTER PARTITION FUNCTION pf_OrderDateKey()SPLIT RANGE (915)

  GO
  (5)单击执行。

  9.查看分区数据

  (1)返回到 View Archive Data.sql 查询窗口。

  (2)单击执行。

  (3)注意,FactInternetSalesPartitioned 表中没有 2001 年的数据,而 FactInternetSalesArchive 表中有数据。

  注意:使用可调窗口方法,您已将数据移动到存档表中并为下一年的数据提供了新的分区函数边界。

  (4)关闭 SQL Server Management Studio。如果收到保存作业的提示,请勿保存。

  (5)关闭 Virtual PC,放弃更改


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3