SQL Server 2000 Indexed Views are similar to
Materialized Views in
Oracle - the Result Set is stored in the Database. Query Performance can be
dramatically enhanced using Indexed Views. Create an Indexed View by implementing
a UNIQUE CLUSTERED index on the view. The results of the view are stored in the leaf-level pages of the clustered
index.
An Indexed View automatically reflects modifications made to the data in the base
tables after the index is created, the same way an index created on a base table
does. As modifications are made to the data in the base tables, the data modifications
are also reflected in the data stored in the indexed view. The requirement that the
clustered index of the view be unique improves the efficiency with which SQL Server 2000
can find the rows in the index that are affected by any data modification.
SET SHOWPLAN_ALL ON
SELECT SUM(UnitPrice*Quantity*(1.00-Discount)) AS
Rev,
OrderDate, ProductID
FROM dbo.[Order Details] od, dbo.Orders o
WHERE od.OrderID=o.OrderID AND ProductID in (2,4,25)
AND OrderDate >= '05/01/1998'
GROUP BY OrderDate, ProductID
ORDER BY Rev DESC
|--Sort(ORDER BY)
|--Stream Aggregate(GROUP BY) ...
|--Sort(ORDER BY) ...
|--Nested Loops(Inner Join) ...
|--Index Seek
...
|--Clustered Index
Seek ...
USE Northwind
GO
SET SHOWPLAN_ALL OFF
GO
-- Set the options to support Index
Views
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING,ANSI_WARNINGS,CONCAT_NULL_YIELDS_NULL ON
GO
SET ARITHABORT,QUOTED_IDENTIFIER,ANSI_NULLS ON
GO
-- Create the Index View
DROP VIEW VW_summary
CREATE VIEW VW_summary
WITH SCHEMABINDING
AS
SELECT SUM(UnitPrice*Quantity*(1.00-Discount)) AS Rev,
OrderDate, ProductID, COUNT_BIG(*)
AS COUNT
FROM dbo.[Order Details] od, dbo.Orders o
WHERE od.OrderID=o.OrderID
GROUP BY OrderDate, ProductID
GO
-- Create UNIQUE CLUSTERED index on the view
CREATE UNIQUE CLUSTERED INDEX IVW_summary
ON VW_summary (OrderDate, ProductID)
GO
|--Sort(ORDER
BY:([VW_summary] ...
|--Clustered Index Seek ...
If you prefer a graphical representation of the
execution plan, use Query Analyzer.