A materialized view log (mlog) records incremental updates to the base table to support fast refresh of materialized views. An mlog is a record table that tracks changes in the base table and applies these changes to the corresponding materialized view to enable fast refresh.
If the automatic management of materialized view logs is enabled, OceanBase Database automatically creates or updates the mlog definition when you create a materialized view with incremental refresh or a real-time materialized view. For more information about the automatic management of materialized view logs, see Automatic management of materialized view logs.
Limitations
- You can create a materialized view log only on a regular table or a materialized view.
- A regular 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 is completed.
- A materialized view log supports LOB columns, but only supports inline storage of LOB data. For more information about LOB types, see LOB types.
- A materialized view log does not support the following four types of data: JSON, XML, GIS, and UDT.
- A materialized view log does not support generated columns (including virtual and non-virtual columns).
- A materialized view log does not support specifying partitions. The partitions of the materialized view log are bound to those of the base table.
- The maximum length of the name of a materialized view log is the same as that of a regular table, which cannot exceed 64 characters. The name of the base table for a materialized view log must be less than or equal to 58 characters because the name of the materialized view log will be prefixed with
mlog$_. - A materialized view log does not support table-level restore.
- A materialized view log will not be moved to the recycle bin when it is deleted.
- After a materialized view log is created, you cannot perform the
ALTERoperation on it. - You cannot create an index on a materialized view log.
- You cannot perform DML operations on a materialized view log. An error will be returned.
Privileges
- You must have the
SELECTandCREATE TABLEprivileges on the base table to create a materialized view log. - You must have the
ALTERprivilege on the base table to modify a materialized view log. - You must have the
DROP TABLEprivilege to drop a materialized view log. - You can only grant the
SELECTprivilege to a materialized view log. Other DML operations are not supported.
Schema definition of a 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 definition of a materialized view log is as follows:
A regular 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 regular table.
The schema definition of a materialized view log is as follows:
| Column | Type | Description |
|---|---|---|
| sequence$$ | in64_t | An auto-increment column that serves as the primary key of the materialized view log (mlog).
NoteThe primary key of mlog consists of the primary key of the base table, all partitioning keys (if any), and the auto-increment column |
| primary key | Follows the base table | If the base table has a primary key, the primary key column(s) of the base table are recorded in mlog. If the base table has a composite primary key, multiple columns are recorded in mlog. |
| dmltype$$ | char(1) | Records the DML type. Valid values: I, D, and U, which stand for INSERT, DELETE, and UPDATE, respectively. |
| old_new$$ | char(1) | Used to mark old values and new values in UPDATE statements. An UPDATE statement will write two rows of data to the materialized view log: one row for the old value before the UPDATE operation and another row for the new value after the UPDATE operation. The old value is marked with O, and the new value is marked with N. |
| column 1 | Follows the base table | Column 1 of the base table. |
| ... | N/A | N/A |
| column N | Follows the base table | Column N of the base table. |
| ora_rowscn | N/A | A pseudo column that records a hidden column in the storage layer. It can be read. |
| m_row$$ | uint64_t | This value is recorded in mlog only if the base table has no primary key. mlog must contain the primary key column(s) of the base table. If the base table has no primary key, the name of the hidden primary key in mlog is M_ROW$$. |
Operations on an existing materialized view log
- You can directly query the schema of the materialized view log and the data in it.
- You can execute the
PURGEoperation on a materialized view log of a base table by using DBMS_MVIEW.PURGE_LOG(table_name). - If the size of a materialized view log exceeds the available disk space, an error will be returned. In this case, you must delete the materialized view log and recreate it before you can use it.
Impact of operations on the base table on the materialized view log
DML operations on the base table
The materialized view log is defined to record DML operations on the base table. Therefore, the INSERT, DELETE, and UPDATE operations on the base table will be recorded in the materialized view log, as follows:
- If you perform an
INSERToperation on the base table, each row of data inserted will also be recorded in the materialized view log. Thedmltype$$column of the record will beI, and theold_new$$column will beN. - If you perform a
DELETEoperation on the base table, each row of data deleted will also be recorded in the materialized view log. Thedmltype$$column of the record will beD, and theold_new$$column will beO. - If you perform an
UPDATEoperation on the base table, each row of data modified will be recorded in two rows of data in the materialized view log. The first row records the old value of the row before theUPDATEoperation. Thedmltype$$column of this row will beU, and theold_new$$column will beO. The second row records the new value of the row after theUPDATEoperation. Thedmltype$$column of this row will beU, and theold_new$$column will beN.
DDL operations on the base table
Before you delete the base table, you must delete the corresponding materialized view log. Otherwise, an error will be returned. This is because a materialized view log is bound to the base table. Therefore, you cannot delete the base table while retaining the materialized view log.
For more information about DDL operations supported by the base table, see Online DDL and offline DDL operations.
Create a materialized view log
Note
OceanBase Database does not support specifying partitions for mlogs. The partitions of an mlog are bound to those 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 the privileges of OceanBase Database, see Privilege types in MySQL-compatible mode.
Syntax
The SQL statement for creating a materialized view log is as follows:
CREATE [OR REPLACE] MATERIALIZED VIEW LOG ON [database.] table_name
[parallel_clause]
[with_clause]
[mv_log_purge_clause];
Parameters:
OR REPLACE: optional. If you specify this clause, OceanBase Database creates a new materialized view log with the same name as the existing one and replaces the existing one with the new one. During the recreation of the materialized view log, the read/write operations on the materialized view and the base table are not affected.table_name: specifies the name of the base table.parallel_clause: optional. Specifies the parallelism for purging the materialized view log.with_clause: optional. Specifies the auxiliary columns in the materialized view log.mv_log_purge_clause: optional. Specifies the time when the data in the materialized view log is purged.
For more information about the parameters in the syntax, see CREATE MATERIALIZED VIEW LOG.
Examples:
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. Specify the parallelism for purging the materialized view log as5, and record the changes to thecol2column in the materialized view log. The changes to thecol2column are recorded in the materialized view log. The materialized view log is purged every1day starting from the current date.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 information about the materialized view log 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
Privilege requirements
To execute the ALTER MATERIALIZED VIEW LOG statement, you must have the ALTER privilege on the base table. For more information about the privileges of OceanBase Database, see Privilege types in MySQL-compatible mode.
Syntax
The SQL statement 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
Parameters:
database.: optional. Specifies the database where the materialized view is located. If you omit this clause, the base table is assumed to be in the current database.table_name: specifies the name of the base table.alter_mlog_action_list: specifies the list of operations that can be performed on the materialized view log. You can specify multiple operations separated by commas (,).
For more information about the parameters in the syntax, see ALTER MATERIALIZED VIEW LOG.
Examples:
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 on the
test_tbl1table.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE(col2, col3, col4) INCLUDING NEW VALUES;Set the parallelism for purging 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 be purged every1day starting from the current date.ALTER MATERIALIZED VIEW LOG ON test_tbl1 PURGE START WITH sysdate() NEXT sysdate() + INTERVAL 1 DAY;Set the inline storage length threshold for LOB data in 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
- If a base table is involved in an ongoing transaction when you drop a materialized view log, the drop operation is blocked until the transaction ends.
- When you drop a materialized view log, the materialized view is not moved to the recycle bin.
Privileges
To drop a materialized view log, you must have the DROP TABLE privilege. For more information about the privileges of OceanBase Database, see Privilege types in MySQL-compatible mode.
Syntax
The SQL statement for dropping a materialized view log is as follows:
DROP MATERIALIZED VIEW LOG ON [database.] table;
Parameters:
database.: optional. Specifies the database where the base table of the materialized view log is located. If you omit this clause, the base table is assumed to be in your own database.table: specifies the name of the base table.
Examples:
Drop the materialized view log on the tbl1 table.
DROP MATERIALIZED VIEW LOG ON tbl1;
Example
This example shows how to create a regular table, a materialized view log, and a materialized view with incremental refresh, and how to delete the materialized view log and the materialized view with incremental refresh.
This example shows how to create a regular table, a materialized view log, and an incrementally refreshed materialized view. It also covers how to delete a materialized view log and an 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 on the
test_tbl1table, specifying the use of sequence numbers (SEQUENCE) to identify changed data. The columns to be recorded includecol2andcol3.CREATE MATERIALIZED VIEW LOG ON test_tbl1 WITH SEQUENCE (col2, col3) INCLUDING NEW VALUES;Create a materialized view named
mv_test_tbl1with incremental refresh, set to automatically refresh every 5 minutes. In the query part, specify grouping by thecol2column from thetest_tbl1table 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_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 for the
test_tbl1table.SELECT * FROM oceanbase.DBA_MVIEW_LOGS WHERE MASTER = 'test_tbl1';The returned 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 setDelete the materialized view log on the
test_tbl1table.DROP MATERIALIZED VIEW LOG ON test_tbl1;Delete the materialized view
mv_test_tbl1.DROP MATERIALIZED VIEW mv_test_tbl1;