CAUTION Some of the features shown below are only supported from StarRocks v3.1 onwards.
Examine an asynchronous materialized view
To get a whole picture of asynchronous materialized views that you are working with, you can first check their working state, refresh history, and resource consumption.Check the working state of an asynchronous materialized view
You can check the working state of an asynchronous materialized view using SHOW MATERIALIZED VIEWS. Among all the information returned, you can focus on the following fields:is_active: Whether the state of the materialized view is active. Only an active materialized view can be used for query acceleration and rewrite.last_refresh_state: The state of the last refresh, including PENDING, RUNNING, FAILED, and SUCCESS.last_refresh_error_message: The reason why the last refresh failed (if the materialized view state is not active).rows: The number of data rows in the materialized view. Please note that this value can be different from the actual row count of the materialized view because the updates can be deferred.
View the refresh history of an asynchronous materialized view
You can view the refresh history of an asynchronous materialized view by querying the tabletask_runs in the database information_schema. Among all the information returned, you can focus on the following fields:
CREATE_TIMEandFINISH_TIME: The start and end time of the refresh task.STATE: The state of the refresh task, including PENDING, RUNNING, FAILED, and SUCCESS.ERROR_MESSAGE: The reason why the refresh task failed.
Monitor the resource consumption of an asynchronous materialized view
You can monitor and analyze the resource consumed by an asynchronous materialized view during and after the refresh.Monitor resource consumption during refresh
During a refresh task, you can monitor its real-time resource consumption using SHOW PROC ‘/current_queries’. Among all the information returned, you can focus on the following fields:ScanBytes: The size of data that is scanned.ScanRows: The number of data rows that are scanned.MemoryUsage: The size of memory that is used.CPUTime: CPU time cost.ExecTime: The execution time of the query.
Analyze resource consumption after refresh
After a refresh task, you can analyze its resource consumption via query profiles. You can view the profile of the materialized view refresh task through the Web UI of the cluster Leader FE node. While an asynchronous materialized view refreshes itself, an INSERT OVERWRITE statement is executed. You can check the corresponding query profile to analyze the time and resources consumed by the refresh task. Among all the information returned, you can focus on the following metrics:Total: Total time consumed by the query.QueryCpuCost: Total CPU time cost of the query. CPU time costs are aggregated for concurrent processes. As a result, the value of this metric may be greater than the actual execution time of the query.QueryMemCost: Total memory cost of the query.- Other metrics for individual operators, such as join operators and aggregate operators.
Verify whether queries are rewritten by an asynchronous materialized view
You can check whether a query can be rewritten with an asynchronous materialized view from its query plan using EXPLAIN. If the metricSCAN in the query plan shows the name of the corresponding materialized view, the query has been rewritten by the materialized view.
Example 1:
Diagnose and solve problems
Here we list some common problems you might encounter while working with an asynchronous materialized view, and the corresponding solutions.Materialized view building failure
If you failed to create an asynchronous materialized view, that is, the CREATE MATERIALIZED VIEW statement cannot be executed, you can look into the following aspects:-
Check whether you have mistakenly used the SQL statement for synchronous materialized views.
The database provides two different materialized views: the synchronous materialized view and the asynchronous materialized view.
The basic SQL statement used to create a synchronous materialized view is as follows:
However, the SQL statement used to create an asynchronous materialized view contains more parameters:In addition to the SQL statement, the main difference between the two materialized views is that asynchronous materialized views support all query syntax that the database provides, but synchronous materialized views only support limited choices of aggregate functions.
- Check whether you have specified a correct partition column. When creating an asynchronous materialized view, you can specify a partitioning strategy, which allows you to refresh the materialized view on a finer granularity level. Currently, the database only supports range partitioning, and only supports referencing a single column from the SELECT expression in the query statement that is used to build the materialized view. You can use the date_trunc() function to truncate the column to change the granularity level of the partitioning strategy. Please note that any other expressions are not supported.
- Check whether you have the necessary privileges to create the materialized view. When creating an asynchronous materialized view, you need the SELECT privileges of all objects (tables, views, materialized views) that are queried. When UDFs are used in the query, you also need the USAGE privileges of the functions.
Materialized view refresh failure
If the materialized view fails to refresh, that is, the state of the refresh task is not SUCCESS, you can look into the following aspects:- Check whether you have adopted an inappropriate refresh strategy. By default, the materialized view refreshes immediately after it is created. However, in v2.5 and early versions, materialized views that adopt the MANUAL refresh strategy do not refresh after being created. You must refresh it manually using REFRESH MATERIALIZED VIEW.
-
Check whether the refresh task exceeds the memory limit.
Usually, when an asynchronous materialized view involves large-scale aggregation or join calculations that exhaust the memory resources. To solve this problem, you can:
- Specify a partitioning strategy for the materialized view to refresh one partition each time.
- Enable the Spill to Disk feature for the refresh task. From v3.1 onwards, the database supports spilling the intermediate results to disks when refreshing a materialized view. Execute the following statement to enable Spill to Disk:
Materialized view refresh timeout
Large materialized views may fail to refresh because the refresh task exceeds the timeout period. You can consider the following solutions to solve this problem:- Specify a partitioning strategy for the materialized view to achieve fine-grained refresh As described in Create partitioned materialized views, by partitioning the materialized view you can achieve incremental building and refresh, thus avoiding the issue of excessive resource consumption during the initial refresh.
-
Set a longer timeout period
The default timeout for materialized view refresh tasks is 5 minutes in versions earlier than v3.2 and 1 hour in v3.2 and later. If you encounter timeout exceptions, you can adjust the timeout period by using the following statement:
-
Analyze performance bottlenecks of the materialized view refresh
Refreshing materialized views with complex computation is time-consuming. You can analyze its performance bottlenecks by analyzing the query profile of the refresh task:
- Obtain the
query_idcorresponding to the refresh task by queryinginformation_schema.task_runs. - Analyze the query profile of the refresh task using the following statements:
- GET_QUERY_PROFILE: Retrive the original query profile based on
query_id. - ANALYZE PROFILE: Analyze the query profile on a per-fragment basis, and display it in a tree structure.
- GET_QUERY_PROFILE: Retrive the original query profile based on
- Obtain the
Materialized view state is not active
If the materialized view cannot rewrite queries or refresh, and the state of the materialized viewis_active is false, it could be a result of the schema change on the base tables. To solve this problem, you can manually set the materialized view state to active by executing the following statement:
Materialized view refresh task uses excessive resources
If you find that the refresh tasks are using excessive system resources, you can look into the following aspects:- Check whether you have created a materialized view too large. If you have joined too many tables that cause a significant amount of calculation, the refresh task will occupy many resources. To solve this problem, you need to assess the size of the materialized view and re-plan it.
- Check whether you have set an unnecessarily frequent refresh interval. If you adopt the fixed-interval refresh strategy, you can set a lower refresh frequency to solve the problem. If the refresh tasks are triggered by data changes in the base tables, loading data too frequently can also cause this problem. To solve this problem, you need to define a proper refresh strategy for your materialized view.
- Check whether the materialized view is partitioned. An unpartitioned materialized view can be costly to refresh because the database refreshes the whole materialized view each time. To solve this problem, you need to specify a partitioning strategy for the materialized view to refresh one partition each time.
-
Set the materialized view state to inactive so that all of its refresh tasks are stopped:
-
Terminate the running refresh task using CANCEL REFRESH MATERIALIZED VIEW:
Materialized view query rewrite failure
If your materialized view fails to rewrite relevant queries, you can look into the following aspects:-
Diagnose rewrite failure using TRACE
The database provides the TRACE statement for you to diagnose materialized view rewrite failure:
TRACE LOGS MV <query>: Available in v3.2 and later, this command analyzes the detailed rewrite process and the reasons of the failure.TRACE REASON MV <query>: Available in v3.2.8 and later, this command provides concise reasons for rewrite failure.
-
Check whether the materialized view and the query match.
- The database matches the materialized view and the query with a structure-based matching technique rather than text-based matching. Therefore, it’s not guaranteed that a query can be rewritten just because it appears similar to that of a materialized view.
- Materialized views can only rewrite SPJG (Select/Projection/Join/Aggregation) type of queries. Queries involving window functions, nested aggregation, or join plus aggregation are not supported.
- Materialized views cannot rewrite queries that involve complex Join predicates in Outer Joins. For example, in cases like
A LEFT JOIN B ON A.dt > '2023-01-01' AND A.id = B.id, we recommend you specify the predicate from theJOIN ONclause in aWHEREclause.
-
Check whether the materialized view state is active.
The database checks the status of the materialized view before rewriting queries. Queries can be rewritten only when the materialized view state is active. To solve this problem, you can manually set the materialized view state to active by executing the following statement:
-
Check whether the materialized view meets the data consistency requirements.
The database checks the consistency of data in the materialized view and in the base table data. By default, queries can be rewritten only when the data in the materialized view is up-to-date. To solve this problem, you can:
- Add
PROPERTIES('query_rewrite_consistency'='LOOSE')to the materialized view to disable consistency checks. - Add
PROPERTIES('mv_rewrite_staleness_second'='5')to tolerate a certain degree of data inconsistency. Queries can be rewritten if the materialized view’s last confirmed complete refresh is within this time interval, regardless of whether the data in the base tables changes. A refresh covering only part of the partitions does not renew this baseline.
- Add
- Check whether the query statement of the materialized view lacks output columns. To rewrite range and point queries, you must specify the columns used as the filtering predicates in the SELECT expression of the materialized view’s query statement. You need to check the SELECT statement of the materialized view to ensure it includes columns referenced in the WHERE and ORDER BY clauses of the query.
mv1 uses nested aggregation. Thus, it cannot be used to rewrite queries.
mv2 uses join plus aggregation. Thus, it cannot be used to rewrite queries. To solve this problem, you can create a materialized view with aggregation, and then a nested materialized view with join based on the previous one.
mv3 cannot rewrite queries in the pattern of SELECT c_city, sum(tax) FROM tbl WHERE dt='2023-01-01' AND c_city = 'xxx' because the column that the predicate referenced is not in the SELECT expression.
FAQ
Which resource group controls the resources for asynchronous materialized views? How can I adjust the configuration of it?
If theresource_group property is not specified when creating an asynchronous materialized view, the system assigns it to the default resource group default_mv_wg. Its default configuration is:
cpu_weight_percent: 1mem_limit: 80%concurrency_limit: 0spill_mem_limit_threshold: 80%
default_mv_wg is stored and persisted in the FE. You can adjust its CPU limit, memory limit, concurrency limit, and spill threshold with ALTER RESOURCE GROUP, for example, ALTER RESOURCE GROUP default_mv_wg WITH ('mem_limit' = '60%'). The BE configuration items default_mv_resource_group_cpu_limit, default_mv_resource_group_memory_limit, default_mv_resource_group_concurrency_limit, and default_mv_resource_group_spill_mem_limit_threshold are deprecated and no longer affect default_mv_wg.
You can also assign a dedicated resource group to a materialized view by specifying the resource_group property.
Example:
If an asynchronous materialized view is scheduled to refresh every 1 minute, what happens if the refresh takes longer than 1 minute?
The system handles this automatically:- If there are pending tasks in the queue, the newly triggered task is merged (status
MERGED). - If there are no pending tasks (only running or succeeded/failed tasks), the newly triggered task waits until the current running task finishes before executing.
After setting auto_refresh_partitions_limit, will the system always refresh exactly that number of partitions?
No. This parameter represents an upper limit. If the system detects that fewer base-table partitions have changed, only those actual partitions will be refreshed.A materialized view is configured to refresh every 5 minutes, but in information_schema.task_runs it appears to refresh every 5 seconds. Why?
From v3.3 onwards, the default value of partition_refresh_number changed from -1 to 1, meaning each refresh operation only processes one partition at a time. If many partitions need to be refreshed, the system splits the work into multiple subtasks, which results in more frequent scheduling entries in task_runs than the user-defined refresh interval.