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 for materialized view logs is enabled, OceanBase Database will automatically create the mlog or automatically update the mlog definition when creating an incrementally refreshed materialized view or a real-time materialized view.
Note
For V4.3.5, starting from V4.3.5 BP4, the automatic management feature for materialized view logs is supported. For more details, 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.
A materialized view log is not moved to the recycle bin when it is dropped.
You cannot perform an
ALTERoperation on a materialized view log after it is created.You cannot create an index on a materialized view log.
You cannot perform DML operations on a materialized view log. An error is 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 of materialized view logs
A table can have only one materialized view log whose schema name is mlog$_table, where table is the name of the base table.
The schema definition of a materialized view log is 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. |
Operations on existing materialized view logs
- You can directly query the schema and data of the schema where a materialized view log is located.
- You can execute the
DBMS_MVIEW.PURGE_LOG(table_name)procedure to purge the materialized view log of a table. - If the size of a materialized view log exceeds the available disk space, an error is returned. In this case, you must drop and recreate the materialized view log to continue using it.
Base table operations on materialized view logs
DML operations on base tables
The definition of a materialized view log table is to record DML operations on the base table. Therefore, INSERT, DELETE, and UPDATE operations on the base table are recorded in the materialized view log table, as described below:
- When you perform an
INSERToperation on the base table, a record is inserted into the materialized view log table for each row inserted into the base table. Thedmltype$$column value of the record isI, and theold_new$$column value isN. - When you perform a
DELETEoperation on the base table, a record is inserted into the materialized view log table for each row deleted from the base table. Thedmltype$$column value of the record isD, and theold_new$$column value isO. - When you perform an
UPDATEoperation on the base table, two records are inserted into the materialized view log table for each row updated. The first record is for the old value of the row before theUPDATEoperation, and thedmltype$$column value isU, and theold_new$$column value isO. The second record is for the new value of the row after theUPDATEoperation, and thedmltype$$column value isU, and theold_new$$column value isN.
DDL operations on base tables
You must drop the corresponding materialized view log before you drop the base table. Otherwise, an error is returned. Because a materialized view log is bound to a base table, you cannot drop the base table and retain the materialized view log.
For more information about DDL operations supported on base tables, see Online DDL and offline DDL operations.
Create a materialized view log
Note
OceanBase Database does not support specifying partitions (PARTITION) when you create a materialized view log. The partitions of the materialized view log are bound to the partitions of the base table.
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
The syntax for creating a materialized view log is as follows:
CREATE [OR REPLACE] MATERIALIZED VIEW LOG ON [schema.] table_name
[parallel_clause]
[with_clause]
[mv_log_purge_clause];
The parameters are described as follows:
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 for which to create a materialized view log.parallel_clause: optional. Specifies the degree of parallelism for purging the materialized view log.with_clause: optional. Specifies the auxiliary columns to be included in the materialized view log.mv_log_purge_clause: optional. Specifies the time for purging expired records in the materialized view log.
For more information about the parameters, see CREATE MATERIALIZED VIEW LOG.
Here is an example:
Create a table named
tbl1.CREATE TABLE tbl1 (col1 NUMBER, col2 VARCHAR2(20), col3 NUMBER, PRIMARY KEY(col1, col3)) PARTITION BY HASH(col3) PARTITIONS 10;Create a materialized view log on the
tbl1table. Set the degree of parallelism for purging the materialized view log to5, and specify that the materialized view log record the changes of thecol2column, including the new values before and after the changes. Configure the materialized view log to purge expired records on a daily basis starting from the current date.CREATE MATERIALIZED VIEW LOG ON 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
tbl1table.DESC mlog$_tbl1;The return 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 | NO | PRI | 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
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 base table. For more information about privileges in OceanBase Database, see Privilege types in Oracle mode.
Syntax
The syntax for modifying a materialized view log is as follows:
ALTER MATERIALIZED VIEW LOG ON [schema.]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]]
parallel_clause:
NOPARALLEL
| PARALLEL integer
The parameters are described as follows:
schema.: optional. The schema where the base table corresponding to the materialized view log is located. If you omitschema., the default base table in the schema where the session is located 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 NUMBER PRIMARY KEY, col2 VARCHAR2(20), col3 NUMBER, col4 BLOB);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 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 current_date NEXT current_date + 1;
Drop a materialized view log
Considerations
- If a transaction is running on the base table when you drop a materialized view log, the drop operation will be blocked until the transaction ends.
- A materialized view log is not moved to the recycle bin when it is dropped.
Privileges
To drop a materialized view log, you must have the DROP TABLE privilege. For more information about privileges in OceanBase Database, see Privilege types in MySQL mode.
Syntax
The syntax for dropping a materialized view log is as follows:
DROP MATERIALIZED VIEW LOG ON [database.] table;
The parameters are described as follows:
schema.: optional. The name of the schema where the base table for which to drop the materialized view log is located. If you do not specifyschema., the base table is assumed to be in your own schema.table: the name of the base table for which to drop 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_tbl1. Define the materialized view as an incrementally refreshed one with an automatic refresh interval of 5 minutes. In the query section, specify to group the records in thetest_tbl1table by thecol2column and calculate the number of records (cnt), the number of non-null records in thecol3column (cnt_col3), and the sum of thecol3column (sum_col3) as the result of the materialized view.CREATE MATERIALIZED VIEW mv_test_tbl1 REFRESH FAST ON DEMAND START WITH current_date NEXT current_date + 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 sys.DBA_MVIEW_LOGS WHERE MASTER = 'TEST_TBL1';Notice
In Oracle mode, if the
MASTERfield in thesys.DBA_MVIEW_LOGSview matches the table name, the table name must be in uppercase letters.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_USER001 | TEST_TBL1 | MLOG$_TEST_TBL1 | NULL | NO | YES | NO | YES | YES | YES | NO | NO | NULL | NULL | 03-SEP-25 | 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;