Updating data in the base table may result in inconsistencies between the materialized view and the base table. To maintain data consistency in the materialized view, OceanBase Database refreshes the materialized view as needed.
OceanBase Database supports both full and incremental refresh strategies for materialized views, as well as manual and automatic scheduling of refreshes.
Note
Refreshing a materialized view also automatically updates all its indexes.
Full refresh
OceanBase Database performs full refreshes using a shadow table. This process involves creating a hidden table, executing the refresh statement on the hidden table, and then swapping the original table with the hidden one. Therefore, full refresh operations require additional storage space and will completely rebuild indexes (if any exist).
Considerations for full refresh
- A full refresh can be a time-consuming process, especially when a large amount of data needs to be read and processed. Therefore, before performing a full refresh, you should always consider the time required for the full refresh.
- If the column types in the current base table match those in the materialized view, full refresh is supported. If not, full refresh is not supported.
- If a materialized view is fully refreshed, any dependent materialized views (nested materialized views) must be fully refreshed before they are incrementally refreshed. Otherwise, an error will be returned.
Incremental refresh
Currently, incremental refresh for materialized views supports SQL statements in five scenarios: single-table non-aggregation, single-table aggregation, multi-table join, multi-table join with aggregation, and set queries (UNION ALL). Incremental refresh is not supported for SQL statements that do not fit into these five scenarios. For more information about the SQL statements that support incremental refreshes, see the description below.
Notice
- Because the
REFRESH FASTmethod uses the records in the materialized view log (mlog) to determine what needs to be incrementally refreshed, you must create the materialized view log for the base table before creating the materialized view when using incremental refresh. - All columns used in the incrementally refreshed materialized view must be included in the mlog.
Incremental refresh in single-table non-aggregation scenarios
Note
For OceanBase Database V4.3.5, incremental refresh for materialized views supports single-table non-aggregation scenarios starting from V4.3.5 BP3.
Example of incremental refresh in single-table non-aggregation scenarios
Create a table named
tbl1.CREATE TABLE tbl1 (col1 INT PRIMARY KEY, col2 INT, col3 INT, col4 INT);Create a materialized view log on the
tbl1table.CREATE MATERIALIZED VIEW LOG ON tbl1 WITH SEQUENCE (col2, col3, col4) INCLUDING NEW VALUES;Create an incremental refresh materialized view
mv_tbl1based on thetbl1table.CREATE MATERIALIZED VIEW mv_tbl1 REFRESH FAST ON DEMAND AS SELECT col1, col2 FROM tbl1;
Incremental refresh in single-table aggregation scenarios
The basic requirements for incremental refresh in single-table aggregation scenarios are as follows:
The table specified in the
FROMclause must be a base table, not an inline view or a standard view.Note
Because nested materialized views do not support real-time materialized views, real-time materialized views cannot be used when the
FROMtable is a materialized view.Only one table is allowed in the
FROMclause.An mlog must be created on the
FROMtable, and all columns used in the view must exist in the mlog.The view definition must not contain subqueries.
Window functions are not supported.
The view definition must not include
ROLLUP,HAVING,DISTINCT,ORDER BY,LIMIT, orFETCHclause.If the query includes
DISTINCT, the output columns in the incrementally refreshable materialized view must be unique. In this case, you can either prohibit the use ofDISTINCTdirectly, or remove it.Statements without
GROUP BYmust be scalar aggregations.For materialized views with
GROUP BY, aggregation functions such asSUMandCOUNTare supported, and only simple columns can be used in the aggregation functions. The requirements forGROUP BYare as follows:Aggregation function Requirements for the SELECT clause COUNT( expr ) N/A SUM ( expr ) COUNT( expr ) or expr be not null AVG ( expr ) SUM ( expr ) and COUNT( expr ) STDDEV ( expr ) SUM ( expr ),COUNT( expr ),SUM ( expr * expr ) VARIANCE ( expr ) SUM ( expr ),COUNT( expr ),SUM ( expr * expr ) Other aggregation functions that can be decomposed into SUM and COUNT (the calculation method may change, which may affect the precision) SUM (col1) and COUNT(col1) MAX(expr)/MIN(expr) Note
For OceanBase Database V4.3.5, the
MAXandMINaggregate functions support incremental refresh from V4.3.5 BP4.COUNT(expr) - The
GROUP BYclause must be in the standardGROUP BYsyntax, and does not supportROLLUPorHAVING. - The
SELECTclause must contain allGROUP BYcolumns. - The aggregation functions do not contain the
DISTINCTkeyword, and the parameters are fundamental columns. - The
SELECTclause must contain the columns of the aggregation function and the dependency columns of the aggregation function, and theCOUNT(*)column.For example, if the aggregation functionSUM(expr)is used, theCOUNT(*)column, theCOUNT(expr)column, and theSUM(expr)column must be included.
- The
Requirements for incremental refresh with MAX/MIN aggregate functions:
When the preceding requirements are met, the GROUP BY column must have an index with the GROUP BY column as the prefix in the base table of the materialized view.
Note
For OceanBase Database V4.3.5, the MIN/MAX aggregate functions support non-basic columns as parameters starting from V4.3.5 BP5. In addition, non-basic columns can be specified in the GROUP BY clause.
Here is an example:
Create a table named
m_tbl1.obclient> CREATE TABLE m_tbl1( col1 INT PRIMARY KEY, col2 DATETIME, col3 INT, col4 INT, col5 INT, col6 INT);Create a materialized view log on the
m_tbl1table.obclient> CREATE MATERIALIZED VIEW LOG ON m_tbl1 WITH PRIMARY KEY, ROWID, SEQUENCE (col2, col3, col4, col5, col6) INCLUDING NEW VALUES;Create an index named
idx1_m_tbl1on them_tbl1table based on thecol3and(DATE(col2))columns.obclient> CREATE INDEX idx1_m_tbl1 ON m_tbl1(col3, (DATE(col2)));Create a materialized view named
m_tbl1_mv1for single-table incremental refresh with aggregation.obclient> CREATE MATERIALIZED VIEW m_tbl1_mv1 REFRESH FAST ON DEMAND AS SELECT col3, DATE(col2) gby_2, COUNT(*) cnt, MAX(col4) max_c4, MIN(col5 + col6) min_c5_c6 FROM m_tbl1 GROUP BY col3, DATE(col2);
Example of incremental refresh of a single-table aggregate materialized view
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 INT, col3 INT, col4 INT);Create a materialized view log on the
test_tbl1table.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE (col2, col3, col4) INCLUDING NEW VALUES;Create a materialized view that is refreshed incrementally.
Create a materialized view named
mv1_test_tbl1. Specify the refresh method as incremental and allow manual refresh. The query part of the materialized view selects thecol2column from thetest_tbl1table and calculates the aggregate results ofcount(*),count(col3), andsum(col3), grouped by the values 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 the refresh method as incremental and allow manual refresh. The query part of the materialized view calculates the aggregate results ofcount(*),count(col3), andsum(col3)from 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 the refresh method as incremental and allow manual refresh. The query part of the materialized view calculates the results ofcount(col3)andsum(col3)from 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 the refresh method as incremental and allow manual refresh. The query part of the materialized view selects thecol2andcol3columns from thetest_tbl1table and calculates the aggregate results ofcount(*),count(col3), andsum(col3), grouped 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 the refresh method as incremental and allow manual refresh. The query part of the materialized view selects thecol2column from thetest_tbl1table and calculates the aggregate results ofcount(*),count(col3),sum(col3), andavg(col3), and also calculates some custom columnscalcol1andcalcol2, grouped by the values 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 the refresh method as incremental and allow manual refresh. The query part of the materialized view selects thecol2column from thetest_tbl1table and calculates the aggregate results ofcount(*),count(col3),sum(col3),count(col3*col3),sum(col3*col3), andSTDDEV(col3), grouped by the values 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;Create a materialized view by using the
MAXandMINaggregate functions.Create an index named
idx_test_tbl1on thetest_tbl1table based on thecol1andcol2columns.CREATE INDEX idx_test_tbl1 ON test_tbl1(col1, col2);Create a materialized view named
mv7_test_tbl1. Specify the refresh method as incremental and allow manual refresh. The query part of the materialized view selects thecol1andcol2columns from thetest_tbl1table and calculates the aggregate results ofcount(*), the sum of the minimum value ofcol3and the maximum value ofcol4in each group, and groups the data by the combination ofcol1andcol2.CREATE MATERIALIZED VIEW mv7_test_tbl1 REFRESH FAST ON DEMAND AS SELECT
col1, col2, count(*) cnt, MIN(col3) + MAX(col4) AS min_max_val FROM test_tbl1 GROUP BY col1, col2; ```
Incremental refresh of a materialized view with multiple tables joined
The following requirements must be met for incremental refresh of a materialized view with multiple tables joined:
The
FROMclause cannot contain an inline view.The
FROMclause must contain at least two tables.Note
- For OceanBase Database V4.3.5, incremental refresh of a materialized view with multiple tables joined supports outer joins (
LEFT JOIN/RIGHT JOIN) starting from V4.3.5 BP3. - Outer joins are supported only if the join tree is a left-deep join tree in which
INNER JOINis performed beforeLEFT JOINorRIGHT JOIN.
- For OceanBase Database V4.3.5, incremental refresh of a materialized view with multiple tables joined supports outer joins (
Materialized log (mlog) must be created for each table in the
FROMclause, and the columns used in the materialized view must be present in the mlogs.The view definition must not contain subqueries.
The view definition must not contain the
ROLLUP,HAVING,WINDOW FUNCTION,DISTINCT,ORDER BY,LIMIT, orFETCHclause.The view definition must not contain expressions that generate unstable output values, such as
ROWNUM,RAND, andSYSDATE.
Notice
For OceanBase Database V4.3.5:
- Before V4.3.5 BP5, the
FROMclause must contain tables with primary keys, and the primary keys must be output in theSELECTclause. - Starting from V4.3.5 BP5, the requirement for the base tables to have primary keys is removed.
Example of incremental refresh of a materialized view with multiple tables joined
Create the base tables
t1andt2.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 the materialized view
mv1_t1_t2for incremental refresh of the join between 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 improve the performance of incremental refresh and real-time materialized views for simple joins, we recommend that you create indexes for the materialized view and its dependent base tables as follows:
- Create indexes on the join keys of each table to improve the join performance in incremental updates and real-time materialized views.
- Create indexes on the primary key columns of each base table in the materialized view.
- As the number of
JOINtables in the materialized view increases, the incremental refresh performance of the materialized view and the query performance of the real-time materialized view usually decrease.
Here is an example of creating indexes for the materialized view and its dependent base tables:
(Optional) Run the following statements to drop the test data.
You can skip this step if the following database objects do not exist.
DROP MATERIALIZED VIEW LOG ON t1; DROP TABLE IF EXISTS t1; DROP MATERIALIZED VIEW LOG ON t2; DROP TABLE IF EXISTS t2; DROP MATERIALIZED VIEW rt_mv1;Run the following statements to create the
t1table and theidx_t1_c2index.CREATE TABLE t1(c1 INT PRIMARY KEY AUTO_INCREMENT, c2 INT, c3 INT, c4 INT, c5 INT); CREATE INDEX idx_t1_c2 ON t1(c2);Run the following statements to create the
t2table and theidx_t2_c3index.CREATE TABLE t2(c1 INT PRIMARY KEY AUTO_INCREMENT, c2 INT, c3 INT, c4 INT, c5 INT); CREATE INDEX idx_t2_c3 ON t2(c3);Run the following statements to create materialized view logs on 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;Run the following statements to create the real-time materialized view
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;Run the following statements to create indexes on the primary key columns of the base tables in 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);
Incremental refresh of aggregate views over multiple tables
The requirements for incremental refresh of aggregate views over multiple tables are as follows:
The requirements for incremental refresh of aggregate views over multiple tables are the union of the requirements for incremental refresh of aggregate views over a single table and incremental refresh of joined views over multiple tables.
Incremental refresh is supported for materialized views with outer joins. The limitations for outer joins in non-aggregated materialized views also apply to aggregated materialized views with outer joins. The limitations for inner joins in aggregated materialized views also apply to aggregated materialized views with outer joins. However, aggregated materialized views with outer joins do not support the
MINandMAXaggregate functions, and do not support real-time materialized views.Note
For OceanBase Database V4.3.5, incremental refresh is supported for materialized views with outer joins starting with V4.3.5 CP5.
Examples of incremental refresh of aggregate views over multiple tables
Create the base tables
t3andt4.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 materialized view logs on tables
t3andt4.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 the real-time materialized view
mv1_t3_t4for the incremental refresh of the join and aggregate operations on tablest3andt4.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;
Incremental refresh of materialized views with set queries
Note
For OceanBase Database V4.3.5, incremental refresh of materialized views with set queries is supported starting from V4.3.5 BP3.
Set materialized views support using UNION ALL set queries and allow incremental refresh. Incrementally refreshed set materialized views permit all types of incrementally refreshable materialized views in each set branch, except for outer join materialized views.
The basic requirements for incremental refresh with set queries are as follows:
Materialized views with set queries do not support real-time materialized views.
The top-level query must be
UNION ALL; the view definition must not contain subqueries, norORDER BY/LIMIT/FETCHclauses.For each branch of
UNION ALL, the output column types at the same projection position must be the same. It is not supported to forcibly convert column types to a consistent type usingCASTin sub-branches.For example, for multi-table join materialized views, the primary keys of each joined table must appear in the
SELECTstatement. These output columns in theSELECTcannot have additionalCASToperations added due toUNION ALL.For each branch of
UNION ALL, if there are constants at the same projection position with the same type but different values, these constants can serve as identifier columns to distinguish each branch.
Example
Create the base tables
ua_tbl1andua_tbl2.CREATE TABLE ua_tbl1 (col1 INT PRIMARY KEY, col2 INT, col3 INT, col4 INT);CREATE TABLE ua_tbl2 (col1 INT PRIMARY KEY, col2 INT, col3 INT, col4 INT);Create materialized view logs on the
ua_tbl1andua_tbl2tables.CREATE MATERIALIZED VIEW LOG ON ua_tbl1 WITH PRIMARY KEY, ROWID, SEQUENCE (col2, col3, col4) INCLUDING NEW VALUES;CREATE MATERIALIZED VIEW LOG ON ua_tbl2 WITH PRIMARY KEY, ROWID, SEQUENCE (col2, col3, col4) INCLUDING NEW VALUES;Create the materialized view
mv_ua_tbl1_tbl2for incremental refresh of materialized views with set queries.CREATE MATERIALIZED VIEW mv_ua_tbl1_tbl2 REFRESH FAST ON DEMAND AS SELECT a.col1 as a_c1, b.col1 as b_c1, 1 marker, a.col2 val FROM ua_tbl1 a INNER JOIN ua_tbl2 b ON a.col2 = b.col3 UNION ALL SELECT col1 a_c1, col2 b_c1, 2 marker, count(*) val FROM ua_tbl1 GROUP BY col1, col2;
Parallelism control for materialized view refresh
OceanBase Database provides a parallelism control mechanism for materialized view refresh. If you explicitly specify the parallelism for a refresh operation, the specified value is used. If you do not explicitly specify the parallelism, you can configure the system variable mview_refresh_dop to control the parallelism for materialized view refresh at the session level.
Note
For OceanBase Database V4.3.5, the variable mview_refresh_dop was introduced in V4.3.5 BP1.
Set the mview_refresh_dop parameter
mview_refresh_dop is a system variable in OceanBase Database that specifies the default degree of parallelism (DOP) for materialized view refresh operations. By setting an appropriate value for this variable, you can significantly improve the refresh efficiency and optimize the database performance.
When mview_refresh_dop is set to 0 or 1, parallel refresh is disabled. For more information about the mview_refresh_dop system variable, see mview_refresh_dop.
Here are some examples:
Set the DOP for the current session to 5.
SET mview_refresh_dop = 5;Set the DOP for all sessions to 5.
SET GLOBAL mview_refresh_dop = 5;Notice
Setting a global variable does not take effect in the current session. You must log in again to create a new session for the setting to take effect.
View the parallelism information of a materialized view
Query the DBA_MVIEWS view to obtain the background refresh parallelism of a materialized view.
Note
The
DBA_MVIEWSview displays the parallelism of a materialized view that is specified for the materialized view.- When the value of the
REFRESH_DOPcolumn is not 0, the background refresh task of the materialized view uses the parallelism specified byREFRESH_DOP. - When the value of
REFRESH_DOPis 0, the parallelism specified by themview_refresh_dopparameter at the global level is used.
Here is an example:
Create a materialized view named
mv0_t1.CREATE MATERIALIZED VIEW mv0_t1 REFRESH COMPLETE ON DEMAND START WITH sysdate() NEXT sysdate() + INTERVAL 10 SECOND AS SELECT c1, c2 FROM t1;Check the background refresh parallelism setting for the materialized view
mv0_t1.SELECT OWNER, MVIEW_NAME,REFRESH_DOP FROM oceanbase.DBA_MVIEWS WHERE OWNER = 'db_test' AND MVIEW_NAME = 'mv0_t1';The return result is as follows:
+---------+------------+-------------+ | OWNER | MVIEW_NAME | REFRESH_DOP | +---------+------------+-------------+ | db_test | mv0_t1 | 0 | +---------+------------+-------------+ 1 row in setSet the parallelism of the
mv0_t1materialized view to 8.ALTER MATERIALIZED VIEW mv0_t1 PARALLEL 8;Check the background refresh parallelism setting for the materialized view.
SELECT OWNER, MVIEW_NAME,REFRESH_DOP FROM oceanbase.DBA_MVIEWS WHERE OWNER = 'db_test' AND MVIEW_NAME = 'mv0_t1';The return result is as follows:
+---------+------------+-------------+ | OWNER | MVIEW_NAME | REFRESH_DOP | +---------+------------+-------------+ | db_test | mv0_t1 | 8 | +---------+------------+-------------+ 1 row in set
- When the value of the
Query the DBA_MVREF_RUN_STATS view to obtain the historical background refresh parallelism of a materialized view.
Here is an example:
SELECT REFRESH_ID, MVIEWS, PARALLELISM FROM oceanbase.DBA_MVREF_RUN_STATS WHERE MVIEWS = 'db_test.mv0_t1' ORDER BY REFRESH_ID;The return result is as follows:
+------------+----------------+-------------+ | REFRESH_ID | MVIEWS | PARALLELISM | +------------+----------------+-------------+ | 4928139 | db_test.mv0_t1 | 5 | | 4928218 | db_test.mv0_t1 | 5 | | 4928310 | db_test.mv0_t1 | 8 | | 4928397 | db_test.mv0_t1 | 8 | | 4928485 | db_test.mv0_t1 | 8 | +------------+----------------+-------------+ 5 rows in set
Manually refresh a materialized view
You can use the DBMS_MVIEW package to manually refresh a materialized view whose refresh mode is set to ON DEMAND. For materialized views defined for incremental refreshes, you can specify a complete refresh when you manually refresh them.
Notes
The refresh operation can only be executed by the owner of the materialized view or the tenant administrator user.
Use the REFRESH Statement to Refresh a Materialized View
DBMS_MVIEW.REFRESH (
IN mv_name VARCHAR(65535), -- materialized view name.
IN method VARCHAR(65535) DEFAULT NULL, -- Refresh option
-- f fast refresh </think> -- f fast refresh
-- ? Forced refresh
-- C|c refresh all rows in table
-- A|a Refreshes continuously, which is equivalent to C.
IN refresh_parallel INT DEFAULT 0); -- Refresh parallelism level.
Example:
Insert data into the
test_tbl1table. The table contains three data records.INSERT INTO test_tbl1 VALUES (1, 1, 1, 1),(2, 2, 2, 2),(3, 3, 3, 3);Run the following command to view the information about the materialized view
mv1_test_tbl1.SELECT * FROM mv1_test_tbl1;The return result is as follows:
Empty setManually refresh the materialized view
mv1_test_tbl1by using theDBMS_MVIEW.REFRESHprocedure.Refresh the materialized view using the refresh option defined for the materialized view:
CALL DBMS_MVIEW.REFRESH('mv1_test_tbl1');Refresh the materialized view by specifying the refresh option:
CALL DBMS_MVIEW.REFRESH('mv1_test_tbl1', 'c');
Query the information about the materialized view
mv1_test_tbl1again.SELECT * FROM mv1_test_tbl1;The following command is returned:
+------+------+----------+----------+ | col2 | cnt | cnt_col3 | sum_col3 | +------+------+----------+----------+ | 1 | 1 | 1 | 1 | | 2 | 1 | 1 | 2 | | 3 | 1 | 1 | 3 | +------+------+----------+----------+ 3 rows in set
Adjust the parallelism of manual refresh of materialized view
You can set the default parallelism for manual refreshes of materialized views by using the mview_refresh_dop system variable.
You can also explicitly specify refresh_parallel to set the refresh degree of parallelism when calling the DBMS_MVIEW.REFRESH function.
Notice
No parallel refresh is enabled if no parallelism is explicitly specified and the value of the mview_refresh_dop variable is 0 or 1.
Example:
Set the current parallelism of the session to 5.
SET mview_refresh_dop = 5;Manually refresh the materialized view:
Set the refresh parallelism to 8 explicitly. This time, the refresh parallelism is set to 8.
CALL DBMS_MVIEW.REFRESH('mv1', 'c', 8);The parallelism for this refresh is 5, based on the session variable value, as it was not explicitly specified.
CALL DBMS_MVIEW.REFRESH('mv1', 'c');
Automatic refresh of materialized views
When you create a materialized view and specify the START WITH datetime_expr and NEXT datetime_expr clauses, the system creates a background automatic refresh task for the materialized view when the conditions are met.
Note
You can view the background refresh tasks of a materialized view in the DBA_SCHEDULER_JOBS view. For more information, see Query materialized views.
Parallelism of automatic refresh of materialized views
When a materialized view is automatically refreshed in the background, you can specify the parallelism in the following two ways:
Note
The following parallelism settings are in descending order of priority.
Specify the parallelism (Table DOP) when you create a materialized view.
Here is an example:
CREATE MATERIALIZED VIEW mv_t1 PARALLEL 8 REFRESH COMPLETE ON DEMAND START WITH sysdate() NEXT sysdate() + INTERVAL 10 SECOND AS SELECT c1, c2 FROM t1;Set the global session variable
mview_refresh_dopas the refresh parallelism.Automatic refresh operations are performed in internal sessions. To make the setting effective in the background session, you must set the global-level mview_refresh_dop variable.
Notice
If you do not explicitly specify the parallelism when you create a materialized view and the
mview_refresh_dopvariable is set to 0 or 1, the background refresh task does not enable parallel refresh.Here is an example:
Set the global session parallelism to 5.
SET GLOBAL mview_refresh_dop = 5;Create a materialized view:
If you specify the parallelism as 8 when you create a materialized view, the background refresh task uses 8 as the refresh parallelism.
CREATE MATERIALIZED VIEW mv1_t1 PARALLEL 8 REFRESH COMPLETE ON DEMAND START WITH sysdate() NEXT sysdate() + INTERVAL 10 SECOND AS SELECT c1, c2 FROM t1;If you do not specify the parallelism when you create a materialized view, the background refresh task uses the value of the
mview_refresh_dopvariable, which is 5, as the refresh parallelism.CREATE MATERIALIZED VIEW mv2_t1 REFRESH COMPLETE ON DEMAND START WITH sysdate() NEXT sysdate() + INTERVAL 10 SECOND AS SELECT c1, c2 FROM t1;
Refresh nested materialized views
Note
For OceanBase Database V4.3.5, starting from V4.3.5 BP5, you can create nested materialized views based on materialized views without a primary key.
Refresh rules for nested materialized views
The refresh methods supported by nested materialized views are the same as those supported by non-nested materialized views, including full refresh and incremental refresh. Although refreshing a nested materialized view only requires the user tables and materialized views (along with their mlogs) it directly depends on, its data consistency depends on the materialized views on which it is built. This means that to ensure the data of a nested materialized view remains up-to-date through refresh, the data of the materialized views it depends on must be refreshed first to ensure they are up-to-date.
For example, in the following figure, materialized view mv1 is built on tables tbl1 and tbl2, materialized view mv2 is built on materialized view mv1 and table tbl3, and materialized view mv3 is built on materialized views mv1 and mv2. If you refresh mv1, mv2, and mv3 in this order, the data of the nested materialized views will be consistent. However, if you refresh mv2 first and then mv1, the data of mv2 will not be up-to-date (it will lag behind mv1). Similarly, if you refresh mv3 first and then mv2, the data of mv3 will not be up-to-date (it will lag behind mv2).
Starting from OceanBase Database V4.3.5 BP3, nested materialized views in OceanBase Database V4.3.5 support cascading refresh. Cascading refresh is divided into cascading inconsistent refresh and cascading consistent refresh.
Cascading inconsistent refresh: This method refreshes all materialized views that the nested materialized view depends on from bottom to top. Each materialized view is refreshed without data consistency guarantees, and the data points read from the base tables are inconsistent. Cascading inconsistent refresh is suitable for batch synchronization scenarios, such as when a business party periodically synchronizes data from an upstream data source. After the data synchronization is complete, cascading inconsistent refresh can be used to ensure the eventual consistency of the materialized views.
Cascading consistent refresh: This method ensures that after the entire cascading refresh is completed, the data points of all base tables that the upper-level materialized views depend on are consistent. Cascading consistent refresh is suitable for real-time data synchronization scenarios. We ensure that after each cascading refresh, the data snapshots in the materialized views are at the same data point.
Nested materialized view refresh example
If a materialized view is completely refreshed, any dependent materialized views must be completely refreshed before they can be incrementally refreshed, otherwise an error will occur.
Here is an example:
Create the
tbl1table and insert one row of data.CREATE TABLE tbl1(id INT, name VARCHAR(30), PRIMARY KEY(id));INSERT INTO tbl1 VALUES (1, 'jack');Create the
tbl2table and insert one row of data.CREATE TABLE tbl2(id INT, age INT, PRIMARY KEY(id));INSERT INTO tbl2 VALUES (1, 21);Create materialized view logs on the
tbl1andtbl2tables.CREATE MATERIALIZED VIEW LOG ON tbl1 WITH PRIMARY KEY (name) INCLUDING NEW VALUES;CREATE MATERIALIZED VIEW LOG ON tbl2 WITH PRIMARY KEY (age) INCLUDING NEW VALUES;Create the
mv1materialized view based on thetbl1andtbl2tables.CREATE MATERIALIZED VIEW mv1 (PRIMARY KEY (id1, id2)) REFRESH FAST ON DEMAND AS SELECT tbl1.id id1, tbl2.id id2, tbl1.NAME, tbl2.AGE FROM tbl1, tbl2 WHERE tbl1.id = tbl2.id;Create a materialized view log on the
mv1materialized view.CREATE MATERIALIZED VIEW LOG ON mv1 WITH PRIMARY KEY (name, age) INCLUDING NEW VALUES;Create a materialized view (
mv2) based on themv1materialized view.CREATE MATERIALIZED VIEW mv2 REFRESH FAST AS SELECT COUNT(*) cnt, COUNT(AGE) age_cnt, SUM(AGE) age_sum FROM mv1;Query data from the
mv1materialized view.SELECT * FROM mv1;The return result is as follows:
+------+------+------+------+ | id1 | id2 | NAME | AGE | +------+------+------+------+ | 1 | 1 | jack | 21 | +------+------+------+------+ 1 row in setQuery data from the
mv2materialized view.SELECT * FROM mv2;The return result is as follows:
+------+---------+---------+ | cnt | age_cnt | age_sum | +------+---------+---------+ | 1 | 1 | 21 | +------+---------+---------+ 1 row in setInsert one row of data into the
tbl1andtbl2tables.INSERT INTO tbl1 VALUES (2, 'rose');INSERT INTO tbl2 VALUES (2, 19);Incrementally refresh the
mv1materialized view.CALL dbms_mview.refresh('mv1', 'f');Query data from the
mv1materialized view.SELECT * FROM mv1;The return result is as follows:
+------+------+------+------+ | id1 | id2 | NAME | AGE | +------+------+------+------+ | 1 | 1 | jack | 21 | | 2 | 2 | rose | 19 | +------+------+------+------+ 2 rows in setIncrementally refresh the
mv2materialized view.CALL dbms_mview.refresh('mv2', 'f');Query data from the
mv2materialized view.SELECT * FROM mv2;The return result is as follows:
+------+---------+---------+ | cnt | age_cnt | age_sum | +------+---------+---------+ | 2 | 2 | 40 | +------+---------+---------+ 1 row in setInsert one row of data into the
tbl1andtbl2tables.INSERT INTO tbl1 VALUES (3, 'mary');INSERT INTO tbl2 VALUES (3, 25);Completely refresh the
mv1materialized view.CALL dbms_mview.refresh('mv1', 'c');Query data from the
mv1materialized view.SELECT * FROM mv1;The return result is as follows:
+------+------+------+------+ | id1 | id2 | NAME | AGE | +------+------+------+------+ | 1 | 1 | jack | 21 | | 2 | 2 | rose | 19 | | 3 | 3 | mary | 25 |
+------+------+------+------+ 3 rows in set ```
Incrementally refresh the materialized view
mv2again.CALL dbms_mview.refresh('mv2', 'f');The return result is as follows:
ERROR 9760 (HY000): cannot fast refresh materialized viewNotice
Since
mv1has been fully refreshed, an error is returned when you incrementally refreshmv2directly. You must fully refreshmv2first.Query the data in the materialized view
mv2again.SELECT * FROM mv2;The return result is as follows:
+------+---------+---------+ | cnt | age_cnt | age_sum | +------+---------+---------+ | 2 | 2 | 40 | +------+---------+---------+ 1 row in setFully refresh the materialized view
mv2.CALL dbms_mview.refresh('mv2', 'c');Query the data in the materialized view
mv2again.SELECT * FROM mv2;The return result is as follows:
+------+---------+---------+ | cnt | age_cnt | age_sum | +------+---------+---------+ | 3 | 3 | 65 | +------+---------+---------+ 1 row in set
Real-time nested materialized view example
If a materialized view (nested materialized view) is a real-time materialized view, the incremental refresh of the lower-level materialized view must be performed to update the mlog. The query results of the real-time materialized view are obtained by merging the query results of the lower-level materialized view and itself, which are simulated by the mlog. Therefore, the incremental refresh of the lower-level materialized view must be performed to update the mlog, ensuring that the data of the real-time materialized view is the most recent.
Note
For OceanBase Database V4.3.5, the automatic management of materialized view logs is supported starting from V4.3.5 BP4. If automatic management of mlogs is enabled, you do not need to create an mlog for the base table before creating a real-time materialized view. OceanBase Database automatically creates the corresponding mlog or updates the definition of an existing mlog table to include the columns required by the newly created materialized view. For more information, see Automatic management of materialized view logs.
Here is an example:
Create a table named
tbl3.CREATE TABLE tbl3(col1 INT, col2 INT, col3 INT);Create a materialized view log on the
tbl3table.CREATE MATERIALIZED VIEW LOG ON tbl3 WITH PRIMARY KEY, ROWID, SEQUENCE (col1, col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_tbl3_1based on thetbl3table.CREATE MATERIALIZED VIEW mv_tbl3_1 ENABLE ON QUERY COMPUTATION AS SELECT col1, col2, col3, COUNT(*) AS cnt FROM tbl3 GROUP BY col1, col2, col3;Create a materialized view log on the
mv_tbl3_1materialized view.CREATE MATERIALIZED VIEW LOG ON mv_tbl3_1 WITH PRIMARY KEY, ROWID, SEQUENCE (col1, col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_tbl3_2based on themv_tbl3_1materialized view.CREATE MATERIALIZED VIEW mv_tbl3_2 ENABLE ON QUERY COMPUTATION AS SELECT col1, col2, col3, COUNT(*) AS cnt FROM mv_tbl3_1 GROUP BY col1, col2, col3;Create a materialized view log on the
mv_tbl3_2materialized view.CREATE MATERIALIZED VIEW LOG ON mv_tbl3_2 WITH PRIMARY KEY, ROWID, SEQUENCE (col1, col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_tbl3_3based on themv_tbl3_2materialized view.CREATE MATERIALIZED VIEW mv_tbl3_3 ENABLE ON QUERY COMPUTATION AS SELECT col1, col2, col3, COUNT(*) AS cnt FROM mv_tbl3_2 GROUP BY col1, col2, col3;Insert a row of data into the
tbl3table.INSERT INTO tbl3 VALUES(1, 1, 1);Query the
tbl3table.SELECT * FROM tbl3;The returned result is as follows:
+------+------+------+ | col1 | col2 | col3 | +------+------+------+ | 1 | 1 | 1 | +------+------+------+ 1 row in setQuery the
mv_tbl3_1materialized view.SELECT * FROM mv_tbl3_1;The returned result is as follows:
+------+------+------+------+ | col1 | col2 | col3 | cnt | +------+------+------+------+ | 1 | 1 | 1 | 1 | +------+------+------+------+ 1 row in setQuery the
mv_tbl3_2materialized view.SELECT * FROM mv_tbl3_2;The returned result is as follows:
Empty setQuery the
mv_tbl3_3materialized view.SELECT * FROM mv_tbl3_3;The returned result is as follows:
Empty setIncrementally refresh the
mv_tbl3_1materialized view.CALL dbms_mview.refresh('mv_tbl3_1','f');Query the
mv_tbl3_2materialized view again.SELECT * FROM mv_tbl3_2;The returned result is as follows:
+------+------+------+------+ | col1 | col2 | col3 | cnt | +------+------+------+------+ | 1 | 1 | 1 | 1 | +------+------+------+------+ 1 row in setQuery the
mv_tbl3_3materialized view again.SELECT * FROM mv_tbl3_3;The returned result is as follows:
Empty setIncrementally refresh the
mv_tbl3_2materialized view.CALL dbms_mview.refresh('mv_tbl3_2','f');View the data in the materialized view
mv_tbl3_3again.SELECT * FROM mv_tbl3_3;The returned result is as follows:
+------+------+------+------+ | col1 | col2 | col3 | cnt | +------+------+------+------+ | 1 | 1 | 1 | 1 | +------+------+------+------+ 1 row in set
View refresh statistics
OceanBase Database can collect and store statistics about view refresh operations. You can query these statistics by using specific views. Current and historical view refresh statistics are stored in the database. By analyzing historical view refresh statistics, you can understand and analyze the performance of view refreshes in the database.
The statistics about view refreshes provide the following features:
Reporting: provides an overview of current and historical statistics about view refreshes, including the actual time required for the refresh to execute, to track and monitor the refresh performance.
Diagnostics: provides detailed current and historical statistics about view refreshes to analyze the performance of view refreshes. For example, if the refresh of a view takes a long time, the statistics can help you identify whether the performance degradation is caused by an increase in system load or an increase in data changes.
Collect statistics about materialized views
You can collect statistics about materialized views by using the analyze table statement or the call dbms_stats.gather_table_stats('database_name', 'table_name') procedure.
For more information about how to collect statistics about tables and columns, see GATHER_TABLE_STATS.
For more information about how to manage the collection and retention of statistics about view refreshes, see DBMS_MVIEW_STATS overview.
View the refresh information of materialized views
| View | Description |
|---|---|
| DBA_MVIEWS | Displays the information about materialized views. |
| DBA_MVREF_STATS_SYS_DEFAULTS | Displays the system scope default values of the refresh statistics attributes of materialized views. |
| DBA_MVREF_STATS_PARAMS | Displays the refresh statistics attributes associated with each materialized view. |
| DBA_MVREF_RUN_STATS | Displays the information about each refresh run of a materialized view. Each refresh run is identified by a REFRESH_ID. |
| DBA_MVREF_STATS | Displays the basic timing statistics of materialized view refreshes. |
| DBA_MVREF_CHANGE_STATS | Displays the statistics about materialized view refreshes. |
| DBA_MVREF_STMT_STATS | Displays the information about the refresh statements. |
| DBA_SCHEDULER_JOBS | Displays the information about all scheduler jobs in the database. |