A materialized view log (mlog) records incremental update data of base tables (ordinary tables or materialized views) to support the fast refresh feature of materialized views. A materialized view log is a record table that tracks changes in the base table and applies the changes to the corresponding materialized view.
If the automatic management feature of materialized view logs is enabled, OceanBase Database automatically creates a materialized view log or updates the materialized view log definition when you create an incremental refresh materialized view or a real-time materialized view.
Note
For OceanBase Database V4.3.5, the automatic management feature of materialized view logs is supported from V4.3.5 BP4. For more information, see Automatic management of materialized view logs.
Limitations
You can create a materialized view log only on a base table or materialized view.
A base table can be bound to only one materialized view log.
If a transaction is running on the base table when you create a materialized view log, the creation operation will be blocked until the transaction ends.
Materialized view logs support columns of the LOB type, but only for inline storage of LOB data. For more information about LOB types, see LOB types.
Notice
For OceanBase Database V4.3.5, materialized view logs support LOB columns starting from V4.3.5 BP1.
The materialized view log does not support JSON, XML, GIS, and UDT data.
The materialized view log does not support generated columns (including virtual and non-virtual generated columns).
The materialized view log does not support partitioning.
The name of a materialized view log cannot exceed 64 characters in length. The name of the base table cannot exceed 58 characters in length, because the name of the materialized view log is prefixed with
mlog$_.Table-level restore is not supported for materialized view logs.
When a materialized view log is deleted separately, it does not go to the recycle bin.
ALTERoperations are not supported for materialized view logs after they are created.Indexes cannot be created on materialized view logs.
All DML operations are not supported on materialized view logs; otherwise, an error will be returned.
Privileges
- To create a materialized view log, you must have the
SELECTprivilege on the base table and theCREATE TABLEprivilege. - To modify a materialized view log, you must have the
ALTERprivilege on the base table. - To drop a materialized view log, you must have the
DROP TABLEprivilege. - You can grant only the
SELECTprivilege on a materialized view log. Other DML privileges are not supported.
Schema definition for the materialized view log
A table can have only one materialized view log. The schema name of the materialized view log is mlog$_table, where table is the name of the base table.
The schema of a materialized view log is defined as follows:
| Column | Type | Description |
|---|---|---|
| sequence$$ | in64_t | The auto-increment column, which is the primary key of the materialized view log table.
NoteThe primary key of the materialized view log table is a composite key that consists of the primary key of the base table (if any) and all partitioning keys of the base table (if any), and the auto-increment column |
| primary key | Follows the base table | If the base table has a primary key, the primary key of the base table is recorded in the materialized view log table. If the base table has a composite primary key, all columns of the composite primary key are recorded in the materialized view log table. |
| dmltype$$ | char(1) | The DML type. Valid values include I, D, and U, which indicate INSERT, DELETE, and UPDATE, respectively. |
| old_new$$ | char(1) | The old value or new value of a row in an UPDATE statement. For each UPDATE statement, two rows are written to the materialized view log, one for the old value and the other for the new value. The old value is marked with O, and the new value is marked with N. |
| column 1 | Follows the base table | The first ordinary column of the base table. |
| ... | N/A | N/A |
| column N | Follows the base table | The Nth ordinary column of the base table. |
| ora_rowscn | N/A | A pseudo-column that records the value of a hidden column in the storage layer. The value can be read. |
| m_row$$ | uint64_t | The value of the hidden primary key of the base table. This column is recorded only when the base table has no primary key. |
Manage existing materialized view logs
- You can query the schema where the materialized view log resides for its structure and data.
- You can execute the DBMS_MVIEW.PURGE_LOG(table_name) procedure to purge the materialized view log of a base table.
- If the size of a materialized view log exceeds the available disk space, an error will be returned. In this case, you must drop and recreate the materialized view log.
Impact on materialized view logs of operations on base tables
Base table DML operations
The definition of a materialized view log table is to record the DML operations on the base table. Therefore, the INSERT, DELETE, and UPDATE operations on the base table are ultimately recorded in the materialized view log, specifically as follows:
- If you perform an
INSERToperation on the base table, each inserted row will generate a corresponding record in the materialized view log. Thedmltype$$column value of this record will beI, and theold_new$$column value will beN. - If you perform a
DELETEoperation on the base table, a record is inserted into the materialized view log for each deleted row. Thedmltype$$column value of these records isD, and theold_new$$column value isO. - When you perform an
UPDATEoperation on a base table, each modified row generates two records in the materialized view log. The first record contains the old value of theUPDATEd row, with thedmltype$$column set toUand theold_new$$column set toO. The second record contains the new value after theUPDATE, with thedmltype$$column set toUand theold_new$$column set toN.
Data DDL operations
Before you drop a base table, you must drop its corresponding materialized view log. Otherwise, an error will be reported. The materialized view log is bound to the base table.
For more information about DDL operations supported by base tables, see Overview of online DDL operations and offline DDL operations.
Create a materialized view log
Note
In OceanBase Database, you cannot specify a partition for a materialized view log. The partition of a materialized view log is the same as that of the base table.
Privilege requirements
To create a materialized view log, you must have the CREATE TABLE and SELECT privileges on the base table. For more information about OceanBase Database privileges, see Privilege types in MySQL mode.
Grammar
The SQL statement for creating a materialized view log is in the following format:
CREATE [OR REPLACE] MATERIALIZED VIEW LOG ON [database.] table_name
[parallel_clause]
[with_clause]
[mv_log_purge_clause];
Parameter description:
OR REPLACE: optional. Specifies whether to create a newmlogif the correspondingmlogalready exists. If this option is specified, the newmlogwill replace the existingmlog. The process of recreating themlogwill not affect normal read or write operations on the materialized view or the base table.Note
For OceanBase Database V4.3.5, the
OR REPLACEoption is supported starting from V4.3.5 BP3.table_name: the name of the base table that corresponds to the materialized view log.parallel_clause: the degree of parallelism for materialized view log cleanup.WITHclause: Optional. It specifies the columns to be included in the materialized view log.mv_log_purge_clause: Specifies the data cleanup time for the materialized view log. This parameter is optional.
For more information about the syntax of creating a materialized view log, see CREATE MATERIALIZED VIEW LOG.
Here is an example:
Create a table named
tbl1.CREATE TABLE tbl1 (col1 INT, col2 VARCHAR(20), col3 INT, PRIMARY KEY(col1, col3)) PARTITION BY HASH(col3) PARTITIONS 10;Create a materialized view log on the
tbl1table. Set the degree of parallelism for parallelizing the materialized view log to5, specify to record the change information of thecol2column, and indicate that the new values before and after the change are recorded; and configure the materialized view log to be cleared on a daily basis, starting from the current date, for the outdated materialized view log records.CREATE MATERIALIZED VIEW LOG ON tbl1 PARALLEL 5 WITH SEQUENCE(col2) INCLUDING NEW VALUES PURGE START WITH sysdate() NEXT sysdate() + interval 1 day;View the materialized view log information on the
tbl1table.DESC mlog$_tbl1;The return result is as follows:
+------------+-------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-------------+------+------+---------+-------+ | col1 | int(11) | NO | PRI | NULL | | | col2 | varchar(20) | YES | | NULL | | | col3 | int(11) | NO | PRI | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | +------------+-------------+------+------+---------+-------+ 6 rows in set
Modify a materialized view log
Note
For OceanBase Database V4.3.5, modifying materialized view logs is supported starting from V4.3.5 BP1.
Privileges
To execute the ALTER MATERIALIZED VIEW LOG statement, the current user must have the ALTER privilege on the target base table. For more information about privileges in OceanBase Database, see Privilege types in MySQL mode.
Syntax
The syntax for modifying a materialized view log is as follows:
ALTER MATERIALIZED VIEW LOG ON [database.]table_name alter_mlog_action_list;
alter_mview_action_list:
alter_mlog_action [, alter_mlog_action ...]
alter_mlog_action:
parallel_clause
| PURGE [[START WITH expr] [NEXT expr]]
| LOB_INROW_THRESHOLD [=] integer
parallel_clause:
NOPARALLEL
| PARALLEL integer
The parameters are described as follows:
database.: optional. The name of the database where the materialized view log is located. If you omitdatabase., the default base table in the database connected by the current session is used.table_name: the name of the base table corresponding to the materialized view log.alter_mlog_action_list: the list of actions that can be performed on the materialized view log. You can specify multiple actions at a time, separated by commas (,).
For more information about the syntax for modifying materialized view logs, see ALTER MATERIALIZED VIEW LOG.
Here is an example:
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT, col4 TEXT);Create a materialized view log based on the
test_tbl1table.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE(col2, col3, col4) INCLUDING NEW VALUES;Set the degree of parallelism of the materialized view log on the
test_tbl1table to 5.ALTER MATERIALIZED VIEW LOG ON test_tbl1 PARALLEL 5;Set the materialized view log on the
test_tbl1table to purge expired materialized view log records on a daily basis starting from the current date.ALTER MATERIALIZED VIEW LOG ON test_tbl1 PURGE START WITH sysdate() NEXT sysdate() + INTERVAL 1 DAY;Modify the LOB inline storage length threshold of the materialized view log on the
test_tbl1table.ALTER MATERIALIZED VIEW LOG ON test_tbl1 LOB_INROW_THRESHOLD 10000;
Drop a materialized view log
Considerations
- When you drop a materialized view log, if the base table is involved in an ongoing transaction, the drop operation is blocked until the transaction is completed.
- When you delete a materialized view log, it does not go to the recycle bin immediately.
Privilege requirements
To drop a materialized view log, you must have the DROP TABLE privilege. For more information about OceanBase Database privileges, see Privilege types in MySQL mode.
Syntax
The SQL syntax for dropping a materialized view log is as follows:
DROP MATERIALIZED VIEW LOG ON [database.] table;
Description of parameters:
database.: Optional. Specifies the database where the base table for the materialized view log resides. If you omitdatabase., the base table is assumed to be in your current database by default.table: specifies the name of the base table corresponding to the materialized view log.
Here is an example:
Drop the materialized view log on the tbl1 table.
DROP MATERIALIZED VIEW LOG ON tbl1;
Examples
This topic describes how to create a table, a materialized view log, an incrementally refreshed materialized view, and how to drop the materialized view log and the incrementally refreshed materialized view.
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 INT PRIMARY KEY, col2 INT, col3 INT);Create a materialized view log for the
test_tbl1table. Specify the sequence number (SEQUENCE) as the identifier for changes, and specify the columns to be recorded, which arecol2andcol3.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE (col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_test_tbl1with incremental refresh and automatic refresh every 5 minutes. In the query section, group records intest_tbl1bycol2and calculate the number of records (cnt), the number of non-null values incol3(cnt_col3), and the sum ofcol3(sum_col3) as the result of the materialized view.CREATE MATERIALIZED VIEW mv_test_tbl1 REFRESH FAST ON DEMAND START WITH sysdate() NEXT sysdate() + interval 5 minute AS SELECT col2, COUNT(*) cnt, COUNT(col3) cnt_col3, SUM(col3) sum_col3 FROM test_tbl1 GROUP BY col2;View the materialized view log information of the
test_tbl1table.SELECT * FROM oceanbase.DBA_MVIEW_LOGS WHERE MASTER = 'test_tbl1';The return result is as follows:
+-----------+-----------+-----------------+-------------+--------+-------------+-----------+----------------+----------+--------------------+--------------------+----------------+-------------+----------------+---------------------+-------------------+-----------------+------------------+-------------+-----------+-----------------+ | LOG_OWNER | MASTER | LOG_TABLE | LOG_TRIGGER | ROWIDS | PRIMARY_KEY | OBJECT_ID | FILTER_COLUMNS | SEQUENCE | INCLUDE_NEW_VALUES | PURGE_ASYNCHRONOUS | PURGE_DEFERRED | PURGE_START | PURGE_INTERVAL | LAST_PURGE_DATE | LAST_PURGE_STATUS | NUM_ROWS_PURGED | COMMIT_SCN_BASED | STAGING_LOG | PURGE_DOP | LAST_PURGE_TIME | +-----------+-----------+-----------------+-------------+--------+-------------+-----------+----------------+----------+--------------------+--------------------+----------------+-------------+----------------+---------------------+-------------------+-----------------+------------------+-------------+-----------+-----------------+ | test_db | test_tbl1 | mlog$_test_tbl1 | NULL | NO | YES | NO | YES | YES | YES | NO | NO | NULL | NULL | 2025-09-03 14:13:06 | 0 | 0 | YES | NO | 1 | 0 | +-----------+-----------+-----------------+-------------+--------+-------------+-----------+----------------+----------+--------------------+--------------------+----------------+-------------+----------------+---------------------+-------------------+-----------------+------------------+-------------+-----------+-----------------+ 1 row in setDrop the materialized view log of the
test_tbl1table.DROP MATERIALIZED VIEW LOG ON test_tbl1;Drop the materialized view
mv_test_tbl1.DROP MATERIALIZED VIEW mv_test_tbl1;