Purpose
This statement is used to create a materialized view log.
A materialized view log (mlog) records incremental updates to the base table (a regular table or a materialized view) to support fast refreshes of materialized views. An mlog is a record table that tracks changes to the base table and applies these changes to the corresponding materialized view, enabling fast refreshes.
Note
In OceanBase Database, the mlog table does not support specifying partitions. The partitions of the mlog table are bound to the partitions of the base table.
Privilege requirements
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 [OR REPLACE] 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 |
|---|---|
| OR REPLACE | Optional. Specifies OR REPLACE to create a new mlog with the same name as an existing mlog and replace the existing mlog with the new one. During the recreation of the mlog, the normal read and write operations on the materialized view and base table are not affected.
NoteStarting from OceanBase Database V4.3.5 BP3, the |
| schema. | Optional. Specifies the schema of the base table of the materialized view log. If you omit schema., the base table is assumed to be in your own schema. |
| table | Specifies the name of the base table of the materialized view log. |
| parallel_clause | Optional. Specifies the degree of parallelism (dop) for creating the materialized view log. For more information, see parallel_clause below. |
| with_clause | Optional. Specifies the auxiliary columns to include in the materialized view log. It indicates whether to record the primary key (PRIMARY KEY) and row identifier (ROWID) of the base table when it changes. You can also use this clause to add a sequence (SEQUENCE) to provide additional sorting information for the materialized view log. For more information, see with_clause below. |
| column_name | Optional. Specifies the name of the column to record the values of all modified rows in the materialized view log. |
| new_values_clause | Optional. Specifies whether to record both the old and new values in the materialized view log for update operations. For more information, see new_values_clause below. |
| mv_log_purge_clause | Optional. Specifies the time when data in the materialized view log is purged. For more information, see mv_log_purge_clause below. |
parallel_clause
NOPARALLEL: The default value. The degree of parallelism is1.PARALLEL integer: Specifies the degree of parallelism. The value ofintegermust be greater than or equal to1.
with_clause
PRIMARY KEY: Indicates that the materialized view log records the primary key columns of the base table. This option is effective only for tables with a primary key. If you do not specify this option, the system automatically adds this attribute to the materialized view log, meaning that the materialized view log will default to including the primary key columns.ROWID: Indicates that the materialized view log records the row identifiers (ROWID) of the base table. If you do not specify this option, the system automatically adds this attribute to the materialized view log, meaning that the materialized view log will default to including theM_ROW$$column.SEQUENCE: Indicates that the materialized view log records the sequence numbers (seq_no) of multiple rows updated in a transaction. The system automatically adds this attribute to the materialized view log, meaning that the materialized view log will default to including theSEQUENCE$$column.
new_values_clause
INCLUDING: The default value. Indicates that both the old and new values can be recorded in the materialized view log. If you want to support fast refreshes for the materialized view, you must specifyINCLUDING NEW VALUES.EXCLUDING: Indicates that new values are not recorded in the materialized view log. Do not useEXCLUDING NEW VALUES, or an error will occur.
mv_log_purge_clause
Notice
The data in the materialized view log will be purged only after the materialized view is successfully refreshed.
IMMEDIATE: Indicates that the materialized view log is purged immediately after each refresh. The default value isSYNCHRONOUS.SYNCHRONOUS: Optional. Indicates that the purge is performed synchronously.
START WITH datetime_expr [NEXT datetime_expr]:START WITH datetime_expr: Indicates the initial purge time for the materialized view log.[NEXT datetime_expr]: Optional. Indicates the next purge time for the materialized view log. It is used to set the next purge time.
[START WITH datetime_expr] NEXT datetime_expr: If you specify only theNEXT datetime_exprparameter and omit theSTART WITH datetime_exprparameter, the initial purge time of the materialized view log is set to the value of theNEXT datetime_exprparameter.
Notice
Make sure that the START WITH datetime_expr and NEXT datetime_expr expressions are set to future dates and times. Otherwise, an error will occur.
We recommend that you use current_date to represent the current time in the current time zone. Here are some examples of time expressions:
Purge expired materialized view log records every 10 seconds starting from the current time (
current_date).START WITH current_date NEXT current_date + INTERVAL '10' SECONDPurge expired materialized view log records every 10 hours starting from the current time (
current_date).START WITH current_date NEXT current_date + INTERVAL '10' HOURPurge expired materialized view log records every 1 day starting from the current time (
current_date).START WITH current_date NEXT current_date + 1
Examples
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 NUMBER PRIMARY KEY, col2 VARCHAR2(20), col3 NUMBER);Create a materialized view log on the
test_tbl1table. Set the parallelism for processing the materialized view log to5, and specify that the log records changes to thecol2column, including both the old and new values. Configure the materialized view log to start from the current date and to clean up expired records every1day.CREATE MATERIALIZED VIEW LOG ON test_tbl1 PARALLEL 5 WITH SEQUENCE(col2) INCLUDING NEW VALUES PURGE START WITH current_date NEXT current_date + 1;View the information about the materialized view log on the
test_tbl1table.DESC mlog$_test_tbl1;The returned result is as follows:
+------------+--------------+------+------+---------+-------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +------------+--------------+------+------+---------+-------+ | COL1 | NUMBER | NO | PRI | NULL | NULL | | COL2 | VARCHAR2(20) | YES | NULL | NULL | NULL | | SEQUENCE$$ | BIGINT(20) | NO | PRI | NULL | NULL | | DMLTYPE$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | | OLD_NEW$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | +------------+--------------+------+------+---------+-------+ 5 rows in setChange the materialized view log on the
test_tbl1table to record changes to thecol2andcol3columns.CREATE OR REPLACE MATERIALIZED VIEW LOG ON test_tbl1 PARALLEL 5 WITH SEQUENCE(col2, col3) INCLUDING NEW VALUES PURGE START WITH current_date NEXT current_date + 1;View the information about the materialized view log on the
test_tbl1table.DESC mlog$_test_tbl1;The returned result is as follows:
+------------+--------------+------+------+---------+-------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +------------+--------------+------+------+---------+-------+ | COL1 | NUMBER | NO | PRI | NULL | NULL | | COL2 | VARCHAR2(20) | YES | NULL | NULL | NULL | | COL3 | NUMBER | YES | NULL | NULL | NULL | | SEQUENCE$$ | BIGINT(20) | NO | PRI | NULL | NULL | | DMLTYPE$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | | OLD_NEW$$ | VARCHAR2(1 ) | YES | NULL | NULL | NULL | +------------+--------------+------+------+---------+-------+ 6 rows in set