Data changes in a base table may result in data inconsistency with its materialized view. To maintain data consistency between base tables and materialized views, OceanBase Database supports refreshes of materialized views. When a materialized view is refreshed, all its indexes are automatically updated.
The refresh of a materialized view is performed in series with the help of object locks. Base tables are not locked during the refresh of a materialized view. This improves the concurrency performance.
Refresh mode
You can use the ON DEMAND clause to specify the manual on-demand refresh mode for a materialized view.
You can use the DBMS_MVIEW package to manually refresh a materialized view, or set periodical refresh of a materialized view by using the START WITH ... NEXT... statement.
For more information about the DBMS_MVIEW package, see Overview.
Refresh methods
Complete refresh
Refresh condition
To perform a complete refresh for a materialized view, the column types of the materialized view must match those of the base tables. If they do not match, you cannot perform a complete refresh.
Working mechanism
OceanBase Database first creates a hidden table, executes refresh statements on the hidden table, and then replaces the original table with the hidden table. Therefore, a complete refresh operation requires additional storage space. In addition, if the original table has indexes, all indexes are re-created.
Note
A complete refresh may take a long time to complete, especially when the materialized view involves the read and processing of a large amount of data. Therefore, we recommend that you consider the time required before you perform a complete refresh.
Incremental refreshes
Notice
This method determines the content for an incremental refresh based on the records in the materialized view log. To perform an incremental refresh for a materialized view, you need to create materialized view logs on base tables before you create the materialized view. For information about how to create a materialized view log, see Create a materialized view log.
Limitations on incremental refreshes of an aggregate materialized view involving a single table
- The
MAXandMINaggregate functions are not supported for incremental refreshes. - Incremental refreshes are not supported in complex scenarios involving inner join views,
UNIONoperators, or subqueries. - Expressions containing random output values, such as
ROWNUMandRAND, are not supported. *ROLLUPandHAVINGare not supported. - The
DISTINCTkeyword can be disabled or removed. This is because the selected output columns are unique in a materialized view that supports an incremental refresh. ORDER BYis not supported.- Window functions are not supported.
- Scalar aggregate functions without a
GROUP BYclause are supported. - Only aggregate functions, such as
SUMandCOUNT, are supported for the incremental refresh of a materialized view that involves aGROUP BYclause. The aggregate functions can use only simple columns and the output must contain theCOUNT(*)andGROUP BYcolumns.
The requirements for GROUP BY are as follows:
- The
GROUP BYclause must be in standardGROUP BYsyntax.ROLLUPandHAVINGare not supported. - The
SELECTclause must contain allGROUP BYcolumns. - The aggregate functions must not contain the
DISTINCTkeyword. The arguments of aggregate functions must be columns of the base table. - The
SELECTclause must containCOUNT(*). The following table lists other supported aggregate functions. At present, theMINandMAXfunctions are not supported.
| Aggregate function | Dependent columns that must be contained in the SELECT clause |
|---|---|
| COUNT( expr ) | N/A |
| SUM ( expr ) | COUNT( expr ) or non-null expressions |
| AVG ( expr ) | SUM ( expr ) and COUNT( expr ) |
| STDDEV ( expr ) | SUM ( expr ), COUNT( expr ), and SUM ( expr * expr ) |
| VARIANCE ( expr ) | SUM ( expr ), COUNT( expr ), and SUM ( expr * expr ) |
Other aggregate functions that can be split into SUM and COUNT (The precision may vary based on the calculation method.) |
SUM (col1) and COUNT(col1) |
Limitations on incremental refreshes of a materialized view involving a multi-table join
- A table specified in the
FROMclause must be a base table, and cannot be an inner join view, a normal view, or a materialized view. - The
FROMclause contains at least two tables that are joined through an inner join. - A table specified in the
FROMclause must have a primary key that is specified in theSELECTstatement. - Materialized view logs are created on all tables specified in the
FROMclause, and contain all columns used in the materialized view. - The definition of the materialized view does not contain subqueries.
- The definition of a materialized view contains only select-project-join (SPJ) queries without the
GROUP BY,ROLLUP,HAVING,WINDOW FUNCTION,DISTINCT,ORDER BY,LIMIT, orFETCHclause. - The definition of a materialized view does not contain expressions with random output values, such as
ROWNUM,RAND, andSYSDATE.
Limitations on incremental refreshes of an aggregate materialized view involving multiple tables
The limitations on incremental refreshes of an aggregate materialized view involving multiple tables are the union set of the [Limitations on incremental refreshes of an aggregate materialized view involving a single table](#Limitations on incremental refreshes of an aggregate materialized view involving a single table) and [Limitations on incremental refreshes of a materialized view involving a multi-table join](#Limitations on incremental refreshes of a materialized view involving a multi-table join) sections.
Example of incremental refreshes of an aggregate materialized view involving a single table
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 NUMBER PRIMARY KEY, col2 NUMBER, col3 NUMBER, col4 NUMBER);Create a materialized view log on the
test_tbl1table.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE (col2, col3) INCLUDING NEW VALUES;Create a materialized view and specify the incremental refresh method.
Create a materialized view named
mv1_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(*),count(col3), andsum(col3)results of thecol2column in thetest_tbl1table. Then, group the data by the value of thecol2column.CREATE MATERIALIZED VIEW mv1_test_tbl1 REFRESH FAST ON DEMAND AS SELECT col2, count(*) cnt, count(col3) cnt_col3, sum(col3) sum_col3 FROM test_tbl1 GROUP BY col2;Create a materialized view named
mv2_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(*),count(col3), andsum(col3)results of thetest_tbl1table.CREATE MATERIALIZED VIEW mv2_test_tbl1 REFRESH FAST ON DEMAND AS SELECT count(*) cnt, count(col3) cnt_col3, sum(col3) sum_col3 FROM test_tbl1;Create a materialized view named
mv3_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(col3)andsum(col3)results of thetest_tbl1table.CREATE MATERIALIZED VIEW mv3_test_tbl1 REFRESH FAST ON DEMAND AS SELECT count(col3) cnt_col3, sum(col3) sum_col3 FROM test_tbl1;Create a materialized view named
mv4_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(*),count(col3), andsum(col3)results of thecol2andcol3columns in thetest_tbl1table. Then, group the data by the values of thecol2andcol3columns.CREATE MATERIALIZED VIEW mv4_test_tbl1 REFRESH FAST ON DEMAND AS SELECT col2, col3, count(*) cnt, count(col3) cnt_col3, sum(col3) sum_col3 FROM test_tbl1 GROUP BY col2, col3;Create a materialized view named
mv5_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(*),count(col3),sum(col3), andavg(col3)results of thecol2column, and thecalcol1andcalcol2results of some custom columns in thetest_tbl1table. Then, group the data by the value of thecol2column.CREATE MATERIALIZED VIEW mv5_test_tbl1 REFRESH FAST ON DEMAND AS SELECT col2, count(*) cnt, count(col3) cnt_col3, sum(col3) sum_col3, avg(col3) avg_col3, avg(col3) * sum(col3)/col2 calcol1, col2+sum(col3) calcol2 FROM test_tbl1 GROUP BY col2;Create a materialized view named
mv6_test_tbl1. Specify incremental refresh as the refresh method, and specify that you can manually initiate a refresh as needed. In the query part, calculate thecount(*),count(col3),sum(col3),count(col3*col3),sum(col3*col3)results of thecol2column in thetest_tbl1table. Then, group the data by the value of thecol2column.CREATE MATERIALIZED VIEW mv6_test_tbl1 REFRESH FAST ON DEMAND AS SELECT col2, count(*) cnt, count(col3) cnt_col3, sum(col3) sum_col3, count(col3*col3) cnt_col3_2, sum(col3*col3) sum_col3_2, STDDEV(col3) stddev_col3 FROM test_tbl1 GROUP BY col2;
Example of incremental refreshes of a materialized view involving a multi-table join
Create two base tables named
t1andt2respectively.CREATE TABLE t1(c1 INT PRIMARY KEY, c2 INT, c3 INT);CREATE TABLE t2(c1 INT PRIMARY KEY, c4 INT, c5 INT);Create materialized view logs on the
t1andt2tables.CREATE MATERIALIZED VIEW LOG ON t1 WITH PRIMARY KEY, ROWID, SEQUENCE (c2) INCLUDING NEW VALUES;CREATE MATERIALIZED VIEW LOG ON t2 WITH PRIMARY KEY, ROWID, SEQUENCE (c4) INCLUDING NEW VALUES;Create an incrementally refreshed materialized view named
mv1_t1_t2based on a join of thet1andt2tables.CREATE MATERIALIZED VIEW mv1_t1_t2 REFRESH FAST AS SELECT t1.c1 t1c1, t1.c2, t2.c1 t2c1, t2.c4 FROM t1 JOIN t2 ON t1.c1=t2.c1;
Note
- To enable a simple join-based materialized view to achieve high incremental refresh performance and real-time materialized view query performance, we recommend that you create indexes for the materialized view and dependent base tables in the following way:
- Create an index for the join key of each table to improve the table join performance during incremental refreshes and real-time materialized view queries.
- Create an index for the primary key column of each base table of the materialized view.
- The incremental refresh performance and real-time materialized view query performance will deteriorate as more tables are joined by the materialized view.
Here are some examples of creating indexes for a materialized view and its dependent base tables:
(Optional) Delete related test data.
If the following database objects do not exist, skip this step.
DROP MATERIALIZED VIEW LOG ON t1; DROP TABLE t1; DROP MATERIALIZED VIEW LOG ON t2; DROP TABLE t2; DROP MATERIALIZED VIEW rt_mv1;Create a table named
t1and an index namedidx_t1_c2on this table.CREATE TABLE t1(c1 INT GENERATED BY DEFAULT AS IDENTITY, c2 INT, c3 INT, c4 INT, c5 INT, PRIMARY KEY(c1)); CREATE INDEX idx_t1_c2 ON t1(c2);Create a table named
t2and an index namedidx_t2_c3on this table.CREATE TABLE t2(c1 INT GENERATED BY DEFAULT AS IDENTITY, c2 INT, c3 INT, c4 INT, c5 INT, PRIMARY KEY(c1)); CREATE INDEX idx_t2_c3 ON t2(c3);Create a materialized view log on each of the
t1andt2tables.CREATE MATERIALIZED VIEW LOG ON t1 WITH PRIMARY KEY, ROWID, SEQUENCE (c2, c3, c4) INCLUDING NEW VALUES; CREATE MATERIALIZED VIEW LOG ON t2 WITH PRIMARY KEY, ROWID, SEQUENCE (c2, c3, c4) INCLUDING NEW VALUES;Create a real-time materialized view named
rt_mv1.CREATE MATERIALIZED VIEW rt_mv1 NEVER REFRESH ENABLE ON QUERY COMPUTATION DISABLE QUERY REWRITE AS SELECT t1.c1 AS t1_c1, t2.c1 AS t2_c1, t1.c2 AS t1_c2, t2.c2 AS t2_c2, t1.c3 AS t1_c3, t2.c3 AS t2_c3 FROM t1, t2 WHERE t1.c2 = t2.c3;Create an index for the primary key column of each base table of the materialized view.
CREATE INDEX idx_mv_t1_c1 ON rt_mv1(t1_c1); CREATE INDEX idx_mv_t2_c1 ON rt_mv1(t2_c1);
Example of incremental refreshes of an aggregate materialized view involving multiple tables
Create two base names named
t3andt4respectively.CREATE TABLE t3(c1 INT, c2 INT, c3 INT, c4 INT, PRIMARY KEY(c1));CREATE TABLE t4(c1 INT, c2 INT, c3 INT, c4 INT, PRIMARY KEY(c1));Create a materialized view log on each of the
t3andt4tables.CREATE MATERIALIZED VIEW LOG ON t3 WITH PRIMARY KEY, ROWID, SEQUENCE(c2, c3, c4) INCLUDING NEW VALUES;CREATE MATERIALIZED VIEW LOG ON t4 WITH PRIMARY KEY, ROWID, SEQUENCE(c2, c3, c4) INCLUDING NEW VALUES;Create an incrementally refreshed materialized view named
mv1_t3_t4based on a join of thet3andt4tables.CREATE MATERIALIZED VIEW mv1_t3_t4 REFRESH FAST ENABLE ON QUERY COMPUTATION AS SELECT t3.c1, COUNT(*) cnt, COUNT(t4.c4) cnt_c4, SUM(t4.c4) sum_c4, AVG(t4.c4) avg_c4 FROM t3, t4 WHERE t3.c2 = t4.c3 GROUP BY t3.c1;
Manually refresh a materialized view
If you set the refresh mode of a materialized view to ON DEMAND, you can use the DBMS_MVIEW package to manually refresh the materialized view.
Note
Only the owner of a materialized view and the tenant administrator can refresh the materialized view.
Refresh a materialized view by using REFRESH
DBMS_MVIEW.REFRESH (
list IN VARCHAR2, -- The name of the materialized view. You can specify only one materialized view.
method IN VARCHAR2 : = NULL, -- The refresh options. Valid values:
-- f: specifies to perform fast refreshes.
-- ?: specifies to perform forcible refreshes.
-- C|c: specifies to perform complete refreshes.
-- A|a: specifies to always perform refreshes, which is equivalent to C.
----------- The following parameters are provided for the compatibility with Oracle and do not take effect. ----------------
rollback_seg IN VARCHAR2 : = NULL,
push_deferred_rpc IN BOOLEAN : = true,
refresh_after_errors IN BOOLEAN : = false,
purge_option IN BINARY_INTEGER : = 1,
parallelism IN BINARY_INTEGER : = 0,
heap_size IN BINARY_INTEGER : = 0,
atomic_refresh IN BOOLEAN : = true,
nested IN BOOLEAN : = false,
out_of_place IN BOOLEAN : = false,
skip_ext_data IN BOOLEAN : = false,
---------------------------------------------------------
refresh_parallel IN BINARY_INTEGER : = 1); -- The DOP of the materialized view refresh operation. This parameter is supported only in OceanBase Database.
Here is an example:
Insert three records into the
test_tbl1table.INSERT INTO test_tbl1 VALUES (1, 1, 1, 1),(2, 2, 2, 2),(3, 3, 3, 3);Query the information about the
mv1_test_tbl1materialized view.SELECT * FROM mv1_test_tbl1;The return result is as follows:
Empty setManually refresh the materialized view
mv1_test_tbl1by usingDBMS_MVIEW.REFRESH.CALL DBMS_MVIEW.REFRESH('mv1_test_tbl1');Query the information about the
mv1_test_tbl1materialized view again.SELECT * FROM mv1_test_tbl1;The return result is as follows:
+------+------+----------+----------+ | COL2 | CNT | CNT_COL3 | SUM_COL3 | +------+------+----------+----------+ | 3 | 1 | 1 | 3 | | 2 | 1 | 1 | 2 | | 1 | 1 | 1 | 1 | +------+------+----------+----------+ 3 rows in set
Refresh DOP
You can use the refresh_parallel option to specify the degree of parallelism (DOP) of the refresh operation. This option is supported only for complete refreshes.
Here is an example:
Refresh the materialized view with a specified DOP.
CALL DBMS_MVIEW.REFRESH('mv3', refresh_parallel => 8);
Automatically refresh a materialized view
If you specify the START WITH datetime_expr and NEXT datetime_expr clauses when you create a materialized view, a background automatic refresh task is created for the materialized view.
Refresh DOP
You can specify the DOP of a background automatic refresh task in the following three ways:
Note
- Priorities of the DOPs specified in the following ways decrease in sequence.
- The refresh DOP is supported only for complete refreshes.
- The following sample SQL statements are for references only and cannot be executed.
Specify the refresh DOP in a hint.
Here is an example:
CREATE /*+ parallel(8) */ MATERIALIZED VIEW ...Specify the refresh DOP in the session variable of a DDL operation.
Here is an example:
Enable the DOP of the DDL operation.
ALTER SESSION ENABLE PARALLEL DDL;Set the DOP value of the DDL operation.
ALTER SESSION FORCE PARALLEL DDL PARALLEL 8;
Specify the table-level DOP when you create the materialized view.
Here is an example:
CREATE MATERIALIZED VIEW xxx PARALLEL 8 ...
Statistics on materialized view refreshes
OceanBase Database can collect and store statistics on materialized view refreshes. You can query specific views to view the statistics. Statistics on both ongoing and history materialized view refresh operations are stored in the database. Statistics on history materialized view refreshes help you understand and analyze the performance of materialized view refreshes in the database.
Statistics on materialized view refreshes provide the following benefits:
Reporting: The system provides statistics overviews of ongoing and history materialized view refresh operations, including the actual time required for executing the refresh. This helps you track and monitor the refresh performance.
Diagnostics: You can use detailed statistics on ongoing and history materialized view refresh operations to effectively analyze the refresh performance. For example, if the refresh of a materialized view takes a long time, you can check the refresh statistics and identify whether the performance decrease is due to the increase in system workload or data changes.
Collect statistics on materialized views
OceanBase Database allows you to collect statistics on materialized views. You can use the analyze table or call dbms_stats.gather_table_stats('database_name', 'table_name') statement to collect statistics.
For more information about how to collect statistics on tables and columns, see GATHER_TABLE_STATS.
For more information about how to collect and retain refresh statistics on materialized views, see Overview.
Statistics on materialized view refreshes
| View | Description |
|---|---|
| ALL_MVIEWS | Displays the information about materialized views. |
| DBA_MVREF_STATS_SYS_DEFAULTS | Displays all default values of the refresh history statistics properties for materialized views in the entire system. |
| DBA_MVREF_STATS_PARAMS | Displays the refresh statistics properties associated with each materialized view. |
| DBA_MVREF_RUN_STATS | Displays the information about each refresh of the materialized views. Each refresh is identified by a REFRESH_ID value. |
| DBA_MVREF_STATS | Displays the basic timing statistics on materialized view refreshes. |
| DBA_MVREF_CHANGE_STATS | Displays the refresh statistics on materialized views. |
| DBA_MVREF_STMT_STATS | Displays the information about the refresh statements of materialized views. |