---
title: "Views vs materialized views: when to store the result · CloudSat"
description: "Views vs materialized views in PostgreSQL, MySQL and MariaDB: how each works, refreshing without blocking reads, scheduling refreshes and when to use which."
url: https://cloudsat.io/learn/views-vs-materialized-views-explained
updated: 2026-11-18T12:00:00Z
source: CloudSat (https://cloudsat.io/). Prices in USD.
---

[Databases](https://cloudsat.io/learn/topic/databases)

# Views vs materialized views: when to store the result

Views vs materialized views in PostgreSQL, MySQL and MariaDB: how each works, refreshing without blocking reads, scheduling refreshes and when to use which.

CloudSat team November 18, 2026 6 min read

A view is a saved query. A materialized view is a saved result. The first is always current and does its work on every read; the second is fast to read and only as current as its last refresh. Nearly every decision between them follows from those two sentences. The examples below use PostgreSQL 18, with a section on MySQL and MariaDB, which have views but no materialized views.

All examples query this table:

```sql
CREATE TABLE orders (
  id          bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id bigint NOT NULL,
  status      text NOT NULL,
  total       numeric(12,2) NOT NULL,
  created_at  timestamptz NOT NULL DEFAULT now()
);
```

## A view is a named query

```sql
CREATE VIEW paid_orders AS
SELECT id, customer_id, total, created_at
FROM orders
WHERE status = 'paid';
```

A view stores no rows. When you query it, PostgreSQL substitutes the view's definition into your query and plans the whole thing at once. That means `SELECT * FROM paid_orders WHERE customer_id = 42` can still use an index on `orders (customer_id)`: wrapping a query in a view costs nothing by itself, and it does not make a slow query fast either.

Views earn their place in three ways:

- **Simpler queries.** Joins and filters that everyone repeats live in one definition.
- **A stable interface.** You can rename or split the underlying tables and keep the view's columns unchanged for the applications that read it.
- **Access control.** Grant `SELECT` on the view instead of the table to expose only some columns or rows. By default a view runs with its owner's privileges; `WITH (security_invoker = true)` makes it check the querying user's rights instead, which matters when you use row-level security.

Simple views over one table, without aggregates or `DISTINCT`, are also updatable: `INSERT`, `UPDATE` and `DELETE` pass through to the table. Add `WITH CHECK OPTION` so nobody can insert a row through `paid_orders` that the view itself would hide.

## A materialized view is a stored result

```sql
CREATE MATERIALIZED VIEW daily_revenue AS
SELECT date_trunc('day', created_at)::date AS day,
       count(*)   AS orders,
       sum(total) AS revenue
FROM orders
WHERE status = 'paid'
GROUP BY 1;

CREATE UNIQUE INDEX ON daily_revenue (day);
```

The query runs once, at creation, and the result is written to disk like a table. You can index it, and reads cost the same as reading any small table, however expensive the query behind it was. The price is staleness: new orders do not appear until you refresh. `WITH NO DATA` at the end of the definition creates it empty, and it cannot be queried until the first refresh.

## Side by side

|  | View | Materialized view |
| --- | --- | --- |
| Stores rows | No | Yes, like a table |
| Freshness | Always current | As of the last refresh |
| Cost of a read | Runs the full query | Reads stored rows |
| Its own indexes | No, uses the base tables' | Yes |
| Writable | Simple views, yes | No |
| PostgreSQL | Yes | Yes |
| MySQL and MariaDB | Yes | No, emulate with a table |

## Refresh without blocking readers

A plain refresh recomputes everything and swaps in the new result:

```sql
REFRESH MATERIALIZED VIEW daily_revenue;
```

It is the fastest kind of refresh, but it takes an exclusive lock, so every query against `daily_revenue` waits until it finishes. For a dashboard that people use all day, refresh concurrently instead:

```sql
REFRESH MATERIALIZED VIEW CONCURRENTLY daily_revenue;
```

PostgreSQL then computes the new result on the side, compares it with the stored rows, and applies only the differences, while readers keep seeing the previous version. Three conditions apply. The materialized view needs at least one unique index that uses plain column names and covers all rows, which is why the example creates one on `day`. It must already be populated. And only one refresh at a time can run against a given materialized view. Concurrent refreshes do more work and leave dead rows behind for autovacuum to clean up, so use them where blocking readers is the bigger problem.

## Schedule the refresh

A cron entry is the simplest scheduler. Put it in `/etc/cron.d/refresh-daily-revenue`:

```bash
*/15 * * * * postgres psql -d shop -qc "REFRESH MATERIALIZED VIEW CONCURRENTLY daily_revenue"
```

To keep scheduling inside the database, use the pg\_cron extension, packaged as `postgresql-18-cron` in Ubuntu 26.04 and in the PostgreSQL APT repository. Add `shared_preload_libraries = 'pg_cron'` and `cron.database_name = 'shop'` to the server configuration, restart, then:

```sql
CREATE EXTENSION pg_cron;
SELECT cron.schedule('refresh-daily-revenue', '*/15 * * * *',
  'REFRESH MATERIALIZED VIEW CONCURRENTLY daily_revenue');
```

Pick the interval from how stale the readers can tolerate, not from how often you could refresh. If a refresh takes four minutes, a five-minute schedule leaves the server busy almost all the time.

## When you need fresh and fast

A refresh always recomputes the whole query. When that is too slow and staleness is not acceptable, maintain the result incrementally:

- **pg\_ivm** is a PostgreSQL extension for incrementally maintained materialized views. It updates the stored result as base tables change, for a supported subset of queries, at the cost of extra work on every write.
- **A summary table kept up to date by triggers or by the application** gives full control, and full responsibility for correctness.
- **ClickHouse** treats a materialized view as an insert trigger: each batch of new rows is transformed and written to a target table as it arrives, which suits event analytics.

## MySQL and MariaDB: build your own

Neither has materialized views, but a summary table plus the event scheduler covers most uses. Recomputing only recent days keeps each run cheap:

```sql
CREATE TABLE daily_revenue (
  day     DATE PRIMARY KEY,
  orders  INT NOT NULL,
  revenue DECIMAL(14,2) NOT NULL
);

CREATE EVENT refresh_daily_revenue
ON SCHEDULE EVERY 15 MINUTE
DO
  REPLACE INTO daily_revenue
  SELECT DATE(created_at), COUNT(*), SUM(total)
  FROM orders
  WHERE status = 'paid' AND created_at >= CURDATE() - INTERVAL 2 DAY
  GROUP BY DATE(created_at);
```

The event scheduler is on by default in MySQL 8 and off in MariaDB. On MariaDB, add `event_scheduler = ON` to the `[mysqld]` section of the server configuration so it survives restarts. Unlike a real refresh, this pattern never removes a day that disappears from the source, so handle deletes separately if they happen.

## How to choose

- **Use a view** when the query is cheap enough to run on every read, when results must be current, or when the goal is abstraction or access control.
- **Use a materialized view** when the query is expensive, the result is read far more often than the data changes, and minutes of staleness are acceptable: dashboards, reports, leaderboards.
- **Use incremental maintenance** when you need both speed and freshness and can afford the extra work on writes.

Before any of these, run `EXPLAIN (ANALYZE, BUFFERS)` on the slow query. A missing index on the base table is a cheaper fix than any caching layer.

## FAQ

### Does a PostgreSQL materialized view update itself?

No. Its contents change only when you run `REFRESH MATERIALIZED VIEW`, by hand or on a schedule.

### Can I add an index to a regular view?

Not in PostgreSQL. A view has no rows of its own, so it relies on the indexes of the tables it reads. Materialized views can have their own indexes.

### Why is the concurrent refresh slower?

It builds the new result separately and compares it row by row with the old one before applying the differences. That is more work than replacing everything, but readers are never blocked. If nobody queries the view while it refreshes, the plain refresh is the better choice.

**Run it on CloudSat.** Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. [See plans](https://cloudsat.io/plans) or [deploy a server](https://dash.cloudsat.io/console/deploy).

## Keep reading

[All guides in Databases](https://cloudsat.io/learn/topic/databases)

- [ETL, ELT and CDC: how to choose a data integration approach](https://cloudsat.io/learn/etl-elt-cdc-data-integration-guide): Databases ETL, ELT and CDC: how to choose a data integration approach 7 min read
- [Databricks vs Snowflake: lakehouse or warehouse, in plain terms](https://cloudsat.io/learn/databricks-vs-snowflake-lakehouse-or-warehouse): Databases Databricks vs Snowflake: lakehouse or warehouse, in plain terms 6 min read
- [Database clustering explained: replicas, failover and sharding](https://cloudsat.io/learn/database-clustering-replicas-failover-sharding): Databases Database clustering explained: replicas, failover and sharding 6 min read
- [MongoDB or MariaDB: choosing between documents and tables](https://cloudsat.io/learn/mongodb-or-mariadb-documents-vs-tables): Databases MongoDB or MariaDB: choosing between documents and tables 6 min read
