CREATE MATERIALIZED VIEW stores the result of a query on disk, letting you query pre-computed data for fast reads.
Materialized views auto-recompute from a base query, so you avoid manual ETL while still getting read performance similar to a table. Use them for dashboards, aggregates, and vector search snapshots in ParadeDB.
Run CREATE MATERIALIZED VIEW
with an AS
query. Add WITH NO DATA
if you want to define it first and populate later with REFRESH
.
IF NOT EXISTS prevents errors on re-run. WITH () sets storage parameters such as fillfactor
. USING method is optional; ParadeDB users can specify a custom storage method.
CREATE MATERIALIZED VIEW mv_customer_sales AS
SELECT c.id, c.name,
SUM(o.total_amount) AS lifetime_value
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id;
This view stores each customer’s lifetime sales so dashboards load instantly.
Data becomes stale as underlying tables change. Use REFRESH MATERIALIZED VIEW
to update it.
REFRESH MATERIALIZED VIEW mv_customer_sales;
This locks the view during the refresh.
REFRESH MATERIALIZED VIEW CONCURRENTLY mv_customer_sales;
CONCURRENTLY
keeps the old data available but requires a unique index on the view.
Choose them when query latency matters more than instant freshness: reporting dashboards, autocomplete lookup lists, AI vector indexes in ParadeDB, or cross-table aggregates.
Store expensive vector search results in a materialized view, then create ParadeDB’s hnsw
index on the view’s embedding column for low-latency similarity lookups.
1. Forgetting to refresh. Schedule cron or pg_cron
jobs.
2. Omitting a unique index. Without it, CONCURRENTLY
fails.
No. You must call REFRESH
manually or via a job scheduler.
Yes. Treat it like a table and create B-tree, GIN, or ParadeDB vector indexes.
Use DROP MATERIALIZED VIEW IF EXISTS mv_customer_sales;
.
No. Inserts are unaffected because views refresh only when you call REFRESH.
No. The underlying query must be fixed at creation time.
Yes. Create the view during deployment and populate later to avoid long locks.