Purpose
You can use this statement to create a materialized view log.
A materialized view log (mlog) records the incremental data updates of a user table, that is the base table of the materialized view, to support quick refresh of the materialized view. An mlog is a record table that tracks changes to the base table and applies these changes to the corresponding materialized view to achieve quick refresh of the materialized view.
Note
OceanBase Database does not support partitioning of an mlog. The partitions of an mlog are bound to those of the base table.
Required privileges
To create a materialized view log, you must have the CREATE TABLE privilege and the SELECT privilege on the base table. For more information about privileges in OceanBase Database, see Privilege types in Oracle mode.
Syntax
CREATE MATERIALIZED VIEW LOG ON [schema.] table [parallel_clause] [with_clause] [mv_log_purge_clause];
parallel_clause:
NOPARALLEL
| PARALLEL integer
with_clause:
WITH [ {PRIMARY KEY | ROWID | SEQUENCE}
[ { , PRIMARY KEY | , ROWID | , SEQUENCE }]... ]
(column_name [, column_name]...)
[new_values_clause]
new_values_clause:
{INCLUDING | EXCLUDING} NEW VALUES
mv_log_purge_clause:
PURGE {IMMEDIATE [ SYNCHRONOUS ]
| START WITH datetime_expr [NEXT datetime_expr]
| [START WITH datetime_expr] NEXT datetime_expr
}
Parameters
| Parameter | Description |
|---|---|
| schema. | The schema where the base table of the materialized view log is located. If you do not specify the schema. parameter, the system assumes by default that the base table resides in your own schema. |
| table | The name of the base table of the materialized view log. |
| parallel_clause | Optional. The degree of parallelism (DOP) for processing materialized view logs. For more information, see parallel_clause. |
| with_clause | Optional. The columns attached to the materialized view log. Specifies whether to record the primary key and the row ID when the base table changes. You can also use this clause to add a sequence to provide additional sorting information for the materialized view log. For more information, see with_clause. |
| column_name | Optional. The name of the column that records the changed rows of the base table in the materialized view log. |
| new_values_clause | Optional. Specifies whether to record both the old and new values before and after a data change in the materialized view log. For more information, see new_values_clause. |
| mv_log_purge_clause | Optional. The time to purge the data in the materialized view log. For more information, see mv_log_purge_clause. |
parallel_clause
NOPARALLEL: sets the DOP to1, which is the default value.PARALLEL integer: sets the DOP to an integer greater than or equal to1.
with_clause
PRIMARY KEY: specifies to record the primary key column of the base table in the materialized view log. This attribute takes effect only on a table with a primary key. If you do not specify this attribute, the system automatically adds this attribute to the materialized view log. In other words, the materialized view log contains the primary key column by default.ROWID: specifies to record the row ID of the base table in the materialized view log. If you do not specify this attribute, the system automatically adds this attribute to the materialized view log. In other words, the materialized view log contains theM_ROW$$column by default.SEQUENCE: specifies to record the sequential numbers of multiple rows updated in a transaction in the materialized view log. If you do not specify this attribute, the system automatically adds this attribute to the materialized view log. In other words, the materialized view log contains theSEQUENCE$$column by default.
new_values_clause
INCLUDING: the default value, which specifies to record old and new values in the materialized view log. To perform the fast refresh of the materialized view, you must specifyINCLUDING NEW VALUES.EXCLUDING: specifies not to record new values in the materialized view log. Do not useEXCLUDING NEW VALUES. Otherwise, an error will be reported.
mv_log_purge_clause
Notice
The corresponding data in the materialized view log is cleared only after the materialized view is refreshed.
IMMEDIATE: specifies to clear the corresponding materialized view log immediately after each refresh of the materialized view. The default value isSYNCHRONOUS.SYNCHRONOUS: Optional. Specifies to perform cleanup synchronously.
START WITH datetime_expr [NEXT datetime_expr]:START WITH datetime_expr: the time to clear the materialized view log for the first time.[NEXT datetime_expr]: Optional. The time to clear the materialized view log the next time.
[START WITH datetime_expr] NEXT datetime_expr: If you specify onlyNEXT datetime_expr, the materialized view log is cleared for the first time at the time specified byNEXT datetime_expr.
Notice
Make sure START WITH datetime_expr and NEXT datetime_expr are set to future points in time. Otherwise, an error will occur.
We recommend that you use current_date to indicate the current time in the time zone. Here are some examples of the time expression:
The following clause specifies to clear the expired materialized view logs once every 10 seconds, starting from the current time.
START WITH current_date NEXT current_date + INTERVAL '10' SECONDThe following clause specifies to clear the expired materialized view logs once every 10 hours, starting from the current time.
START WITH current_date NEXT current_date + INTERVAL '10' HOURThe following clause specifies to clear the expired materialized view logs once every day, starting from the current time.
START WITH current_date NEXT current_date + 1
Examples
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (id NUMBER PRIMARY KEY, name VARCHAR2(20), age NUMBER);Create a materialized view log on the
test_tbl1table. Set the DOP of the materialized view log to5. Specify to record data changes to thenameandagecolumns in the base table to the materialized view, including the old and new values before and after the change. Specify to clear expired log data once a day, starting from the current day.CREATE MATERIALIZED VIEW LOG ON test_tbl1 PARALLEL 5 WITH SEQUENCE(name ,age) INCLUDING NEW VALUES PURGE START WITH current_date NEXT current_date + 1;Query the information of the materialized view log on the
test_tbl1table.DESC mlog$_test_tbl1;The return result is as follows:
+------------+--------------+------+------+---------+-------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +------------+--------------+------+------+---------+-------+ | ID | NUMBER | NO | PRI | NULL | NULL | | NAME | VARCHAR2(20) | YES | NULL | NULL | NULL | | AGE | NUMBER | YES | NULL | NULL | NULL | | SEQUENCE$$ | BIGINT | NO | PRI | NULL | NULL | | DMLTYPE$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | | OLD_NEW$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | +------------+--------------+------+------+---------+-------+ 6 rows in set