Purpose
You can use this statement to create a materialized view.
A materialized view is a special database object that stores the latest copy of query results and is periodically or manually refreshed. Materialized views can contain operations such as aggregation and join operations, and subqueries, and can be indexed and partitioned to further enhance the query performance.
Required privileges
You have the CREATE TABLE privilege. For more information about privileges in OceanBase Database, see Privilege types in MySQL mode.
Syntax
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;
column_list:
column_name [, column_name ...]
refresh_clause:
REFRESH [COMPLETE | FAST | FORCE] [mv_refresh_on_clause]
| NEVER REFRESH
mv_refresh_on_clause:
[ON DEMAND] [[START WITH expr] [NEXT expr]]
query_rewrite_clause:
DISABLE QUERY REWRITE
| ENABLE QUERY REWRITE
on_query_computation_clause:
DISABLE ON QUERY COMPUTATION
| ENABLE ON QUERY COMPUTATION
mv_column_group_option:
WITH COLUMN GROUP(all columns)
| WITH COLUMN GROUP(each column)
| WITH COLUMN GROUP(all columns, each column)
Parameters
Note
You cannot directly create an index by using the statement that creates a materialized view. You can use the CREATE INDEX or ALTER TABLE statement to create an index for a materialized view.
| Parameter | Description |
|---|---|
| view_name | The name of the materialized view to be created. |
| column_list | Optional. The list of columns in the materialized view. You can use the column_list clause to specify column names for the view and separate them with commas (,).
Note
|
| column_name | The name of a column in the materialized view. By default, the column names retrieved by the SELECT statement are used as the column names in the view. |
| PRIMARY KEY | The primary key of the materialized view.
Notice
|
| table_option_list | Optional. The table options of the materialized view. You can separately set table options for a materialized view just like you do for a normal table. For more information about the parameters, see CREATE TABLE. |
| partition_option | Optional. The partitioning options for the materialized view. You can separately set partitioning options for a materialized view just like you do for a normal table. For more information about the parameters, see CREATE TABLE. |
| mv_column_group_option | Optional. The storage format of the materialized view. If you do not specify the mv_column_group_option option, a rowstore materialized view is created by default. For more information, see mv_column_group_option. |
| refresh_clause | Optional. The method for refreshing the materialized view. For more information, see refresh_clause. |
| mv_refresh_on_clause | Optional. The refresh mode of the materialized view. For more information, see mv_refresh_on_clause. |
| query_rewrite_clause | Optional. Specifies whether to enable automatic query rewrite based on the materialized view. For more information, see query_rewrite_clause. |
| on_query_computation_clause | Optional. Specifies whether the current materialized view is a real-time materialized view. For more information, see on_query_computation_clause. |
| view_select_stmt | The SELECT statement that defines data in the materialized view. You use this statement to retrieve data from a base table and store the results in the materialized view. The syntax of view_select_stmt is the same as that of a regular SELECT statement. For more information about the syntax, see SELECT statement. |
mv_column_group_option
WITH COLUMN GROUP(all columns): specifies to create a rowstore materialized view.WITH COLUMN GROUP(each column): specifies to create a columnstore materialized view.WITH COLUMN GROUP(all columns, each column): specifies to create a hybrid rowstore-columnstore materialized view.
Note
You can use the SHOW CREATE TABLE view_name; or SHOW CREATE VIEW view_name; statement to query the definition of a materialized view and determine the storage format of the materialized view.
refresh_clause
REFRESH [FAST | COMPLETE | FORCE] [mv_refresh_on_clause]: specifies the method for refreshing the materialized view.[COMPLETE | FAST | FORCE]: the refresh method of the materialized view. If you do not specify a refresh method,FORCEis used by default.COMPLETE: specifies to perform a complete refresh. In this method, OceanBase Database recalculates the data of the entire materialized view to ensure data consistency with the base table.FAST: specifies to perform an incremental refresh. In this method, OceanBase Database refreshes only the data related to changes in the base table.Notice
This method determines the content for an incremental refresh based on the records in the materialized view log. Therefore, to support incremental refreshes of a materialized view, you need to create a materialized view log of the base table before you create the materialized view. For information about how to create a materialized view log, see Create a materialized view log.
FORCE: specifies to perform a hybrid refresh. In this method, OceanBase Database performs an incremental refresh first. If the incremental refresh fails, OceanBase Database performs a complete refresh. The hybrid refresh method is used by default.
NEVER REFRESH: specifies to disable refresh for the materialized view. If you specify theNEVER REFRESHclause, the materialized view is refreshed only when it is created, and will not be refreshed again.
mv_refresh_on_clause
Note
In addition to defining the refresh schedule by using the mv_refresh_on_clause clause, you can also call the dbms_mviews.refresh stored procedure to manually refresh a materialized view.
ON DEMAND: specifies to refresh the materialized view on demand. This option is optional.[[START WITH expr] [NEXT expr]]: the refresh interval. This option is optional.[START WITH expr]: the time expression that defines when the refresh schedule begins. This option is optional.[NEXT expr]: the time expression that defines when the next refresh starts. This option is optional.Notice
- If you use the
NEXTclause, the time expressions of the refresh schedule must be set to future points in time. Otherwise, an error will occur. - If you want to periodically schedule a refresh task for a materialized view, you can use the
NEXTclause to specify a scheduling interval.
- If you use the
We recommend that you use sysdate() to indicate the current time in the time zone. Here is an example of a time expression:
START WITH sysdate() NEXT sysdate() + INTERVAL 1 DAY
The preceding clause specifies to refresh the materialized view once every other day, starting from the current time.
query_rewrite_clause
Notice
This feature is available only when the materialized view contains only the SELECT JOIN and WHERE clauses. In other words, materialized view-based automatic rewriting is supported only for select project join (SPJ) queries. If a matching materialized view does not meet the requirements, it will not be used for query rewriting, but the system does not return an error.
DISABLE QUERY REWRITE: specifies to disable automatic SQL query rewrite based on the materialized view. This is the default value.ENABLE QUERY REWRITE: specifies to enable automatic SQL query rewrite based on the materialized view.
For more information about automatic SQL query rewrite for materialized views, see Rewrite queries based on materialized views.
on_query_computation_clause
DISABLE ON QUERY COMPUTATION: specifies to create a normal materialized view. This is the default value.ENABLE ON QUERY COMPUTATION: specifies to create a real-time materialized view
For more information about real-time materialized views, see the Create a real-time materialized view section in the Create a materialized view topic.
Examples
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT, col4 INT);Create a materialized view named
mv_test_tbl1. HASH-partition the materialized view by thecol1column into eight partitions. Specify to refresh the materialized view once every day by using the complete refresh method, starting from the current date. Query thetest_tbl1base table for data that meets thecol3 >= 30condition and record the query result in the materialized view.CREATE MATERIALIZED VIEW mv_test_tbl1 PARTITION BY HASH(col1) PARTITIONS 8 REFRESH COMPLETE START WITH sysdate() NEXT sysdate() + INTERVAL 1 DAY AS SELECT col1, col2, col3 FROM test_tbl1 WHERE col3 >= 30;Create a materialized view named
mv2_test_tbl1and set the primary key.CREATE MATERIALIZED VIEW mv2_test_tbl1(c1, c2, c3, PRIMARY KEY(c1)) AS SELECT col1, col2, col3 FROM test_tbl1;Create a columnstore materialized view named
mv3_test_tbl1.CREATE MATERIALIZED VIEW mv3_test_tbl1 WITH COLUMN GROUP(each column) AS SELECT col1, col2, col3 FROM test_tbl1;Query the definition of the materialized view named
mv3_test_tbl1.SHOW CREATE VIEW mv3_test_tbl1;