Materialized Views

SQL · Extra Concepts

Theory

A regular view is just a saved query: every time you select from it, the database runs the underlying query again. A materialized view runs the query once and stores the result physically, like a table. Reading it is fast because the expensive joins and aggregations are already done, but the stored result is only as fresh as its last refresh.

In PostgreSQL you create one with CREATE MATERIALIZED VIEW and update it with REFRESH MATERIALIZED VIEW, which recomputes the whole result. Oracle supports scheduled and incremental refresh, SQL Server offers a similar idea called indexed views, and Snowflake maintains materialized views automatically in the background. MySQL and SQLite have no materialized views; the usual workaround is a summary table rebuilt by a scheduled job. Materialized views fit dashboards that read the same heavy aggregate many times a day while the source changes slowly.

Why we use it

Dashboards often run the same expensive aggregation over large tables again and again. A materialized view computes it once and serves the stored result, saving time and compute on every read.

When to use it

Use materialized views for heavy, frequently read summaries where slightly stale data is acceptable. Use a regular view when results must always be current or the query is cheap. Use a scheduled summary table in databases without them.

How it works

  1. CREATE MATERIALIZED VIEW runs the query once
  2. Result is stored on disk like a table
  3. Readers query the stored result quickly
  4. Source changed and freshness matters? Yes: REFRESH the view. No: Keep serving the stored result.
  5. Fast reads, freshness controlled by refresh

Syntax

CREATE MATERIALIZED VIEW view_name AS
SELECT ...;

Example

CREATE MATERIALIZED VIEW dept_summary AS
SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic