Materialized view — a database object storing the result of a query as a physical table, unlike a regular view (virtual). Fast on SELECT (just read table), but requires explicit refresh on data changes. Used for: expensive aggregations (SUM, AVG over millions of rows), JOIN of multiple large tables, analytics dashboards. Native support: Postgres, Oracle. MySQL — via triggers or periodic table swap.
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
-- Create materialized view
CREATE MATERIALIZED VIEW orders_daily_summary AS
SELECT
DATE(created_at) AS day,
status,
COUNT(*) AS orders,
SUM(amount) AS revenue
FROM orders
GROUP BY day, status;
-- Index for fast queries
CREATE INDEX ON orders_daily_summary (day);
-- Refresh periodically (cron)
REFRESH MATERIALIZED VIEW CONCURRENTLY orders_daily_summary;A materialized view is fundamentally a stored query result, which allows for improved performance in data retrieval. Unlike a standard view that generates results on-the-fly, a materialized view stores the data physically on disk, making it significantly faster for SELECT operations. This performance gain comes at the cost of requiring manual refreshes to synchronize with the underlying data changes.
When you create a materialized view, the database engine executes the defined query and saves the result set as a physical table. This table can then be queried directly, which reduces the computational overhead involved in complex queries, especially those involving aggregations and multiple joins.
However, the refresh mechanism is critical. Depending on the database system, you can set up manual, scheduled, or incremental refreshes:
Different databases have varying syntax and capabilities for materialized views, which can influence their implementation.
Materialized views are particularly valuable in scenarios where query performance is paramount. Here are some common use cases:
While materialized views improve query speed, it is essential to evaluate the trade-offs, particularly related to data freshness and maintenance overhead.
Creating a materialized view involves defining the query that produces the desired dataset and specifying any refresh options. Below are practical examples for PostgreSQL and Oracle:
PostgreSQL Example:
CREATE MATERIALIZED VIEW sales_summary AS
SELECT product_id, SUM(amount) AS total_sales
FROM sales
GROUP BY product_id;
This command creates a materialized view named sales_summary that aggregates sales data by product_id.
To refresh this view manually, use:
REFRESH MATERIALIZED VIEW sales_summary;
Oracle Example:
CREATE MATERIALIZED VIEW employees_mv
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
AS
SELECT department_id, COUNT(*) AS employee_count
FROM employees
GROUP BY department_id;
In this Oracle example, a materialized view named employees_mv is created to count employees per department, with an immediate build and on-demand refresh.
For MySQL, you can simulate a materialized view using a combination of tables and triggers. Create a table to store the results and set up a trigger to update it upon data changes. While not as straightforward as native support in other databases, this approach can still yield performance benefits.
Regular — virtual, recomputed on every query. Materialized — stored, fast read, stale data.
Balance freshness vs load. For dashboards: 5-15 min. For reports: hourly/daily. Incremental better for high-rate tables.
<code>REFRESH MATERIALIZED VIEW CONCURRENTLY</code> — no lock on readers. Requires UNIQUE index. Recommended for prod.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.