Materialized View
Materialized View
A view whose results are stored. Periodic refresh, fast queries.
Materialized View
A view whose results are stored. Periodic refresh, fast queries.
A materialized view is a database object that physically stores the result of a query. A regular view runs its query every time it is read, but a materialized view stores precomputed results like a table, so they can be retrieved quickly.
Main use cases: complex aggregations (large-scale reporting), reducing join costs (storing frequently queried join results), data warehouses (caching ETL results), read performance optimization.
Refresh methods: complete refresh (REFRESH COMPLETE - recompute everything), incremental refresh (REFRESH FAST - apply only changes, with restrictions). In PostgreSQL, REFRESH MATERIALIZED VIEW CONCURRENTLY lets you refresh even while the view is being queried.
-- PostgreSQL: 머티리얼라이즈드 뷰 생성
CREATE MATERIALIZED VIEW monthly_sales_summary AS
SELECT
DATE_TRUNC('month', order_date) AS month,
product_id,
SUM(quantity) AS total_quantity,
SUM(amount) AS total_amount,
COUNT(*) AS order_count
FROM orders
GROUP BY DATE_TRUNC('month', order_date), product_id;
-- 고유 인덱스 추가 (CONCURRENTLY 갱신에 필요)
CREATE UNIQUE INDEX idx_monthly_sales ON monthly_sales_summary(month, product_id);
-- 조회 (매우 빠름 - 이미 계산된 결과)
SELECT * FROM monthly_sales_summary
WHERE month >= '2024-01-01';
-- 수동 갱신
REFRESH MATERIALIZED VIEW monthly_sales_summary;
-- 동시 접근 허용 갱신 (인덱스 필수)
REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_sales_summary;
-- 자동 갱신: cron 또는 pg_cron 확장 사용
-- SELECT cron.schedule('refresh-sales', '0 * * * *',
-- 'REFRESH MATERIALIZED VIEW CONCURRENTLY monthly_sales_summary');
-- Oracle: 자동 갱신 옵션
CREATE MATERIALIZED VIEW mv_daily_sales
REFRESH FAST ON COMMIT -- 트랜잭션 커밋 시 자동 갱신
AS SELECT date, SUM(amount) FROM orders GROUP BY date;
Data analyst: "The dashboard takes 30 seconds to load. It aggregates ten million rows every time..."
DBA: "If you pre-aggregate with a materialized view, it drops to milliseconds."
Data analyst: "But we need real-time data."
DBA: "If it refreshes automatically every hour, you have to accept up to an hour of lag. If you truly need real time, let's look at index optimization or a cache layer."
Interviewer: "Explain the difference between a materialized view and a regular view, and their use cases."
Candidate: "A regular view stores only the query definition and runs it when read, while a materialized view physically stores the results. It is used when complex aggregations or joins are queried frequently. The downsides are that it needs storage space and data freshness depends on the refresh interval."
Interviewer: "How do you decide on a refresh strategy?"
Candidate: "It depends on how often the data changes and how much lag is acceptable. If a nightly batch is enough, use COMPLETE; if changes are frequent, consider FAST REFRESH or trigger-based refresh."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Reviewer: "This materialized view is being COMPLETE REFRESHed every minute?"
Developer: "Yes, we need the latest data."
Reviewer: "With 10 million rows, a full recompute every minute puts a heavy load on the DB. Use the CONCURRENTLY option and either lengthen the refresh interval to 5 minutes or consider an incremental approach that applies only changed data."
Developer: "I'll add the CONCURRENTLY option and adjust the interval too."