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

原创|其它|编辑:郝浩|2009-12-31 09:43:03.000|阅读 499 次

概述:在此练习中,您将实现一个可调窗口应用场景,通过将分区切换入表中以及从表中将分区切换出去实现此应用场景。在大多数系统中,最常使用的数据都是最新的数据。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

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

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

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

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

  USE AdventureWorksDW
  GO
  SELECT * FROM [dbo].[vw_InternetSales2001]
  GO
  SELECT * FROM [dbo].[FactInternetSalesArchive]
  GO

  3.为存档数据创建分区表

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

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

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

  (4)键入下面的代码。

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

  USE AdventureWorksDW
  GO
  SELECT * FROM [dbo].[vw_InternetSales2001]
  GO
  SELECT * FROM [dbo].[FactInternetSalesArchive]
  GO

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

  7.合并分区函数

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

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

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

  (4)键入下面的代码。

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

  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,放弃更改。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP