How to Create a Materialized View in MySQL

Galaxy Glossary

How can I create and refresh a materialized view in MySQL?

A materialized view stores the result of a query physically, allowing fast reads at the cost of manual or scheduled refreshes.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why use a materialized view instead of a regular view?

Materialized views trade storage for speed. Because the query result is persisted to disk, SELECTs run instantly even on complex aggregations. Regular views re-execute on every call, slowing down dashboards and APIs.

Does MySQL support materialized views natively?

MySQL 8.0 lacks a dedicated CREATE MATERIALIZED VIEW statement. You emulate one with a physical table plus routines or events that refresh the data.

How do I create a materialized view step-by-step?

1.Build the backing table

Create a table that mirrors the desired columns of your query result.

2. Populate it initially

INSERT INTO ... SELECT executes the source query once and fills the table.

3. Automate refresh logic

Use a scheduled EVENT, a trigger on base tables, or a manual CALL to keep data in sync.

What syntax should I follow?

Use the template in the next block; swap column names and refresh strategy as needed.

When should I refresh?

Dashboards needing near-real-time data can use per-minute events.Nightly reporting can refresh once a day. Always weigh accuracy versus load.

Best practices for performance

  • Index foreign keys in the backing table
  • REFRESH only changed rows when possible
  • Keep one source of truth—don’t let users edit the materialized table directly

Common mistakes to avoid

  • Forgetting to rebuild indexes after TRUNCATE + INSERT refresh routines
  • Refreshing too frequently on massive data sets

.

Why How to Create a Materialized View in MySQL is important

How to Create a Materialized View in MySQL Example Usage


-- Query BI dashboard instantly
SELECT c.name, m.total_spent, m.last_order
FROM mv_customer_spend m
JOIN Customers c ON c.id = m.customer_id
ORDER BY m.total_spent DESC
LIMIT 10;

How to Create a Materialized View in MySQL Syntax


-- 1. Create backing table
CREATE TABLE mv_customer_spend (
    customer_id INT PRIMARY KEY,
    total_spent DECIMAL(10,2),
    last_order DATE
);

-- 2. Populate once
INSERT INTO mv_customer_spend (customer_id, total_spent, last_order)
SELECT c.id,
       SUM(o.total_amount) AS total_spent,
       MAX(o.order_date) AS last_order
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id;

-- 3. Event-based nightly refresh (overwrite strategy)
CREATE EVENT ev_refresh_mv_customer_spend
ON SCHEDULE EVERY 1 DAY
DO
BEGIN
    TRUNCATE TABLE mv_customer_spend;
    INSERT INTO mv_customer_spend
    SELECT c.id,
           SUM(o.total_amount),
           MAX(o.order_date)
    FROM Customers c
    JOIN Orders o ON o.customer_id = c.id
    GROUP BY c.id;
END;

Common Mistakes

Frequently Asked Questions (FAQs)

Can I make a materialized view update automatically on every insert?

Yes. Add AFTER INSERT/UPDATE/DELETE triggers on base tables to INSERT or UPDATE the materialized table accordingly. This offers real-time accuracy but increases write overhead.

How do I force a manual refresh?

Call a stored procedure or run TRUNCATE TABLE + INSERT SELECT manually when you need an immediate update outside the regular schedule.

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
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.