All posts · SQL & PL/SQL

Oracle Materialized Views: Caching Complex Queries

Materialized views store pre-computed query results, dramatically speeding up expensive aggregations, joins, and reports that run frequently on large datasets.

Anurag Jangra · February 16, 2026 · 6 min read · ... views

What materialized views are

A materialized view is a database object that physically stores the result of a query. Unlike regular views (which execute the query each time), materialized views return pre-computed results instantly. They’re refreshed periodically or on demand to stay in sync with source tables.

Basic creation

CREATE MATERIALIZED VIEW mv_sales_summary
BUILD IMMEDIATE            -- build at creation time (vs DEFERRED)
REFRESH COMPLETE           -- refresh strategy
ON DEMAND                  -- refresh trigger
ENABLE QUERY REWRITE       -- allow optimizer to use this MV for queries on base tables
AS
SELECT
  department_id,
  TRUNC(sale_date, 'MM') AS sale_month,
  SUM(amount) AS total_sales,
  COUNT(*) AS transaction_count
FROM sales
GROUP BY department_id, TRUNC(sale_date, 'MM');

Refresh strategies

COMPLETE: truncates and re-populates from scratch. Safe for any query but can be slow for large MVs.

FAST (incremental): applies only changes since last refresh using materialized view logs. Much faster but requires CREATE MATERIALIZED VIEW LOG on source tables and query restrictions.

FORCE: uses FAST if possible, falls back to COMPLETE.

-- Create log on source table for fast refresh:
CREATE MATERIALIZED VIEW LOG ON sales
WITH ROWID, SEQUENCE (department_id, sale_date, amount)
INCLUDING NEW VALUES;

Refresh scheduling

-- Automatic refresh every night at 2am:
CREATE MATERIALIZED VIEW mv_sales_summary
REFRESH COMPLETE
START WITH TRUNC(SYSDATE) + 26/24  -- 2am today
NEXT TRUNC(SYSDATE+1) + 26/24     -- 2am each subsequent day
AS ...;

Query rewrite: transparent acceleration

When ENABLE QUERY REWRITE is set, Oracle automatically rewrites queries on base tables to use the MV:

-- This query on the base table...
SELECT department_id, SUM(amount) FROM sales GROUP BY department_id;

-- ...is automatically rewritten to:
SELECT department_id, total_sales FROM mv_sales_summary;

Users and applications get fast results without changing their queries.

When to use materialized views

  • Nightly or hourly summary reports run against large OLTP tables
  • Cross-instance joins (remote database links involved)
  • Queries that the optimizer consistently executes badly due to data complexity
  • When you can’t add indexes to source tables (production OLTP systems)

Think Beyond the Implementation

Questions worth sitting with after reading this

01

Why is this architecture appropriate for this specific context — and where would it be the wrong choice?

02

What assumptions did we make that aren't stated explicitly? What happens if those assumptions are wrong?

03

What would break first if the requirements changed — volume doubled, a third system was added, or the deadline halved?

04

What alternatives did we reject, and why? Was the decision made on evidence — or habit?

AJ
Anurag Jangra
Oracle Cloud PaaS Consultant · OIC & VBCS Specialist

4.5+ years delivering enterprise Oracle Cloud integrations and VBCS applications across manufacturing, IT services, and financial sectors. OCI Certified — writes about real-world OIC, VBCS, SQL, and BI Publisher patterns from production experience.

Chat on WhatsApp