Synchronous Materialized View
A synchronous materialized view stores the precomputed results of a single-table SELECT statement in a VeloDB Cloud warehouse. The query engine maintains the view with writes to the base table. The optimizer can select an eligible view to accelerate queries on that table.
Before you begin
Connect to your warehouse and select a database. To create a synchronous materialized view, your warehouse SQL user needs ALTER_PRIV on the base table, in addition to the privileges required to read it. The examples also create a database, create tables, and insert data, so use an account with the corresponding privileges.
Before creating a view, check these requirements:
- Use a single native table as the source. The defining
SELECTcannot containJOIN,HAVING,LIMIT, orLATERAL VIEW. - The
SELECTlist must not contain auto-increment columns, constants, duplicate expressions, window functions, orVARBINARYtype columns. - Aggregate functions in the
SELECTlist must be the root expression (sum(a + 1)is supported,sum(a) + 1is not). - Materialized view column names must not conflict with base table columns or columns of other views (you can use a
col as xxxalias to avoid this). - Evaluate the extra compute, storage, and write overhead before adding views to a table.
- On a Unique Key table, the view must retain all base-table key columns and cannot perform aggregation.
How synchronous maintenance works
A synchronous materialized view is stored as an index of its base table. After its initial build completes, data modifications maintain the base table and its views together, so queries use a consistent version of the data. You do not schedule separate refresh jobs. At query time, the optimizer chooses between the base index and eligible materialized views based on the query and estimated cost.
Creating the view still runs as an asynchronous background job. Wait for its state to become FINISHED before checking whether queries use it.
Applicable scenarios
| Scenario | Description |
|---|---|
| Accelerate aggregation | Precompute time-consuming aggregations such as SUM(), COUNT(), and BITMAP_UNION() |
| Match different prefix indexes | When the query filter columns do not match the base table's prefix index, build a view prefixed by the filter columns |
| Prefilter to reduce scans | Use a WHERE condition to filter early and shrink the data volume |
| Precompute complex expressions | Precompute complex expressions such as abs(k1)+k2+1 and reuse them directly at query time |
Limitations
| Category | Limitation |
|---|---|
| Syntax scope | Only single-table SELECT is supported, with WHERE/GROUP BY/ORDER BY; JOIN, HAVING, LIMIT, and LATERAL VIEW are not supported |
| Query method | Query the base table and let the optimizer choose the view. The view is an index, rather than a separately named table |
SELECT list | Cannot contain auto-increment columns, constants, duplicate expressions, or window functions; cannot contain VARBINARY type columns |
| Column name requirements | Must not duplicate names in the base table or other materialized views on the base table; use an alias (col as xxx) to avoid conflicts |
| Aggregate functions | Must be the root expression (sum(a) + 1 is not supported, sum(a + 1) is supported); no other non-aggregate expression is allowed after an aggregate function (SELECT x, sum(a) is allowed, SELECT sum(a), x is not) |
| Predicate deletes | For deletes that use delete predicates, each condition column must be present in every materialized index. If a required column is missing, drop the affected view before deleting. See Delete data for model-specific restrictions |
| Load performance | Additional materialized views can slow loads because every write also maintains the views |
| Data model | A view on a Unique Key table must retain all base-table key columns and cannot perform aggregation. On Unique Key and Aggregate Key tables, a view's WHERE clause can reference only key columns |
Use a synchronous materialized view
Use SQL to create, inspect, and drop synchronous materialized views. The following example precomputes sales aggregation by store. Creation and maintenance consume compute and storage, so drop the example views and tables when you finish.
Prepare base table data
Suppose a user has a sales detail table that records the transaction ID, salesperson, store, sale time, and amount of each transaction.
-- Create a test_db
create database test_db;
use test_db;
-- Create the table
create table sales_records
(
record_id int,
seller_id int,
store_id int,
sale_date date,
sale_amt bigint
)
distributed by hash(record_id);
-- Insert data
insert into sales_records values(1,1,1,"2020-02-02",1), (1,1,1,"2020-02-02",2);
Create a materialized view
Goal: Create a pre-aggregated view for queries that frequently analyze sales by store.
Command:
create materialized view store_amt as
select store_id as store_id_, sum(sale_amt) from sales_records group by store_id;
Description: This view groups by store_id and sums sale_amt for each store, accelerating aggregation queries on the store dimension.
Check the creation status
Goal: Creating a materialized view is asynchronous, so you need to confirm the task status.
Command:
show alter table materialized view from test_db;
Description: The result shows all materialized view creation tasks for this database. Example output:
+--------+---------------+---------------------+---------------------+---------------+-----------------+----------+---------------+----------+------+----------+---------+
| JobId | TableName | CreateTime | FinishTime | BaseIndexName | RollupIndexName | RollupId | TransactionId | State | Msg | Progress | Timeout |
+--------+---------------+---------------------+---------------------+---------------+-----------------+----------+---------------+----------+------+----------+---------+
| 494349 | sales_records | 2020-07-30 20:04:56 | 2020-07-30 20:04:57 | sales_records | store_amt | 494350 | 133107 | FINISHED | | NULL | 2592000 |
+--------+---------------+---------------------+---------------------+---------------+-----------------+----------+---------------+----------+------+----------+---------+
Key field descriptions:
| Field | Meaning |
|---|---|
TableName | The source table of the materialized view |
RollupIndexName | The name of the materialized view |
State | Task state. FINISHED means the view was created successfully and can be matched automatically by queries |
Cancel a creation job
Goal: Cancel the creation task while the background asynchronous task is still running.
Command:
cancel alter table materialized view from test_db.sales_records;
Description: If the materialized view has already been created, this command cannot cancel the creation, but you can drop the materialized view with the drop command.
Inspect the view schema
Goal: View all materialized views on the target table and their schemas.
Command:
desc sales_records all;
Description: Example output follows. Object IDs, timestamps, column labels, and plan details can differ in your warehouse:
+---------------+---------------+---------------------+--------+--------------+------+-------+---------+-------+---------+------------+-------------+
| IndexName | IndexKeysType | Field | Type | InternalType | Null | Key | Default | Extra | Visible | DefineExpr | WhereClause |
+---------------+---------------+---------------------+--------+--------------+------+-------+---------+-------+---------+------------+-------------+
| sales_records | DUP_KEYS | record_id | INT | INT | Yes | true | NULL | | true | | |
| | | seller_id | INT | INT | Yes | true | NULL | | true | | |
| | | store_id | INT | INT | Yes | true | NULL | | true | | |
| | | sale_date | DATE | DATEV2 | Yes | false | NULL | NONE | true | | |
| | | sale_amt | BIGINT | BIGINT | Yes | false | NULL | NONE | true | | |
| | | | | | | | | | | | |
| store_amt | AGG_KEYS | store_id_ | INT | INT | Yes | true | NULL | | true | `store_id` | |
| | | mva_SUM__`sale_amt` | BIGINT | BIGINT | Yes | false | NULL | SUM | true | `sale_amt` | |
+---------------+---------------+---------------------+--------+--------------+------+-------+---------+-------+---------+------------+-------------+
You can see that sales_records has a materialized view named store_amt, which is the view created in the previous step.
Show the creation statement
Goal: Query the original DDL of a materialized view.
Command:
show create materialized view store_amt on sales_records;
Description: Example output follows. Object IDs, timestamps, column labels, and plan details can differ in your warehouse:
+---------------+-----------+------------------------------------------------------------------------------------------------------------------------+
| TableName | ViewName | CreateStmt |
+---------------+-----------+------------------------------------------------------------------------------------------------------------------------+
| sales_records | store_amt | create materialized view store_amt as select store_id as store_id_, sum(sale_amt) from sales_records group by store_id |
+---------------+-----------+------------------------------------------------------------------------------------------------------------------------+
Verify query rewriting
Goal: Queries still target the base table, and the optimizer can rewrite them to use an eligible materialized view.
Command:
select store_id, sum(sale_amt) from sales_records group by store_id;
The query above is automatically matched to store_amt. You can use the EXPLAIN command to verify whether the current query hits the materialized view:
explain select store_id, sum(sale_amt) from sales_records group by store_id;
Description: The result is as follows:
+------------------------------------------------------------------------+
| Explain String (Nereids Planner) |
+------------------------------------------------------------------------+
| PLAN FRAGMENT 0 |
| OUTPUT EXPRS: |
| store_id[#11] |
| sum(sale_amt)[#12] |
| PARTITION: HASH_PARTITIONED: store_id_[#7] |
| |
| HAS_COLO_PLAN_NODE: true |
| |
| VRESULT SINK |
| MYSQL_PROTOCAL |
| |
| 3:VAGGREGATE (merge finalize)(384) |
| | output: sum(partial_sum(__sum_1)[#8])[#10] |
| | group by: store_id_[#7] |
| | sortByGroupKey: false |
| | cardinality = 1 |
| | final projections: store_id_[#9], sum(__sum_1)[#10] |
| | final project output tuple id: 4 |
| | distribute expr lists: store_id_[#7] |
| | |
| 2:VEXCHANGE |
| offset: 0 |
| distribute expr lists: |
| |
| PLAN FRAGMENT 1 |
| |
| PARTITION: HASH_PARTITIONED: record_id[#2] |
| |
| HAS_COLO_PLAN_NODE: false |
| |
| STREAM DATA SINK |
| EXCHANGE ID: 02 |
| HASH_PARTITIONED: store_id_[#7] |
| |
| 1:VAGGREGATE (update serialize)(374) |
| | STREAMING |
| | output: partial_sum(__sum_1[#1])[#8] |
| | group by: store_id_[#0] |
| | sortByGroupKey: false |
| | cardinality = 1 |
| | distribute expr lists: |
| | |
| 0:VOlapScanNode(369) |
| TABLE: test_db.sales_records(store_amt), PREAGGREGATION: ON |
| partitions = 1/1 (sales_records) |
| tablets = 10/10, tabletList = 266568, 266570, 266572 ... |
| cardinality = 1, avgRowSize = 1805.0, numNodes = 1 |
| pushAggOp = NONE |
| |
| |
| ========== MATERIALIZATIONS ========== |
| |
| MaterializedView |
| MaterializedViewRewriteSuccessAndChose: |
| internal.test_db.sales_records.store_amt chose, |
| |
| MaterializedViewRewriteSuccessButNotChose: |
| not chose: none, |
| |
| MaterializedViewRewriteFail: |
| |
| |
| ========== STATISTICS ========== |
| planned with unknown column statistics |
+------------------------------------------------------------------------+
MaterializedViewRewriteSuccessAndChose shows the materialized views that were successfully hit. A specific example:
+------------------------------------------------------------------------+
| MaterializedViewRewriteSuccessAndChose: |
| internal.test_db.sales_records.store_amt chose, |
+------------------------------------------------------------------------+
The output above indicates that the query successfully hit the materialized view named store_amt. Note that if the target table contains no data, the materialized view may not be hit.
MATERIALIZATIONS fields
| Field | Meaning |
|---|---|
| MaterializedViewRewriteSuccessAndChose | The materialized view that was successfully selected and used for query optimization |
| MaterializedViewRewriteSuccessButNotChose | The materialized view that matched successfully but was not selected (not optimal based on cost evaluation) |
| MaterializedViewRewriteFail | A materialized view that did not match. The original SQL could not be matched against the existing view |
Drop a materialized view
Goal: Remove a materialized view that is no longer needed.
Command:
drop materialized view store_amt on sales_records;
Examples
Example 1: Accelerate aggregation queries
Precompute exact distinct visitor counts with bitmaps
Business scenario: Compute the UV (unique visitors) of an advertisement.
Steps:
-
Create the source table that stores ad click details:
create table advertiser_view_record(click_time datetime,advertiser varchar(10),channel varchar(10),user_id int) distributed by hash(user_id);insert into advertiser_view_record values("2020-02-02 02:02:02",'a','a',1), ("2020-02-02 02:02:02",'a','a',2); -
The user wants to query the UV of an advertisement (an exact deduplication on users for the same ad). The typical query is:
selectadvertiser,channel,count(distinct user_id)fromadvertiser_view_recordgroup byadvertiser, channel; -
For the UV scenario, create a materialized view with
bitmap_unionto perform exact deduplication in advance. For nonnegative integer user IDs accepted byTO_BITMAP, bitmap aggregation can provide exact distinct counts. Ensure that incoming IDs stay in this range before creating the view:create materialized view advertiser_uv asselectadvertiser as advertiser_,channel as channel_,bitmap_union(to_bitmap(user_id))fromadvertiser_view_recordgroup byadvertiser, channel; -
After the materialized view is created, run the original UV query again. The optimizer can select
advertiser_uv. UseEXPLAINto verify its choice:selectadvertiser,channel,count(distinct user_id)fromadvertiser_view_recordgroup byadvertiser, channel; -
Once the materialized view is selected, the query reads the stored bitmaps and combines them with
BITMAP_UNION_COUNT. The following illustrates this logical operation. Continue to issue queries againstadvertiser_view_record, becauseadvertiser_uvis an index of that table:selectadvertiser,channel,bitmap_union_count(to_bitmap(user_id))fromadvertiser_uvgroup byadvertiser, channel; -
Use the
explaincommand to check whether the query matched the materialized view:explain selectadvertiser,channel,count(distinct user_id)fromadvertiser_view_recordgroup byadvertiser, channel; -
Example output follows. Plan details can differ in your warehouse:
+---------------------------------------------------------------------------------------------------------------------------------------------------------+| Explain String(Nereids Planner) |+---------------------------------------------------------------------------------------------------------------------------------------------------------+| PLAN FRAGMENT 0 || OUTPUT EXPRS: || advertiser[#13] || channel[#14] || count(DISTINCT user_id)[#15] || PARTITION: HASH_PARTITIONED: advertiser_[#7], channel_[#8] || || HAS_COLO_PLAN_NODE: true || || VRESULT SINK || MYSQL_PROTOCAL || || 3:VAGGREGATE (merge finalize)(440) || | output: bitmap_union_count(partial_bitmap_union_count(__bitmap_union_2)[#9])[#12] || | group by: advertiser_[#7], channel_[#8] || | sortByGroupKey:false || | cardinality=1 || | final projections: advertiser_[#10], channel_[#11], bitmap_union_count(__bitmap_union_2)[#12] || | final project output tuple id: 4 || | distribute expr lists: advertiser_[#7], channel_[#8] || | || 2:VEXCHANGE || offset: 0 || distribute expr lists: || || PLAN FRAGMENT 1 || || PARTITION: HASH_PARTITIONED: user_id[#6] || || HAS_COLO_PLAN_NODE: false || || STREAM DATA SINK || EXCHANGE ID: 02 || HASH_PARTITIONED: advertiser_[#7], channel_[#8] || || 1:VAGGREGATE (update serialize)(430) || | STREAMING || | output: partial_bitmap_union_count(__bitmap_union_2[#2])[#9] || | group by: advertiser_[#0], channel_[#1] || | sortByGroupKey:false || | cardinality=1 || | distribute expr lists: || | || 0:VOlapScanNode(425) || TABLE: test_db.advertiser_view_record(advertiser_uv), PREAGGREGATION: ON || partitions=1/1 (advertiser_view_record) || tablets=10/10, tabletList=266637,266639,266641 ... || cardinality=1, avgRowSize=0.0, numNodes=1 || pushAggOp=NONE || || || ========== MATERIALIZATIONS ========== || || MaterializedView || MaterializedViewRewriteSuccessAndChose: || internal.test_db.advertiser_view_record.advertiser_uv chose, || || MaterializedViewRewriteSuccessButNotChose: || not chose: none, || || MaterializedViewRewriteFail: || || || ========== STATISTICS ========== || planed with unknown column statistics |+---------------------------------------------------------------------------------------------------------------------------------------------------------+ -
In the
explainoutput, you can seeinternal.test_db.advertiser_view_record.advertiser_uv chose, which means the query directly scans the data of the materialized view and the match succeeded. At the same time,count(distinct)on theuser_idcolumn is rewritten asbitmap_union_count(to_bitmap), which performs exact deduplication via Bitmap.
Example 2: Match a different prefix index
Reorder columns to accelerate a filter on k3
Business scenario: Match a prefix index.
The source table has four columns (k1, k2, k3, and kx) and a sort key of (k1, k2). Its prefix index begins with those key columns. When the query condition contains where k1=1 and k2=2, the index can accelerate it. However, conditions such as where k3=3 cannot hit the prefix index. To address this, you can create a materialized view whose first column is k3.
Steps:
-
Create the table and insert data:
create table test_table(k1 int,k2 int,k3 int,kx int)duplicate key (k1, k2)distributed by hash(k1);insert into test_table values(1,1,1,1),(3,3,3,3); -
Create a materialized view that uses k3 as the prefix index:
create materialized view mv_1 as SELECT k3 as k3_, k2 as k2_, k1 as k1_ FROM test_table; -
Use
EXPLAINto check whether the query matches the materialized view:explain select k1, k2, k3 from test_table where k3=3; -
Example output follows. Plan details can differ in your warehouse:
+----------------------------------------------------------+| Explain String(Nereids Planner) |+----------------------------------------------------------+| PLAN FRAGMENT 0 || OUTPUT EXPRS: || k1[#7] || k2[#8] || k3[#9] || PARTITION: HASH_PARTITIONED: k1_[#2] || || HAS_COLO_PLAN_NODE: false || || VRESULT SINK || MYSQL_PROTOCAL || || 0:VOlapScanNode(256) || TABLE: test_db.test_table(mv_1), PREAGGREGATION: ON || PREDICATES: (mv_k3[#0] = 3) || partitions=1/1 (test_table) || tablets=10/10, tabletList=271177,271179,271181 ... || cardinality=1, avgRowSize=0.0, numNodes=1 || pushAggOp=NONE || final projections: k1_[#2], mv_k2[#1], mv_k3[#0] || final project output tuple id: 2 || || || ========== MATERIALIZATIONS ========== || || MaterializedView || MaterializedViewRewriteSuccessAndChose: || internal.test_db.test_table.mv_1 chose, || || MaterializedViewRewriteSuccessButNotChose: || not chose: none, || || MaterializedViewRewriteFail: || || || ========== STATISTICS ========== || planed with unknown column statistics |+----------------------------------------------------------+ -
In the
EXPLAINoutput, you can seeinternal.test_db.test_table.mv_1 chose, which means the query successfully hit the materialized view.
Example 3: Accelerate queries with prefiltering and expression computation
Precompute expressions and filter stored rows
Business scenario: Filter data in advance or accelerate expression computation.
Steps:
-
Create the table and insert data:
create table d_table (k1 int null,k2 int not null,k3 bigint null,k4 date null)duplicate key (k1,k2,k3)distributed BY hash(k1) buckets 3;insert into d_table select 1,1,1,'2020-02-20';insert into d_table select 2,2,2,'2021-02-20';insert into d_table select 3,-3,null,'2022-02-20'; -
Create two materialized views, one for expression precomputation and one for data prefiltering:
-- mv1 performs expression computation in advancecreate materialized view mv1 asselectabs(k1)+k2+1,sum(abs(k2+2)+k3+3)fromd_tablegroup byabs(k1)+k2+1;-- mv2 filters with a where expression in advance to reduce the data volume in the materialized viewcreate materialized view mv2 asselectyear(k4),month(k4)fromd_tablewhereyear(k4) = 2020; -
Verify materialized view hits:
-- Hits mv1selectabs(k1)+k2+1,sum(abs(k2+2)+k3+3)fromd_tablegroup byabs(k1)+k2+1;-- Hits mv1selectbin(abs(k1)+k2+1),sum(abs(k2+2)+k3+3)fromd_tablegroup bybin(abs(k1)+k2+1);-- Hits mv2selectyear(k4) + month(k4)fromd_tablewhereyear(k4) = 2020;-- Hits the original table d_table; does not hit mv2 because the where condition does not matchselectyear(k4),month(k4)fromd_table;
FAQ
Q1: After the materialized view is created, why is it not rewritten successfully?
Cause: The materialized view may still be under construction.
Diagnostic command:
show alter table materialized view from test_db;
Description: If the State field is not FINISHED, wait for the build to complete. Only after the state becomes FINISHED can a query hit the materialized view. In addition, if the base table contains no data, the hit may not be triggered either.
Q2: Why does a query stop using a synchronous materialized view after an upgrade?
Query plans and cost estimates can change after an upgrade. First confirm that the view is FINISHED, then compare EXPLAIN output for the query. Check MaterializedViewRewriteSuccessAndChose, MaterializedViewRewriteSuccessButNotChose, and MaterializedViewRewriteFail to distinguish a matching problem from a cost-based choice.
Older Apache Doris guidance recommended disabling enable_sync_mv_cost_based_rewrite when moving from Apache Doris 2.x to 3.0. That is historical compatibility advice and is not a VeloDB Cloud 26.x tuning procedure. If a regression remains after checking the plan, provide the query, view definition, and plans to VeloDB Cloud support.
Q3: What is the difference between sync and async materialized views?
| Comparison | Sync materialized view | Async materialized view |
|---|---|---|
| Data consistency | Strongly consistent with the base table; updated synchronously on write | Refreshed asynchronously with latency |
| Supported syntax | Single-table SELECT only | Supports complex queries such as multi-table JOIN |
| Query method | Query the base table. The optimizer can select the view index | Query the view as a table, or use transparent rewriting |
| Maintenance cost | Maintained automatically without manual intervention | Requires a refresh policy |
| Applicable scenarios | Single-table aggregation, prefix index, prefiltering, expression precomputation | Multi-table JOIN and cross-table precomputation |