Advanced SQL & Data Engineering • Training 11
Article-Training • SQL Server–Friendly Series

Indexed Views / Precomputed Results

When SQL Server Can Store the Result of a View — and What It Costs

Indexed views trade extra write work and storage for potentially faster repeated reads.

Schema-Bound View → Unique Clustered Index → Stored Results → Faster Reads → Higher Write Cost
Base Tables
→
Indexed View
→
Precomputed Result
Reads ↓
|
Writes ↑
8learning modules
24interactive practices
5rapid review questions
50%certificate threshold
Learning target
Design indexed views only when the workload can justify their maintenance cost.
Practice progress0 / 24

Complete 12 of 24 practices (50%) and enter your name to unlock the Certificate of Participation.

MODULE 01
⚡

What an Indexed View Really Is

A normal view stores only query definition. An indexed view becomes a physically maintained result set once a unique clustered index is created on the view.

⚡
Precompute now, pay maintenance later

If a dashboard repeatedly groups millions of fact rows by the same dimensions, an indexed view may store that aggregate result. But every insert, update or delete affecting those base rows must also maintain the indexed structure.

Core ideas

  • Normal view = stored query definition
  • Indexed view = physically indexed result
  • Read savings come with write/storage cost
  • Use only when repeated query patterns justify maintenance
Conceptual model
Base tables
   |
   +-- INSERT / UPDATE / DELETE
   |        |
   |        +--> maintain indexed view
   |
   +-- Query indexed view
            |
            +--> precomputed rows / aggregates
✅

An indexed view is a workload tradeoff, not a free cache.

Practice — 3 cases

Practice 1 / Práctica 1
What makes an indexed view different from a normal view?
Practice 2 / Práctica 2
What is the main tradeoff of an indexed view?
Practice 3 / Práctica 3
Which workload shape is a stronger candidate for an indexed view?
MODULE 02
🔒

SCHEMABINDING: Lock the Definition to the Schema

SQL Server requires indexed views to use WITH SCHEMABINDING. The view definition becomes an explicit dependency on the underlying schema.

⚡
The engine must trust that the definition cannot drift underneath the index

If a view is physically indexed, SQL Server cannot allow someone to silently drop or alter a referenced column in a way that invalidates the stored result.

Core ideas

  • Indexed views require WITH SCHEMABINDING
  • Use schema-qualified two-part object names
  • Dependencies become explicit
  • Schema changes must respect the indexed-view dependency
Schema-bound view skeleton
CREATE VIEW dbo.vSalesSummary
WITH SCHEMABINDING
AS
SELECT
    s.CustomerID,
    COUNT_BIG(*) AS RowCount,
    SUM(s.Amount) AS TotalAmount
FROM dbo.Sales AS s
GROUP BY s.CustomerID;
✅

SCHEMABINDING is the foundation that makes physical indexing of the view safe.

Practice — 3 cases

Practice 4 / Práctica 4
What must a view use before it can be indexed?
Practice 5 / Práctica 5
Why does schema binding matter?
Practice 6 / Práctica 6
How must base objects generally be referenced in a schema-bound view?
MODULE 03
🧱

The Unique Clustered Index Creates the Materialized Result

The unique clustered index is the step that transforms the schema-bound view from a logical definition into a physically maintained indexed result.

⚡
No unique clustered index, no indexed view

For a grouped sales view, the grouped key — such as CustomerID — may form the unique clustered index key if each grouped row is unique.

Core ideas

  • First index must be UNIQUE CLUSTERED
  • Key must uniquely identify view rows
  • Additional indexes can be added later
  • Index design should match actual access patterns
Create the materializing index
CREATE UNIQUE CLUSTERED INDEX CIX_vSalesSummary
ON dbo.vSalesSummary(CustomerID);

-- Optional later:
CREATE INDEX IX_vSalesSummary_TotalAmount
ON dbo.vSalesSummary(TotalAmount);
✅

The unique clustered index is not just an optimization; it is what materializes the indexed view.

Practice — 3 cases

Practice 7 / Práctica 7
What index must be created first on an indexed view?
Practice 8 / Práctica 8
Why must the first index be unique?
Practice 9 / Práctica 9
What can you add after the unique clustered index exists?
MODULE 04
📐

Restrictions: Indexed Views Are Not Ordinary Views

SQL Server restricts what can appear in an indexed view because the engine must maintain the stored result incrementally and deterministically.

⚡
Materialization requires predictable math

Many constructs that are perfectly valid in normal views are not eligible for indexed views. Aggregated designs also require patterns such as COUNT_BIG for maintainable row counts.

Core ideas

  • Indexed-view definitions have stricter rules than normal views
  • Expressions must satisfy determinism requirements
  • Aggregates have special requirements such as COUNT_BIG
  • Validate eligibility before redesigning a workload around the feature
Aggregate view pattern
CREATE VIEW dbo.vSalesByCustomer
WITH SCHEMABINDING
AS
SELECT
    CustomerID,
    COUNT_BIG(*) AS RowCount,
    SUM(Amount) AS TotalAmount
FROM dbo.Sales
GROUP BY CustomerID;
✅

The restrictions exist because SQL Server must maintain the stored result correctly on every qualifying base-table change.

Practice — 3 cases

Practice 10 / Práctica 10
Which aggregate is typically required instead of COUNT(*) in an indexed aggregate view?
Practice 11 / Práctica 11
Why are indexed-view definitions restricted?
Practice 12 / Práctica 12
What is a good design approach before coding an indexed view?
MODULE 05
⚙️

SET Options: The Operational Requirement People Forget

Indexed views depend on required SET options. These settings are part of the feature's correctness contract, not a cosmetic preference.

⚡
A correct schema can still fail operationally with the wrong session settings

Deployment scripts, ETL sessions and application connections should be validated so the required settings are consistent when maintaining or using the indexed view.

Core ideas

  • Required SET options are part of deployment design
  • Connection behavior matters, not just DDL scripts
  • Validate settings in representative application sessions
  • Document requirements for future support teams
Deployment mindset
-- Example deployment checklist concept:
-- Verify required SQL Server SET options
-- before CREATE VIEW / CREATE INDEX
-- and in application connection behavior.

-- Treat settings as part of the indexed-view contract,
-- not as an afterthought.
✅

An indexed view is both a schema feature and an operational configuration feature.

Practice — 3 cases

Practice 13 / Práctica 13
Why do SET options matter for indexed views?
Practice 14 / Práctica 14
Which general operational lesson follows from SET-option requirements?
Practice 15 / Práctica 15
What should you do before deploying an indexed view to production?
MODULE 06
✍️

The Hidden Cost: Every Write Maintains the View

Indexed views stay transactionally consistent because SQL Server maintains them when base data changes. That consistency is powerful — and it is exactly why write cost rises.

⚡
Faster dashboard, slower insert? Measure both.

An indexed aggregate might reduce a report from seconds to milliseconds while adding measurable overhead to every insert into the fact table. Whether that trade is good depends on workload priorities.

Core ideas

  • Indexed views are maintained synchronously with base changes
  • More indexed views can amplify write cost
  • Transaction log and CPU usage can increase
  • Evaluate read/write balance, not SELECT speed alone
Before / after test
-- Measure representative workload:
-- 1) Baseline report query
-- 2) Baseline INSERT/UPDATE batch
-- 3) Create indexed view
-- 4) Re-run report query
-- 5) Re-run write batch
-- 6) Compare:
--    logical reads
--    elapsed time
--    CPU
--    log generation
--    index/storage size
✅

Indexed views are justified only when total workload benefit exceeds total maintenance cost.

Practice — 3 cases

Practice 16 / Práctica 16
What happens when a base-table row changes?
Practice 17 / Práctica 17
Why can indexed views hurt write-heavy workloads?
Practice 18 / Práctica 18
What should be measured before approving an indexed view?
MODULE 07
🧭

Indexed View vs Summary Table vs ETL Precompute

Indexed views are one form of precomputation, not the only one. A summary table refreshed every minute, hour or night may offer more flexibility when real-time maintenance is unnecessary.

⚡
Freshness requirements should drive architecture

If the dashboard can tolerate data five minutes old, a controlled ETL summary may avoid adding synchronous cost to every transaction.

Core ideas

  • Indexed view = transactionally current, synchronously maintained
  • Summary table = flexible refresh schedule
  • ETL precompute can support more complex transformations
  • Choose based on freshness, complexity and write tolerance
Architecture comparison
Indexed View
- Current with base transaction
- Strict definition rules
- Write cost on every change

Summary Table / ETL
- Refresh on your schedule
- More transformation freedom
- Can be stale between refreshes
✅

Precomputation is an architecture decision: indexed view when synchronous freshness matters, summary tables when controlled refresh is acceptable.

Practice — 3 cases

Practice 19 / Práctica 19
What is one alternative to an indexed view for precomputed analytics?
Practice 20 / Práctica 20
Why might a summary table be preferable?
Practice 21 / Práctica 21
When is an indexed view comparatively attractive?
MODULE 08
🏭

Production Decision Guide: When an Indexed View Is Worth It

The best indexed-view candidates are stable, repeated and expensive read patterns where freshness must remain transactionally current and write overhead is acceptable.

⚡
Do not materialize complexity; materialize proven repetition

A complex query that runs once a day may not deserve synchronous maintenance all day long. A smaller aggregate hit thousands of times per hour might.

Core ideas

  • Measure baseline before design
  • Confirm feature eligibility and SET requirements
  • Test reads and writes together
  • Revisit the indexed view if workload patterns change
Production checklist
1. Is the read pattern repeated and expensive?
2. Must results stay transactionally current?
3. Is the view definition eligible for indexing?
4. Are required SET options controlled?
5. Is SCHEMABINDING acceptable operationally?
6. Can a unique clustered key identify each view row?
7. What read improvement is expected?
8. What write overhead is introduced?
9. What storage/log impact appears?
10. Would an ETL summary table be simpler?
11. Are support teams aware of schema dependencies?
12. Does measured benefit still justify the feature after deployment?
✅

An indexed view is successful when it improves the total system workload, not merely one SELECT statement.

Practice — 3 cases

Practice 22 / Práctica 22
What is the strongest first question before creating an indexed view?
Practice 23 / Práctica 23
What should be validated after deploying an indexed view?
Practice 24 / Práctica 24
What is the strongest production rule for indexed views?
5-Question Knowledge Check

Can you decide when SQL Server should precompute a view?

Open each item after answering it in your own words. The 24 interactive practices above drive certificate progress.

1. What physically materializes an indexed view?

Creating the required unique clustered index on a schema-bound view.

2. Why is SCHEMABINDING required?

It protects the indexed result from incompatible underlying schema changes.

3. What is the main cost of indexed views?

Writes must maintain the indexed-view structure synchronously.

4. Why might a summary table be better?

It gives you more control over refresh timing and transformation logic.

5. What proves an indexed view is worth keeping?

Measured total-workload benefit proves the design.

Precomputed Results Decision Map

Indexed view or another strategy?

NeedStrong candidate
Transactionally current repeated aggregateIndexed View
Complex transformation with flexible refreshSummary Table / ETL
One-time or rare expensive queryTune query/indexes first
Write-heavy OLTP with little repeated read benefitUsually avoid indexed view
Read-heavy stable repeated patternEvaluate indexed view with measurement

Certificate of Participation

Complete at least 12 of the 24 practice cases (50%) and enter your name.

0 / 24 • 0%
Production note

Indexed views are a precomputation strategy, not a default optimization.