Lakehouse View and Materialized View Guide
Overview
Views and Materialized Views are important tools for encapsulating complex query logic and simplifying data access. A View is a virtual table that computes results dynamically at query time; a Materialized View pre-computes and stores results, supporting query rewrite acceleration. This guide is organized by business scenario to help you quickly master view creation and management methods.
Quick Navigation
- Create a Regular View -- Encapsulate complex query logic
- Create a Materialized View -- Pre-compute results to accelerate queries
- Refresh a Materialized View -- Manually update pre-computed data
- Query Rewrite Verification -- Confirm queries automatically use materialized views
- Drop Views -- Clean up views no longer needed
SQL Commands Covered
| Command | Purpose | Use Case |
|---|---|---|
CREATE VIEW | Create a logical view | Encapsulate JOIN/aggregation to simplify queries |
CREATE MATERIALIZED VIEW | Create a materialized view | Pre-compute high-frequency query results |
REFRESH MATERIALIZED VIEW | Refresh a materialized view | Update pre-computed data |
DROP VIEW / DROP MATERIALIZED VIEW | Drop a view | Clean up abandoned views |
Prerequisites
The following examples use a simulated sales detail table sales_detail:
Create a Regular View
Use CREATE VIEW to define a logical view. Views do not store data; they dynamically execute the underlying SQL at query time.
Using the View:
Result Explanation:
| region | sale_count | total_amount |
|---|---|---|
| East | 2 | 10000 |
| West | 1 | 3000 |
Create a Materialized View
Use CREATE MATERIALIZED VIEW to create a pre-computed view. Materialized views store actual data and can be automatically rewritten to by the optimizer at query time.
Query the Materialized View:
Result Explanation:
| sale_date | daily_total |
|---|---|
| 2024-06-01 | 8000 |
| 2024-06-02 | 5000 |
Refresh a Materialized View
When source table data changes, use REFRESH MATERIALIZED VIEW to update the materialized view.
Result Explanation:
| sale_date | daily_total |
|---|---|
| 2024-06-03 | 4000 |
Query Rewrite Verification
The Singdata Lakehouse optimizer automatically rewrites queries on source tables to use materialized views, thereby accelerating response times.
Drop Views
Use DROP VIEW or DROP MATERIALIZED VIEW to drop views.
Clean Up Test Data
After completing view verification, it is recommended to clean up the test table:
Notes
- View Dependencies: Dropping a source table will invalidate views that depend on it. Use
DESC VIEWto check dependency relationships. - Materialized View Refresh: Materialized views are not auto-refreshed by default. For near-real-time data scenarios, consider using Dynamic Tables.
- Query Rewrite Conditions: The optimizer only rewrites queries when the materialized view's aggregation dimensions exactly match the query.
- Storage Cost: Materialized views consume additional storage space, proportional to the pre-computed result set size.
- DROP Syntax: Dropping a materialized view must use
DROP MATERIALIZED VIEW, notDROP VIEW.
