创建SQL Server 2008分区对齐索引视图


在此练习中,您将创建分区对齐索引视图。索引可以包含表中所有行的数据,不过,它消除了分区的某些优势。但是,可以创建分区对齐视图,在分区对齐视图上又可以创建索引。然后,无论查询是否显式使用分区对齐视图,都可以使用这些分区对齐索引。

  注意: 您可以复制此练习中所用的脚本,这些脚本位于 C:\SQLHOLS\Partitioning\Solution\ Partition Processing 文件夹中的 Partition Processing.ssmssln 解决方案中。

  1.创建分区对齐视图

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

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

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

  (4)键入下面的代码。


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

-->  USE AdventureWorksDW

  GO

  CREATE VIEW vw_InternetSales2001

  WITH SCHEMABINDING

  AS

  SELECT

  [InternetSalesID],

  [ProductKey],

  [FullDateAlternateKey] as OrderDate,

  [fisp].[OrderDateKey],

  [SalesOrderNumber],

  [OrderQuantity],

  [UnitPrice]

  FROM [dbo].[FactInternetSalesPartitioned] AS fisp

  INNER JOIN [dbo].[DimTime] AS dt ON [fisp].[OrderDateKey]=[dt].[TimeKey]

  WHERE [DueDateKey]<185

  GO

  CREATE VIEW vw_InternetSales2002

  WITH SCHEMABINDING

  AS

  SELECT

  [InternetSalesID],

  [ProductKey],

  [FullDateAlternateKey] as OrderDate,

  [fisp].[OrderDateKey],

  [SalesOrderNumber],

  [OrderQuantity],

  [UnitPrice]

  FROM [dbo].[FactInternetSalesPartitioned] AS fisp

  INNER JOIN [dbo].[DimTime] AS dt ON [fisp].[OrderDateKey]=[dt].[TimeKey]

  WHERE [DueDateKey] BETWEEN 185 AND 549

  GO

  CREATE VIEW vw_InternetSales2003

  WITH SCHEMABINDING

  AS

  SELECT

  [InternetSalesID],

  [ProductKey],

  [FullDateAlternateKey] as OrderDate,

  [fisp].[OrderDateKey],

  [SalesOrderNumber],

  [OrderQuantity],

  [UnitPrice]

  FROM [dbo].[FactInternetSalesPartitioned] AS fisp

  INNER JOIN [dbo].[DimTime] AS dt ON [fisp].[OrderDateKey]=[dt].[TimeKey]

  WHERE [DueDateKey]>549

  GO

  (5)单击执行。

  2.创建索引

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

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

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

  (4)键入下面的代码。


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

-->  USE AdventureWorksDW

  GO

  CREATE UNIQUE CLUSTERED INDEX idx_CL_vw_InternetSales2001

  on [dbo].[vw_InternetSales2001]

  (

  [OrderDateKey] ASC,

  [InternetSalesID] ASC,

  [ProductKey] ASC,

  [OrderDate] ASC,

  [SalesOrderNumber] ASC,

  [OrderQuantity],

  [UnitPrice])

  ON ps_OrderDateKey(OrderDateKey)

  GO

  CREATE UNIQUE CLUSTERED INDEX idx_CL_vw_InternetSales2002

  on [dbo].[vw_InternetSales2002]

  (

  [OrderDateKey] ASC,

  [InternetSalesID] ASC,

  [ProductKey] ASC,

  [OrderDate] ASC,

  [SalesOrderNumber] ASC,

  [OrderQuantity],

  [UnitPrice])

  ON ps_OrderDateKey(OrderDateKey)

  GO

  CREATE UNIQUE CLUSTERED INDEX idx_CL_vw_InternetSales2003

  on [dbo].[vw_InternetSales2003]

  (

  [OrderDateKey] ASC,

  [InternetSalesID] ASC,

  [ProductKey] ASC,

  [OrderDate] ASC,

  [SalesOrderNumber] ASC,

  [OrderQuantity],

  [UnitPrice])

  ON ps_OrderDateKey(OrderDateKey)

  GO

  (5)单击执行。

  3.查看查询执行计划

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

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

  (3)键入 View Execution Plan.sql,然后按 Enter。

  (4)键入下面的代码。


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

-->  USE AdventureWorksDW

  SELECT ProductKey, OrderQuantity

  FROM vw_InternetSales2003

  WHERE OrderDate BETWEEN '01/01/2003' AND '06/06/2003'

  GO

  (5)在工具栏中,单击显示估计的执行计划。

  (6)在得到的执行计划中,确认查询优化器选择了 idx_CL_vw_InternetSales2003 分区索引。

  注意:优化器会选择较小的视图索引,而不选择较大的表索引。可使用分区对齐索引视图改善具有分区表的系统的性能。

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


« 
» 
快速导航

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