How to create a materialized view in Oracle

Galaxy Glossary

How do I create and refresh a materialized view in Oracle?

CREATE MATERIALIZED VIEW stores the result of a query as a physical table that Oracle can refresh automatically or on demand to speed up complex look-ups.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What is a materialized view in Oracle?

A materialized view is a precomputed, queryable snapshot of data that lives as a real table on disk. It answers repeated, costly queries with sub-second speed by holding the results in advance.

When should I use a materialized view?

Use one when dashboards, reports, or APIs repeatedly join large tables like Orders and OrderItems. Pre-aggregation lowers CPU load and query latency without rewriting application SQL.

How do I create a materialized view?

Quick syntax cheat-sheet

Combine CREATE MATERIALIZED VIEW with a defining query, choose a refresh mode (ON COMMIT or ON DEMAND), and decide on build timing (IMMEDIATE or DEFERRED).

Which refresh methods are available?

FAST refresh uses materialized view logs to apply only row changes. COMPLETE refresh rebuilds the entire snapshot. FORCE lets Oracle pick FAST if possible, else COMPLETE.

How do I refresh manually?

Call DBMS_MVIEW.REFRESH('mv_name','C') for COMPLETE or 'F' for FAST. Schedule with DBMS_SCHEDULER for hourly or daily jobs.

Practical example: daily revenue per customer

The sample below pre-aggregates yesterday’s sales so customer dashboards load instantly.

CREATE MATERIALIZED VIEW mv_daily_customer_revenue
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS
SELECT o.customer_id,
TRUNC(o.order_date) AS order_day,
SUM(o.total_amount) AS daily_revenue
FROM Orders o
GROUP BY o.customer_id, TRUNC(o.order_date);

Best practices

Index the materialized view’s join and filter columns. Store it in the same tablespace as source data for I/O locality. Monitor LAST_REFRESH_DATE in DBA_MVIEWS.

Common mistakes and fixes

Forgetting materialized view logs prevents FAST refresh—add logs with CREATE MATERIALIZED VIEW LOG ON Orders. Creating unnecessary COMPLETE refresh jobs wastes resources—switch to ON COMMIT or FAST where possible.

Why How to create a materialized view in Oracle is important

How to create a materialized view in Oracle Example Usage


-- Fast-refreshable daily order totals by customer
CREATE MATERIALIZED VIEW mv_customer_daily_totals
BUILD IMMEDIATE
REFRESH FAST ON COMMIT
AS
SELECT c.id                AS customer_id,
       TRUNC(o.order_date) AS order_day,
       COUNT(*)            AS orders_placed,
       SUM(o.total_amount) AS revenue
FROM   Customers c
JOIN   Orders    o ON o.customer_id = c.id
GROUP  BY c.id, TRUNC(o.order_date);

How to create a materialized view in Oracle Syntax


CREATE MATERIALIZED VIEW [schema.]view_name
  [BUILD {IMMEDIATE | DEFERRED}]
  REFRESH {FAST | COMPLETE | FORCE}
    [ON {COMMIT | DEMAND}]
  [START WITH start_time] [NEXT interval]
AS
SELECT ...
-- Ecommerce example
CREATE MATERIALIZED VIEW mv_top_products
BUILD IMMEDIATE
REFRESH COMPLETE ON DEMAND
AS
SELECT p.id, p.name, SUM(oi.quantity) AS units_sold
FROM   Products p
JOIN   OrderItems oi ON oi.product_id = p.id
GROUP  BY p.id, p.name;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I index a materialized view?

Yes. After creation, add B-tree or bitmap indexes to columns used in WHERE or JOIN clauses to maximize read speed.

Does DROP MATERIALIZED VIEW remove its logs?

No. You must explicitly DROP MATERIALIZED VIEW LOG ON table_name or the logs remain and consume space.

How do I check the last refresh time?

Query SELECT last_refresh_date FROM dba_mviews WHERE mview_name = 'MV_NAME'; to see the timestamp.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo