Overview
Applications in databases often perform complex queries on large tables. Such queries involve multi-table joins and aggregations on tables that contain billions of rows. Processing these queries can be expensive, in terms of system resources and the time it takes to compute the results. Asynchronous materialized views in the database are designed to tackle these issues. An asynchronous materialized view is a special physical table that holds pre-computed query results from one or more base tables. When you perform complex queries on the base table, the database returns the pre-computed results from the relevant materialized views to process these queries. This way, query performance can be improved because repetitive complex calculations are avoided. This performance difference can be significant when a query is run frequently or is sufficiently complex. Additionally, asynchronous materialized views are especially useful for building mathematical models upon your data warehouse. By doing so, you can provide a unified data specification to upper-layer applications, shield the underlying implementation, or protect the raw data security of the base tables.Understand materialized views in the database
StarRocks v2.3 and earlier versions provided a synchronous materialized view that can be built only on a single table. Synchronous materialized views, or the Rollup, retain higher data freshness and lower refreshing costs. However, compared to asynchronous materialized views supported from v2.4 onwards, synchronous materialized views have many limitations. You have limited choices of aggregation operators when you want to build a synchronous materialized view to accelerate or rewrite your queries. The following table compares the asynchronous materialized views (ASYNC MV) and the synchronous materialized view (SYNC MV) in the database in the perspective of features that they support:Basic concepts
- Base table Base tables are the driving tables of a materialized view. For the databaseā asynchronous materialized views, base tables can be native tables in the default catalog, tables in external catalogs (supported from v2.5), or even existing asynchronous materialized views (supported from v2.5) and views (supported from v3.1). The database supports creating asynchronous materialized views on all types of tables.
-
Refresh
When you create an asynchronous materialized view, its data reflects only the state of the base tables at that time. When the data in the base tables change, you need to refresh the materialized view to keep the changes synchronized.
Currently, the database supports two generic refreshing strategies:
- ASYNC: Asynchronous refresh mode. Materialized views can be refreshed automatically when the data in the base table changes, or at regular intervals based on specified intervals.
- MANUAL: Manual refresh mode. The materialized view will not be automatically refreshed. The refresh tasks can only be triggered manually by users.
-
Query rewrite
Query rewrite means that when executing a query on base tables with materialized views built on, the system automatically judges whether the pre-computed results in the materialized view can be reused for the query. If they can be reused, the system will load the data directly from the relevant materialized view to avoid the time- and resource-consuming computations or joins.
From v2.5, the database supports automatic, transparent query rewrite based on the SPJG-type asynchronous materialized views. The SPJG-type materialized views refer to materialized views whose plan only includes Scan, Filter, Project, and Aggregate types of operators.
NOTE Asynchronous materialized views created on base tables in a JDBC catalog or a Hudi catalog do not support query rewrite.
Decide when to create a materialized view
You can create an asynchronous materialized view if you have the following demands in your data warehouse environment:- Accelerating queries with repetitive aggregate functions Suppose that most queries in your data warehouse include the same sub-query with an aggregate function, and these queries have consumed a huge proportion of your computing resources. Based on this sub-query, you can create an asynchronous materialized view, which will compute and store all results of the sub-query. After the materialized view is built, the database rewrites all queries that contain the sub-query, loads the intermediate results stored in the materialized view, and thus accelerates these queries.
- Regular JOIN of multiple tables Suppose that you need to regularly join multiple tables in your data warehouse to make a new wide table. You can build an asynchronous materialized view for these tables, and set the ASYNC refreshing strategy that triggers refreshing tasks at a fixed time interval. After the materialized view is built, query results are returned directly from the materialized view, and thus the latency caused by JOIN operations is avoided.
- Data warehouse layering Suppose that your data warehouse contains a mass of raw data, and queries in it require a complex set of ETL operations. You can build multiple layers of asynchronous materialized views to stratify the data in your data warehouse, and thus decompose the query into a series of simple sub-queries. It can significantly reduce repetitive computation, and, more importantly, help your DBA identify the problem with ease and efficiency. Beyond that, data warehouse layering helps decouple raw data and statistical data, protecting the security of sensitive raw data.
- Accelerating queries in data lakes Querying a data lake can be slow due to network latency and object storage throughput. You can enhance the query performance by building an asynchronous materialized view on top of the data lake. Moreover, the database can intelligently rewrite queries to use the existing materialized views, saving you the trouble of modifying your queries manually.
Create an asynchronous materialized view
The databaseā asynchronous materialized views can be created on the following base tables:- The databaseā native tables (all table types are supported)
- Tables in external catalogs
- Hive Catalog
- Hudi Catalog
- Iceberg Catalog
- JDBC Catalog
- Paimon Catalog
- DeltaLake Catalog
- Existing asynchronous materialized views
- Existing views
Before you begin
The following examples involve two base tables in the default catalog:- The table
goodsrecords the item IDitem_id1, the item nameitem_name, and the item priceprice. - The table
order_listrecords the order IDorder_id, client IDclient_id, item IDitem_id2, and order dateorder_date.
goods.item_id1 is equivalent to the column order_list.item_id2.
Execute the following statements to create the tables and insert data into them:
sum(). Besides, the business scenario demands the data refresh at an interval of one day.
The query statement is as follows:
Create the materialized view
You can create a materialized view based on a specific query statement using CREATE MATERIALIZED VIEW. Based on the tablegoods, order_list, and the query statement mentioned above, the following example creates the materialized view order_mv to analyze the total of each order. The materialized view is set to refresh itself at an interval of one day.
NOTE
- While creating an asynchronous materialized view, you must specify either the data distribution strategy or the refresh strategy of the materialized view, or both.
- You can set different partitioning and bucketing strategies for an asynchronous materialized view from those of its base tables, but you must include the partition keys and bucket keys of the materialized views in the query statement used to create the materialized view.
- Asynchronous materialized views support a dynamic partitioning strategy in a longer span. For example, if the base table is partitioned at an interval of one day, you can set the materialized view to be partitioned at an interval of one month.
- The query statement used to create a materialized view does not support random functions, including rand(), random(), uuid(), and sleep().
- Asynchronous materialized views support a variety of data types. For more information, see CREATE MATERIALIZED VIEW - Supported data types.
- By default, executing a CREATE MATERIALIZED VIEW statement immediately triggers the refresh task, which can consume a certain proportion of the system resources. If you want to defer the refresh task, you can add the REFRESH DEFERRED parameter to your CREATE MATERIALIZED VIEW statement.
- Materialized view can only be created in default_catalog. You could either create it with default_catalog.database.mv, or switch to default_catalog through
set catalog <default_catalog>statement.
-
About refresh mechanisms of asynchronous materialized views
Currently, the database supports two ON DEMAND refresh strategies: MANUAL refresh and ASYNC refresh.
In StarRocks v2.5, asynchronous materialized views further support a variety of asynchronous refreshing mechanisms to control the cost of refresh and increase the success rate:
- If an MV has many large partitions, each refresh can consume a large amount of resources. In v2.5, the database supports splitting refresh tasks. You can specify the maximum number of partitions to be refreshed, and the database performs refresh in batches, with a batch size smaller or equal to the specified maximum number of partitions. This feature ensures large asynchronous materialized views are stably refreshed, enhancing the stability and robustness of data modeling.
- You can specify the time to live (TTL) for partitions of an asynchronous materialized view, reducing the storage size taken by the materialized view.
- You can specify the refresh range to refresh only the latest few partitions, reducing the refresh overhead.
- You can specify the base tables where data changes will not automatically trigger a refresh of the corresponding materialized view.
- You can assign a resource group to the refresh task.
CAUTION To prevent full refresh operations from exhausting system resources and causing task failures, it is recommended to create partitioned materialized views based on partitioned base tables. This ensures that when data updates occur within a base table partition, only the corresponding partition of the materialized view are refreshed, rather than refreshing the entire materialized view. For more information, please refer to Data Modeling with Materialized Views - Partitioned Modeling.
- About nested materialized views StarRocks v2.5 supports creating nested asynchronous materialized views. You can build asynchronous materialized views based on existing asynchronous materialized views. The refreshing strategy for each materialized view does not affect the materialized views on the upper or lower layers. Currently, the database does not limit the number of nesting levels. In a production environment, we recommend that the number of nesting layers does not exceed THREE.
- About external catalog materialized views The database supports building asynchronous materialized views based on Hive Catalog (since v2.5), Hudi Catalog (since v2.5), Iceberg Catalog (since v2.5), and JDBC Catalog (since v3.0). Creating a materialized view on external catalogs is similar to creating an asynchronous materialized view on the default catalog, but with some usage restrictions. For more information, please refer to Data lake query acceleration with materialized views.
Manually refresh an asynchronous materialized view
You can refresh an asynchronous materialized view regardless of its refreshing strategy via REFRESH MATERIALIZED VIEW. StarRocks v2.5 supports refreshing specific partitions of an asynchronous materialized view by specifying partition names. StarRocks v3.1 supports making a synchronous call of the refresh task, and the SQL statement is returned only when the task succeeds or fails.Query the asynchronous materialized view directly
The asynchronous materialized view you created is essentially a physical table that contains the complete set of pre-computed results in accordance with the query statement. Therefore, you can directly query the materialized view after the materialized view is refreshed for the first time.NOTE You can directly query an asynchronous materialized view, but the results may be inconsistent with what you get from the query on its base tables.
Rewrite and accelerate queries with the asynchronous materialized view
StarRocks v2.5 supports automatic and transparent query rewrite based on the SPJG-type asynchronous materialized views. The SPJG-type materialized views query rewrite includes single table query rewrite, Join query rewrite, aggregation query rewrite, Union query rewrite and query rewrite based on nested materialized views. From v3.3.0 onwards, the database further supports text-based materialized view rewrite. For more information, please refer to Query Rewrite with Materialized Views. Currently, the database supports rewriting queries on asynchronous materialized views that are created on the default catalog or an external catalog such as a Hive catalog, Hudi catalog, or Iceberg catalog. When querying data in the default catalog, the database ensures strong consistency of results between the rewritten query and the original query by excluding materialized views whose data is inconsistent with the base table. When the data in a materialized view expires, the materialized view will not be used as a candidate materialized view. When querying data in external catalogs, the database does not ensure a strong consistency of the results because the database cannot perceive the data changes in external catalogs. For more about asynchronous materialized views that are created based on an external catalog, please refer to Data lake query acceleration with materialized views.NOTE Asynchronous materialized views created on base tables in a JDBC catalog do not support query rewrite.
Manage an asynchronous materialized view
Alter an asynchronous materialized view
You can alter the property of an asynchronous materialized view using ALTER MATERIALIZED VIEW.-
Enable an inactive materialized view.
-
Rename an asynchronous materialized view.
-
Alter the refreshing interval of an asynchronous materialized view to 2 days.
Show asynchronous materialized views
You can view the asynchronous materialized views in your database by using SHOW MATERIALIZED VIEWS or querying the system metadata view in Information Schema.-
Check all asynchronous materialized views in your database.
-
Check a specific asynchronous materialized view.
-
Check specific asynchronous materialized views by matching the name.
-
Check all asynchronous materialized views by querying the metadata view
materialized_viewsin Information Schema. For more information, please refer to information_schema.materialized_views.
Check the definition of asynchronous materialized view
You can check the query used to create an asynchronous materialized view via SHOW CREATE MATERIALIZED VIEW.Check the execution status of asynchronous materialized view
You can check the execution (building or refreshing) status of an asynchronous materialized view by querying thetasks and task_runs in Information Schema.
The following example checks the execution status of the materialized view that was created most recently:
-
Check the
TASK_NAMEof the most recent task in the tabletasks. -
Check the execution status in the table
task_runsusing theTASK_NAMEyou have found.
Check the refresh mode
- To check which mode was configured for a materialized view, read
REFRESH_MODEinSHOW MATERIALIZED VIEWSorinformation_schema.materialized_views. - To check which mode a specific refresh used, read
EXTRA_MESSAGE.refreshModefor that run ininformation_schema.task_runs. See Querying Task Run Details for a SQL example.
Drop an asynchronous materialized view
You can drop an asynchronous materialized view via DROP MATERIALIZED VIEW.Relevant session variables
The following variables control the behavior of an asynchronous materialized view:analyze_mv: Whether and how to analyze the materialized view after refresh. Valid values are an empty string (Do not analyze),sample(Sampled statistics collection), andfull(Full statistics collection). Default issample.enable_materialized_view_rewrite: Whether to enable the automatic rewrite for materialized view. Valid values aretrue(Default since v2.5) andfalse.