Skip to content

Materialized View

Medium — good to knowDatabase

ELI5 — The Vibe Check

A materialized view is a saved query result that the database keeps on disk like a cheat sheet. Instead of re-running that expensive 10-second report every time someone asks, it just reads the pre-computed answer. You refresh it whenever you want fresh data. It's caching with a fancy SQL hat.

Real Talk

A materialized view stores the result of a query physically on disk, unlike a regular view which re-executes on every access. It dramatically speeds up expensive aggregations and joins at the cost of storage and staleness. PostgreSQL supports REFRESH MATERIALIZED VIEW with CONCURRENTLY for non-blocking updates.

Show Me The Code

CREATE MATERIALIZED VIEW monthly_revenue AS
SELECT date_trunc('month', created_at) AS month,
       SUM(amount) AS total
FROM orders
GROUP BY month;

REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_revenue;

When You'll Hear This

"We refresh the materialized view every hour for the dashboard." / "Materialized views turned our 8-second query into 50ms."

Made with passive-aggressive love by manoga.digital. Powered by Claude.