Materialized Views
Materialized Views allow you to pre-compute the results of complex SQL queries and store them in a cache. This can significantly improve the performance of analytical queries on large datasets.
Table of Contents
- Overview
- Creating Materialized Views
- Using Materialized Views
- Management Commands
- Internal Architecture
- Next Steps
Overview
Unlike standard Views, which reference the base tables every time a query is executed, Materialized Views store the results in a physical cache.
Key Features
- Query Performance Boost: Returns the results of complex queries involving JOINs or aggregations instantly.
- Auto-Refresh: Automatically updates the results in the background every set number of seconds.
- Transparent Caching: When you execute a
SELECTquery, the DBX engine automatically uses the cache if a corresponding materialized view exists and is fresh.
Creating Materialized Views
Basic Syntax
CREATE MATERIALIZED VIEW [view_name]
[REFRESH EVERY [seconds]]
AS [select_query]
Example: Sales summary with 60s auto-refresh
use dbx_core::Database;
fn main() -> dbx_core::DbxResult<()> {
let db = Database::open_in_memory()?;
// Create a view that automatically refreshes sales statistics every 60 seconds
db.execute_sql(
"CREATE MATERIALIZED VIEW sales_summary
REFRESH EVERY 60
AS SELECT category, SUM(price) FROM orders GROUP BY category"
)?;
Ok(())
}
Using Materialized Views
Once a materialized view is created, the DBX engine check the cache internally when executing the same SQL query.
// When you execute a regular SELECT statement, DBX returns the cached result if available.
let results = db.execute_sql("SELECT category, SUM(price) FROM orders GROUP BY category")?;
[!NOTE] If
REFRESH EVERYis not specified, the view retains its initial cache until manually refreshed.
Management Commands
Manual Refresh
Use this to synchronize with the latest data immedately.
REFRESH MATERIALIZED VIEW sales_summary
Dropping a View
DROP MATERIALIZED VIEW sales_summary
Internal Architecture
- Registration: The SQL statement and refresh interval are stored in the
MaterializedViewRegistry. - Background Thread: A dedicated thread (created when the
Databaseopens) periodically checksis_fresh()and re-calculates expired views (currently every 60s). - Interception: When
execute_sqlis called, the engine checks if a materialized view matches the query. If a fresh cache exists, it returns it instantly without planning or executing the full query.
Next Steps
- SQL Reference — Check supported SQL syntax
- Streaming Ingestion — Build real-time data pipelines
- Storage Layers — Understand the 5-tier architecture