Create an Indexed SQL Server View for a Repeated Aggregate

An indexed view stores a maintained result that can reduce repeated aggregate work. The first index must be unique and clustered. SQL Server then maintains that result when the underlying table changes, which trades read work for storage and additional write work.

Prepare an Isolated Example

Use a disposable development database where these demonstration names do not exist and where you can create tables, views and indexes. Do not drop existing objects just to make this example run. Execute the sections in the same SSMS or sqlcmd session because the required SET options must remain in effect.

SQL
SET NUMERIC_ROUNDABORT OFF;
SET ANSI_NULLS ON;
SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON;
SET ARITHABORT ON;
SET CONCAT_NULL_YIELDS_NULL ON;
SET QUOTED_IDENTIFIER ON;

CREATE TABLE dbo.IndexedViewDemoSale
(
    SaleId int NOT NULL PRIMARY KEY,
    ProductId int NOT NULL,
    Amount decimal(12,2) NOT NULL
);

INSERT dbo.IndexedViewDemoSale (SaleId, ProductId, Amount)
VALUES (1, 10, 12.50), (2, 10, 7.50), (3, 20, 9.00);
GO

The objects below use the same owner and schema. The view references its base table with a two-part name and avoids nondeterministic expressions.

Define and Index the Aggregate

CREATE VIEW needs its own batch. For this grouped view, COUNT_BIG(*) is part of the required definition.

SQL
CREATE VIEW dbo.IndexedViewDemoTotals
WITH SCHEMABINDING
AS
    SELECT ProductId,
           COUNT_BIG(*) AS SaleCount,
           SUM(Amount) AS TotalAmount
    FROM dbo.IndexedViewDemoSale
    GROUP BY ProductId;
GO

CREATE UNIQUE CLUSTERED INDEX CIX_IndexedViewDemoTotals
ON dbo.IndexedViewDemoTotals (ProductId);
GO

ProductId is unique in the grouped result. The non-null exact-decimal amount also avoids nullable aggregate and floating-point precision complications.

Read and Verify the Materialized Result

SQL
SELECT ProductId, SaleCount, TotalAmount
FROM dbo.IndexedViewDemoTotals WITH (NOEXPAND)
ORDER BY ProductId;

Expect product 10 to have count 2 and total 20.00, and product 20 to have count 1 and total 9.00. NOEXPAND keeps this explicit query on the indexed view. Automatic substitution and optimizer behavior depend on the SQL Server edition and query, so verify the actual plan instead of assuming every matching query will use it.

Evaluate the Cost and Clean Up

All connections modifying participating tables need the required SET configuration. Indexed views have detailed expression and syntax restrictions, and schema binding also constrains changes to referenced objects. Review those requirements for the real view, then benchmark representative reads and writes.

After finishing this isolated example, remove only its two objects:

SQL
DROP VIEW dbo.IndexedViewDemoTotals;
DROP TABLE dbo.IndexedViewDemoSale;

Keep the names and execution context explicit. An indexed view is appropriate when measured savings justify its maintenance cost, not simply because an aggregate can be materialized.

Microsoft References

Author: Herman Schoenfeld