This topic describes how to create a materialized view by using SQL statements.
Note
OceanBase Database does not support modifying the properties of a materialized view, such as the refresh time and refresh strategy. In this case, you can delete and re-create the materialized view to achieve the desired modification.
Privilege requirements
To create a materialized view, you must have the CREATE TABLE privilege. For more information about privileges in OceanBase Database, see Privilege types in MySQL-compatible mode.
Syntax
The syntax for creating a materialized view is as follows:
CREATE MATERIALIZED VIEW view_name [([column_list] [PRIMARY KEY(column_list)])]
[table_option_list]
[partition_option]
[mv_column_group_option]
[refresh_clause]
[query_rewrite_clause]
[on_query_computation_clause]
AS view_select_stmt;
Parameters:
view_name: the name of the materialized view to be created.column_list: optional. The list of columns of the materialized view. If you want to specify the names of the view columns, you can use thecolumn_listclause and separate the column names with commas.PRIMARY KEY(column_list): optional. The primary key of the materialized view.table_option_list: optional. The table options of the materialized view.partition_option: optional. The partitioning options of the materialized view.mv_column_group_option: optional. The storage format of the materialized view. If you do not specify this option, a rowstore materialized view is created by default.refresh_clause: optional. The refresh method of the materialized view.query_rewrite_clause: optional. Specifies whether to enable automatic query rewriting for the materialized view.on_query_computation_clause: optional. Specifies whether the materialized view is a regular or real-time materialized view.AS view_select_stmt: the query (SELECT) statement used to define the data of the materialized view. This statement is used to retrieve data from the base table and store the results in the materialized view.Note
- OceanBase Database allows you to create a full-refresh materialized view with an external table as the base table.
- OceanBase Database allows you to add the
AS OF PROCTIME()clause to the base table when you create a materialized view. If you use theAS OF PROCTIME()clause outside the base table of the materialized view, an error is returned. TheAS OF PROCTIME()clause specifies to skip the refresh of this table during incremental refreshes. In this case, you do not need to create an mlog for the table. - OceanBase Database allows you to use a regular view declared as a dimension table (
AS OF PROCTIME()) as the base table of an incremental-refresh materialized view.
For more information about the syntax of the CREATE MATERIALIZED VIEW statement, see CREATE MATERIALIZED VIEW.
Create a materialized view
Create a regular materialized view
To create a regular materialized view, omit or specify the DISABLE ON QUERY COMPUTATION clause.
Here is an example:
Create a table
tbl1as the base table of the materialized view.CREATE TABLE tbl1 (col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT);Create a materialized view named
mv_tbl1based on thetbl1table.CREATE MATERIALIZED VIEW mv_tbl1 AS SELECT col1, col2 FROM tbl1 WHERE col3 >= 20;or
CREATE MATERIALIZED VIEW mv_tbl1 DISABLE ON QUERY COMPUTATION AS SELECT col1, col2 FROM tbl1 WHERE col3 >= 20;
Create a nested materialized view
A nested materialized view is a materialized view built on an existing materialized view. For example, in the following figure, the materialized view mv1 is built based on tables tbl1 and tbl2, which is a typical materialized view. The materialized view mv2 is built based on the materialized view mv1 and table tbl3, which is a nested materialized view. Similarly, the materialized view mv3 is built based on the materialized views mv1 and mv2, which is also a nested materialized view.
When you create a nested materialized view in OceanBase Database, you can specify a refresh strategy. Valid values are as follows:
INDIVIDUAL: The default value. This indicates independent refresh.INCONSISTENT: This indicates cascading inconsistent refresh.CONSISTENT: This indicates cascading consistent refresh.
Note
For non-nested materialized views, the cascading refresh strategy does not take effect. Regardless of which refresh strategy you specify, the default value of `INDIVIDUAL` is used. The three refresh strategies only take effect when you manually use the DBMS_MVIEW.REFRESH package to schedule a refresh. In this case, the refresh is performed based on the specified PL parameters.
Limitations on nested materialized views
- To support incremental refresh for nested materialized views, you must create an mlog on the materialized view (base table).
- If a materialized view is fully refreshed, any dependent materialized views (nested materialized views) must be fully refreshed before they can be incrementally refreshed. Otherwise, an error will be reported.
- Nested materialized views cannot be created as real-time materialized views. In other words, you cannot specify the
ENABLE ON QUERY COMPUTATIONclause when creating a nested materialized view.
Here is an example:
Create a table
tbl3as the base table of the materialized view.CREATE TABLE tbl3(id INT, name VARCHAR(30), PRIMARY KEY(id));Create a table
tbl4as the base table of the materialized view.CREATE TABLE tbl4(id INT, age INT, PRIMARY KEY(id));Create a materialized view named
mv1_tbl3_tbl4based on thetbl3andtbl4tables.CREATE MATERIALIZED VIEW mv1_tbl3_tbl4 (PRIMARY KEY (id1, id2)) REFRESH COMPLETE AS SELECT tbl3.id id1, tbl4.id id2, tbl3.name, tbl4.age FROM tbl3, tbl4 WHERE tbl3.id = tbl4.id;Create a nested materialized view named
mv_mv1_tbl3_tbl4based on themv1_tbl3_tbl4materialized view.CREATE MATERIALIZED VIEW mv_mv1_tbl3_tbl4 REFRESH COMPLETE AS SELECT SUM(AGE) age_sum FROM mv1_tbl3_tbl4;Create a nested materialized view named
mv1_mv1_tbl3_tbl4based on themv1_tbl3_tbl4materialized view, and specify theINCONSISTENTrefresh strategy.CREATE MATERIALIZED VIEW mv1_mv1_tbl3_tbl4 REFRESH COMPLETE INCONSISTENT AS SELECT SUM(AGE) age_sum FROM mv1_tbl3_tbl4;
Create a real-time materialized view
When you create a materialized view, you can specify the ENABLE ON QUERY COMPUTATION clause to create a real-time materialized view.
Considerations
Before you create a real-time materialized view, you must create a materialized view log for each base table on which the real-time materialized view depends.
Note
OceanBase Database supports automatic management of materialized view logs. If you enable automatic management of materialized view logs, you do not need to create materialized view logs for base tables before you create a real-time materialized view. OceanBase Database automatically creates the corresponding materialized view logs or updates the existing materialized view log tables to include the columns required by the new real-time materialized view. For more information, see Automatic management of materialized view logs.
Only specific types of materialized views can be specified as real-time materialized views. If you specify a materialized view that does not meet the requirements as a real-time materialized view, an error is returned. The requirements for real-time materialized views are the same as those for materialized views that support incremental refreshes. For more information, see the Basic requirements for incremental refreshes section in Refresh materialized views.
- Materialized views that use the
MINorMAXfunction cannot be real-time materialized views. - Materialized views that use outer joins cannot be real-time materialized views.
- Materialized views that use set operations cannot be real-time materialized views.
- Nested materialized views cannot be real-time materialized views.
- Materialized views that use the
If the values of system variables in the session that executes a query do not match the values of session variables that are fixed in the materialized view, you must modify the system variables in the session to match the session variables fixed in the real-time materialized view. Otherwise, the real-time materialized view is unavailable. This means that query rewriting for the real-time materialized view does not take effect, or an error is returned when you query the real-time materialized view directly.
Here is an example:
Create a base table named
tbl2.CREATE TABLE tbl2(col1 INT, col2 INT, col3 INT);Create a materialized view log on the
tbl2table.CREATE MATERIALIZED VIEW LOG ON tbl2 WITH PRIMARY KEY, ROWID, SEQUENCE (col1, col2, col3) INCLUDING NEW VALUES;Create a real-time materialized view named
mv_tbl2based on thetbl2table.CREATE MATERIALIZED VIEW mv_tbl2 ENABLE ON QUERY COMPUTATION AS SELECT col1, count(*) AS cnt FROM tbl2 GROUP BY col1;After you create the real-time materialized view, you can query the DBA_MVIEWS view to check whether the materialized view is a real-time materialized view.
SELECT MVIEW_NAME, ON_QUERY_COMPUTATION FROM oceanbase.DBA_MVIEWS WHERE MVIEW_NAME = 'mv_tbl2';The returned result is as follows:
+------------+----------------------+ | MVIEW_NAME | ON_QUERY_COMPUTATION | +------------+----------------------+ | mv_tbl2 | Y | +------------+----------------------+ 1 row in setQuery the execution plan of the real-time materialized view.
EXPLAIN BASIC SELECT * FROM mv_tbl2;From the following execution plan, you can see that data is read from the materialized view and the materialized view logs of the base tables on which the view depends during execution. The data is then calculated and integrated to obtain the real-time data of the materialized view.
The returned result is as follows:
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Query Plan | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | ============================================== | | |ID|OPERATOR |NAME | | | ---------------------------------------------- | | |0 |HASH GROUP BY | | | | |1 |└─SUBPLAN SCAN |INNER_RT_MV$$| | | |2 | └─UNION ALL | | | | |3 | ├─TABLE FULL SCAN |mv_tbl2 | | | |4 | └─HASH GROUP BY | | | | |5 | └─SUBPLAN SCAN |DLT_T$$ | | | |6 | └─WINDOW FUNCTION | | | | |7 | └─TABLE FULL SCAN|mlog$_tbl2 | | | ============================================== | | Outputs & filters: | | ------------------------------------- | | 0 - output([INNER_RT_MV$$.col1], [cast(T_FUN_SUM(INNER_RT_MV$$.cnt), BIGINT(20, 0))]), filter([T_FUN_SUM(INNER_RT_MV$$.cnt) > cast(0, DECIMAL_INT(64, | | 0))]), rowset=16 | | group([INNER_RT_MV$$.col1]), agg_func([T_FUN_SUM(INNER_RT_MV$$.cnt)]) | | 1 - output([INNER_RT_MV$$.col1], [INNER_RT_MV$$.cnt]), filter(nil), rowset=16 | | access([INNER_RT_MV$$.col1], [INNER_RT_MV$$.cnt]) | | 2 - output([UNION([1])], [UNION([2])]), filter(nil), rowset=16 | | 3 - output([mv_tbl2.col1], [cast(mv_tbl2.cnt, DECIMAL_INT(42, 0))]), filter(nil), rowset=16 | | access([mv_tbl2.col1], [mv_tbl2.cnt]), partitions(p0) | | is_index_back=false, is_global_index=false, | | range_key([mv_tbl2.__pk_increment]), range(MIN ; MAX)always true | | 4 - output([DLT_T$$.col1], [T_FUN_SUM(CASE WHEN DLT_T$$.OLD_NEW$$ = 'N' THEN 1 ELSE -1 END)]), filter(nil), rowset=16 | | group([DLT_T$$.col1]), agg_func([T_FUN_SUM(CASE WHEN DLT_T$$.OLD_NEW$$ = 'N' THEN 1 ELSE -1 END)]) | | 5 - output([DLT_T$$.OLD_NEW$$], [DLT_T$$.col1]), filter([DLT_T$$.OLD_NEW$$ = 'N' AND DLT_T$$.SEQUENCE$$ = DLT_T$$.MAXSEQ$$ OR DLT_T$$.OLD_NEW$$ = 'O' | | AND DLT_T$$.SEQUENCE$$ = DLT_T$$.MINSEQ$$]), rowset=16 | | access([DLT_T$$.OLD_NEW$$], [DLT_T$$.SEQUENCE$$], [DLT_T$$.MAXSEQ$$], [DLT_T$$.MINSEQ$$], [DLT_T$$.col1]) | | 6 - output([mlog$_tbl2.OLD_NEW$$], [mlog$_tbl2.SEQUENCE$$], [T_FUN_MAX(mlog$_tbl2.SEQUENCE$$)], [T_FUN_MIN(mlog$_tbl2.SEQUENCE$$)], [mlog$_tbl2.col1]), filter(nil), rowset=16 | | win_expr(T_FUN_MAX(mlog$_tbl2.SEQUENCE$$)), partition_by([mlog$_tbl2.M_ROW$$]), order_by(nil), window_type(RANGE), upper(UNBOUNDED PRECEDING), lower(UNBOUNDED | | FOLLOWING) | | win_expr(T_FUN_MIN(mlog$_tbl2.SEQUENCE$$)), partition_by([mlog$_tbl2.M_ROW$$]), order_by(nil), window_type(RANGE), upper(UNBOUNDED PRECEDING), lower(UNBOUNDED | | FOLLOWING) | | 7 - output([mlog$_tbl2.M_ROW$$], [mlog$_tbl2.SEQUENCE$$], [mlog$_tbl2.OLD_NEW$$], [mlog$_tbl2.col1], [ORA_ROWSCN]), filter([ORA_ROWSCN > last_refresh_scn(500452)]), rowset=16 | | access([mlog$_tbl2.M_ROW$$], [mlog$_tbl2.SEQUENCE$$], [mlog$_tbl2.OLD_NEW$$], [mlog$_tbl2.col1], [ORA_ROWSCN]), partitions(p0) | | is_index_back=false, is_global_index=false, filter_before_indexback[false], | | range_key([mlog$_tbl2.M_ROW$$], [mlog$_tbl2.SEQUENCE$$]), range(MIN,MIN ; MAX,MAX)always true | +----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 38 rows in set
Create a materialized view that supports query rewriting
When you create a materialized view, you can specify the ENABLE QUERY REWRITE clause to enable query rewriting for the materialized view. For more information about materialized view rewriting and rewriting control, see Materialized view query rewriting.
Notice
Specifying the ENABLE QUERY REWRITE clause for a materialized view does not guarantee that queries are rewritten for the materialized view. If a materialized view does not meet the query rewriting conditions, no error is returned, but the materialized view is not used for query rewriting. By default, the value of the system variable query_rewrite_enabled is false, which means that materialized views that specify the ENABLE QUERY REWRITE clause are not used for query rewriting by default.
Here is an example:
Create a materialized view named
mv_spj_tbl1based on thetbl1table and enable query rewriting.CREATE MATERIALIZED VIEW mv_spj_tbl1 ENABLE QUERY REWRITE AS SELECT * FROM tbl1;After you create the materialized view, you can query the DBA_MVIEWS view to check whether query rewriting is enabled for the materialized view.
SELECT MVIEW_NAME, REWRITE_ENABLED FROM oceanbase.DBA_MVIEWS WHERE MVIEW_NAME = 'mv_spj_tbl1';The returned result is as follows:
+-------------+-----------------+ | MVIEW_NAME | REWRITE_ENABLED | +-------------+-----------------+ | mv_spj_tbl1 | Y | +-------------+-----------------+ 1 row in set
Create a columnstore materialized view
OceanBase Database supports materialized views in rowstore, columnstore, and rowstore-columnstore redundant formats. You can specify the mv_column_group_option option to create a columnstore or rowstore-columnstore redundant materialized view. If a materialized view is a wide table formed by joining multiple tables, creating a columnstore materialized view can improve the performance of some queries. You can specify WITH COLUMN GROUP(each column) to create a columnstore materialized view.
Note
If you do not specify the mv_column_group_option option, a rowstore materialized view is created by default.
Here is an example:
Create a columnstore materialized view named mv_ec_tbl1 based on the tbl1 table.
CREATE MATERIALIZED VIEW mv_ec_tbl1
WITH COLUMN GROUP(each column)
AS SELECT *
FROM tbl1;
Specify a primary key when you create a materialized view
Notice
If you specify a primary key for a materialized view, the view maintenance or update fails if the data does not meet the primary key constraints.
Here is an example:
Create a materialized view named mv_pk_tbl1 based on the tbl1 table and specify a primary key for the materialized view.
CREATE MATERIALIZED VIEW mv_pk_tbl1(v_id, v_name, PRIMARY KEY(v_id))
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;
Specify table and partition options when you create a materialized view
When you create a materialized view, you can specify table options and partition options. You can design and configure partition options based on the data characteristics and access patterns to improve query performance and management efficiency.
For more information about the table and partition options, see CREATE TABLE.
Here is an example:
Create a materialized view named mv_pp_tbl1 based on the tbl1 table. Set the parallelism of the materialized view to 5. Hash partition the materialized view by the col1 column and divide it into 8 partitions. Query the records in the tbl1 table that meet the condition col3 >= 20 as the base table and use the query results as the data of the materialized view.
CREATE MATERIALIZED VIEW mv_pp_tbl1
PARALLEL 5
PARTITION BY HASH(col1) PARTITIONS 8
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;
Add an index to a materialized view
You cannot directly create an index in the statement for creating a materialized view. You can use the CREATE INDEX or ALTER TABLE statement to create an index for a materialized view.
Here are some examples:
Create an index named
idx1_mv_tbl1on thecol1column of the materialized viewmv_tbl1.CREATE INDEX idx1_mv_tbl1 ON mv_tbl1(col1);Create an index named
idx2_mv_tbl1on thecol2column of the materialized viewmv_tbl1.ALTER TABLE mv_tbl1 ADD INDEX idx2_mv_tbl1(col2);
Refresh materialized views
OceanBase Database supports four refresh strategies for materialized views: full refresh, incremental refresh, hybrid refresh, and never refresh. The following table describes these strategies:
- Full refresh: Recalculate all data in the materialized view to ensure that the data in the view is consistent with that in the source table.
- Incremental refresh: Refresh only the data that is related to changes in the source table to avoid recalculating the entire view.
- Hybrid refresh: The default strategy. First, an incremental refresh is attempted. If the incremental refresh fails, a full refresh is performed.
- Never refresh: The materialized view is refreshed only when it is created. It cannot be refreshed again after it is created.
For more information about how to refresh a materialized view, see Refresh a materialized view.
Create a materialized view with a complete refresh strategy
When you create a materialized view, you can use the REFRESH COMPLETE clause to set the refresh strategy to complete refresh.
Notice
If a materialized view is completely refreshed, the dependent materialized views (nested materialized views) must also be completely refreshed before they can be incrementally refreshed. Otherwise, an error will occur.
Here is an example:
Create a materialized view named mv_rc_tbl1 based on the tbl1 table, set the refresh strategy to complete refresh (REFRESH COMPLETE), and specify the col1 and col2 columns in the tbl1 table that satisfy the condition col3 >= 20 as the data source for the materialized view.
CREATE MATERIALIZED VIEW mv_rc_tbl1
REFRESH COMPLETE
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;
Create a materialized view with a complete refresh strategy based on an external table
OceanBase Database allows you to create a materialized view with a complete refresh strategy based on an external table.
For more information about external tables, see About external tables.
Here is an example:
Notice
The IP addresses in the example have been desensitized. When verifying, replace them with the actual IP addresses of your machine.
The following example creates an external table based on an external file located on the local machine and in the MySQL-compatible mode of OceanBase Database. The steps are as follows:
Prepare the external file.
Execute the following command to create a file named
ext_tbl1.csvin the/home/admindirectory on the machine where the OBServer node is located.[admin@xxx /home/admin]# vi ext_tbl1.csvThe content of the file is as follows:
1,'A1','2025-01-01' 2,'A2','2025-02-01' 3,'A3','2025-03-01'Set the import file path.
Notice
For security reasons, when you set the system variable
secure_file_priv, you can only modify the global variable by executing an SQL statement through a local socket connection. For more information, see secure_file_priv.Execute the following command to log in to the machine where the OBServer node is located.
ssh admin@10.10.10.1Execute the following command to connect to the
mysql001tenant through a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Execute the following SQL statement to set the import path to
/home/admin.SET GLOBAL secure_file_priv = "/home/admin";
Reconnect to the
mysql001tenant.Here is an example:
obclient -h10.10.10.1 -P2881 -uroot@mysql001 -p****** -A -Ddb_testCreate an external table named
ext_tbl1.CREATE EXTERNAL TABLE ext_tbl1 ( id INT, name VARCHAR(50), c_date DATE ) LOCATION = '/home/admin' FORMAT = ( TYPE = 'CSV' FIELD_DELIMITER = ',' FIELD_OPTIONALLY_ENCLOSED_BY ='\'' ) PATTERN = 'ext_tbl1.csv';Create a materialized view named
mv_ext_tbl1with a complete refresh strategy based on the external tableext_tbl1.CREATE MATERIALIZED VIEW mv_ext_tbl1 REFRESH COMPLETE AS SELECT * FROM ext_tbl1;Query the data in the materialized view
mv_ext_tbl1.SELECT * FROM mv_ext_tbl1;The return result is as follows:
+------+------+------------+ | id | name | c_date | +------+------+------------+ | 1 | A1 | 2025-01-01 | | 3 | A3 | 2025-03-01 | | 2 | A2 | 2025-02-01 | +------+------+------------+ 3 rows in set
Create a materialized view with incremental refresh
When you create a materialized view, use the REFRESH FAST clause to set the refresh strategy of the materialized view to incremental refresh.
Considerations
At present, incremental refresh is supported for materialized views that are created by using SQL statements in the following five categories: non-aggregated single-table statements, aggregated single-table statements, multi-table join statements, aggregated multi-table join statements, and
UNION ALLstatements. For SQL statements that do not fall into these five categories, incremental refresh is not supported. For more information about the requirements for incremental refresh, see the Incremental refresh section in Refresh a materialized view.The
REFRESH FASTmethod uses the records in the materialized view log to determine the content to be incrementally refreshed. Therefore, when you incrementally refresh a materialized view, you must create a materialized view log for the base table before you create the materialized view. For more information about how to create a materialized view log, see Materialized view log.Note
OceanBase Database supports automatic management of materialized view logs. If you enable automatic management of materialized view logs, you do not need to create a materialized view log for the base table before you create an incrementally refreshed materialized view. OceanBase Database automatically creates the corresponding materialized view log or updates the definition of the existing materialized view log to include the columns required by the newly created materialized view. For more information, see Automatic management of materialized view logs.
When you create a materialized view, you can add the
AS OF PROCTIME()clause to the base table. If you useAS OF PROCTIME()outside the base table, an error is returned.AS OF PROCTIME()specifies that the incremental refresh of the materialized view skips the table specified byAS OF PROCTIME(), thereby accelerating the incremental refresh of the materialized view. You do not need to create a materialized view log for the table specified byAS OF PROCTIME(). If you want to use an alias for the table, you must specify the alias after theAS OF PROCTIME()clause.When you use a regular view declared as a dimension table (that is, with the
AS OF PROCTIME()clause) as the base table of an incrementally refreshed materialized view, the following limitations apply:- Not all tables in the materialized view can be dimension tables, just like the base table of the materialized view cannot be a dimension table.
Here is an example:
Create a table named
tbl5as the base table of the materialized view.CREATE TABLE tbl5 (col1 INT PRIMARY KEY, col2 INT, col3 INT);Create a materialized view log for the
tbl5table and specify theSEQUENCEoption for the materialized view log. This option indicates that the changes are identified by sequence numbers. Specify thecol2andcol3columns to be recorded in the materialized view log.CREATE MATERIALIZED VIEW LOG ON tbl5 WITH SEQUENCE (col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_tbl5based on thetbl5table, and set the refresh strategy of the materialized view to incremental refresh. In the query part, group the records in thetbl5table by thecol2column and calculate the number of records in each group (cnt), the number of non-null records in thecol3column (cnt_col3), and the sum of thecol3column (sum_col3) as the results of the materialized view.CREATE MATERIALIZED VIEW mv_tbl5 REFRESH FAST AS SELECT col2, COUNT(*) cnt, COUNT(col3) cnt_col3, SUM(col3) sum_col3 FROM tbl5 GROUP BY col2;Create a materialized view named
mv2_tbl5_tbl1based on thetbl5andtbl1tables, and set the refresh strategy of the materialized view to incremental refresh. Join thetbl5andtbl1tables by using thecol1column. Use theAS OF PROCTIME()clause to specify that the incremental refresh of the materialized view skips thetbl1table.CREATE MATERIALIZED VIEW mv2_tbl5_tbl1 REFRESH FAST ON DEMAND AS SELECT t5.col1 tbl5_c1, t1.col1 tbl1_c1, t5.col2 tbl5_c2, t1.col2 tbl1_c2 FROM tbl5 t5 INNER JOIN tbl1 AS OF PROCTIME() t1 ON t5.col1 = t1.col1 WHERE t5.col2 = 3;Create an incrementally refreshed materialized view based on a regular view that is declared as a dimension table (that is, with the
AS OF PROCTIME()clause).Create a view named
v1_tbl5based on thetbl5table.obclient> CREATE VIEW v1_tbl5 AS SELECT * FROM tbl5;Create a materialized view named
mv3_tbl5_v_tbl5based on thetbl5andv1_tbl5tables, and set the refresh strategy of the materialized view to incremental refresh. Join thetbl5andv1_tbl5tables by using thecol1column. Use theAS OF PROCTIME()clause to specify that thev1_tbl5view is a dimension table.obclient> CREATE MATERIALIZED VIEW mv3_tbl5_v_tbl5 AS SELECT a.col1 a_c1, b.col1 b_c1 FROM tbl5 a JOIN v1_tbl5 AS OF PROCTIME() b ON a.col1 = b.col1;
Create a materialized view with hybrid refresh (default option)
When you create a materialized view, omit the REFRESH FORCE clause or specify it to set the refresh strategy of the materialized view to hybrid refresh.
Here is an example:
Create a materialized view named mv_rf_tbl1 based on the tbl1 table, and set the refresh strategy of the materialized view to hybrid refresh. Specify the col1 and col2 columns that meet the col3 >= 20 condition as the data source of the materialized view.
CREATE MATERIALIZED VIEW mv_rf_tbl1
REFRESH FORCE
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;
Create a never-refreshed materialized view
When you create a materialized view, use the NEVER REFRESH clause to specify that the materialized view does not need to be refreshed. This indicates that the materialized view is only refreshed when it is created and cannot be refreshed again after it is created.
Here is an example:
Create a materialized view named mv_nr_tbl1 based on the tbl1 table, and set the refresh strategy of the materialized view to never-refreshed. Specify the col1 and col2 columns that meet the col3 >= 20 condition as the data source of the materialized view.
CREATE MATERIALIZED VIEW mv_nr_tbl1
NEVER REFRESH
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;
Create a materialized view with automatic refresh
When you create a materialized view, you can specify the START WITH datetime_expr and NEXT datetime_expr clauses to create a background automatic refresh task for the materialized view.
Notice
If you use the NEXT clause, the time expression in the refresh schedule must be set to a future time, otherwise an error will be triggered.
Here is an example:
Create a materialized view named mv_rc_swn_tbl1 based on the tbl1 table. Specify the refresh strategy as a full refresh, and set the initial refresh time to the current date. Then, set the refresh schedule to refresh the materialized view every 1 day.
CREATE MATERIALIZED VIEW mv_rc_swn_tbl1
REFRESH COMPLETE
START WITH sysdate() NEXT sysdate() + interval 1 day
AS SELECT col1, col2
FROM tbl1
WHERE col3 >= 20;