技术开发 频道

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

  2.创建索引

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

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

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

  (4)键入下面的代码。

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

  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 打开,下一个练习还要使用此程序。

0
相关文章