As a database grows, so do the queries that feed reports, dashboards and applications. Before long, the same long chain of JOINs and filters appears in a dozen places, and every copy has to be updated whenever the logic changes. SQL offers a clean answer to this problem: views. And when those views become too slow, many databases offer a second tool, the materialized view.
This guide explains what each one is, how they behave differently, and how to decide which one fits your situation.
What is a view?
A view is a saved query that behaves like a virtual table. It has a name and columns, and you can select from it just like a normal table, but it does not store any data of its own. Every time you query a view, the database runs the underlying query against the base tables and returns the current result.
CREATE VIEW active_customers AS
SELECT c.id, c.name, c.email, COUNT(o.id) AS total_orders
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE c.status = 'active'
GROUP BY c.id, c.name, c.email;
SELECT * FROM active_customers WHERE total_orders > 10;
Whoever queries active_customers doesn’t need to know about the JOIN or the GROUP BY. They simply see a tidy table with the information they need.
Why use regular views
- Simplicity: complex logic is written once and reused everywhere, which makes queries shorter and easier to read.
- Consistency: if a business rule changes, such as the definition of an “active” customer, you update the view and every report follows.
- Security: you can grant users access to a view instead of the base tables, exposing only certain columns or rows. A support team might see customer names and emails, but not payment details.
- Abstraction: the structure of the underlying tables can change while the view keeps presenting the same columns to applications.
The trade-off is performance. Because a view is recalculated on every query, a view built on large tables with heavy aggregations will be just as slow as running the full query by hand. A view organizes your SQL; it does not make it faster by itself.
What is a materialized view?
A materialized view also starts from a query, but instead of running it every time, the database executes it once and stores the result physically, like a table. Reading from it is fast because the work has already been done.
-- PostgreSQL syntax
CREATE MATERIALIZED VIEW monthly_sales AS
SELECT date_trunc('month', order_date) AS month,
SUM(total) AS revenue
FROM orders
GROUP BY 1;
-- Later, to bring the stored data up to date:
REFRESH MATERIALIZED VIEW monthly_sales;
The catch is freshness. Since the result is a snapshot, new orders added after the last refresh won’t appear until the materialized view is refreshed again. You are trading real-time accuracy for speed.
Side-by-side comparison
| Aspect | View | Materialized view |
|---|---|---|
| Stores data? | No, only the query definition | Yes, stores the query result |
| Data freshness | Always current | As fresh as the last refresh |
| Read speed | Same as running the query | Fast, similar to reading a table |
| Disk usage | Minimal | Uses storage like a table |
| Can be indexed? | Generally not directly | Yes, in most databases |
| Maintenance | None beyond the definition | Needs a refresh strategy |
Refresh strategies
Deciding when to refresh is the heart of working with materialized views. Common approaches include:
- Scheduled refresh: a job refreshes the view at fixed intervals, for example every night or every hour. This works well for reports where yesterday’s numbers are good enough.
- On-demand refresh: the application triggers a refresh after a batch load or import finishes.
- Incremental or automatic refresh: some databases, such as Oracle with its “fast refresh” option, can apply only the changes since the last refresh instead of recalculating everything. Support for this varies a lot between systems.
In PostgreSQL, a plain REFRESH locks the materialized view against reads while it runs. The REFRESH MATERIALIZED VIEW CONCURRENTLY option avoids that, but it requires a unique index on the view.
Database support at a glance
Regular views are part of standard SQL and are available in practically every relational database. Materialized views are less uniform. PostgreSQL and Oracle support them directly with CREATE MATERIALIZED VIEW. SQL Server offers a similar idea called indexed views, which are kept up to date automatically but come with strict rules about what the query can contain. MySQL has no native materialized views, so developers often emulate them with a summary table filled by scheduled jobs or triggers.
How to choose
Ask yourself two questions: how expensive is the query, and how current must the data be?
- Use a regular view when the query is reasonably fast, when users need up-to-the-second data, or when your main goal is to simplify queries and control access.
- Use a materialized view when the query is heavy (large aggregations, many JOINs), runs often, and slightly stale data is acceptable, as in dashboards, analytics and summary reports.
- If neither fits, consider other tools first: proper indexes on the base tables, query tuning, or a caching layer in the application.
Common mistakes to avoid
- Stacking views on top of views on top of views. It hides complexity and can make performance problems very hard to diagnose.
- Forgetting to refresh a materialized view, so a dashboard silently shows old numbers.
- Using
SELECT *in view definitions, which can behave unexpectedly when columns are added to the base tables.
Conclusion
A view is a named query that always reflects the current data and helps you organize and secure your database. A materialized view stores the result of a query so it can be read quickly, at the cost of storage and the need to refresh it. Knowing when to use each one is a practical skill that pays off in cleaner code and faster reports.
If you’d like to strengthen your SQL foundations, take a look at the free database courses available on Cursa, where you can practice queries, modeling and optimization step by step.