OceanBase Database supports automatic management of materialized view logs (mlogs). This feature includes the following two aspects:
- When you create a materialized view, OceanBase Database analyzes the dependency of the materialized view on the base table and automatically creates the required mlogs.
- OceanBase Database periodically prunes mlogs in the background, deletes mlogs that are no longer referenced, and reduces the number of columns in mlogs to reduce the maintenance cost of mlogs.
Limitations
When you create a materialized view, an mlog table is automatically created or its definition is automatically updated only if the materialized view is explicitly declared as an incrementally refreshed materialized view (by specifying REFRESH FAST) or a real-time materialized view (by specifying ENABLE ON QUERY COMPUTATION).
Considerations
- When you update the definition of an mlog table, OceanBase Database creates a new mlog table and replaces the original mlog table. During the replacement, the incremental data in the original mlog table is incrementally refreshed into the associated incrementally refreshed materialized views. Therefore, when a base table is associated with a large number of materialized views and contains a large amount of incremental data, updating the mlog table definition may be a time-consuming operation.
- We recommend that you do not set the
mlog_trim_intervalparameter to a very small value. Otherwise, the mlog table may be mistakenly trimmed, and you may need to recreate or update the mlog table when you create a materialized view.
Related parameters
OceanBase Database provides two parameters to control the behavior of mlog auto-maintenance:
- enable_mlog_auto_maintenance: specifies whether to enable the automatic management of materialized view logs.
- mlog_trim_interval: specifies the interval at which the background automatic trimming task of an mlog table is scheduled.
Examples
Enable the automatic management of mlogs.
Note
In OceanBase Database V4.6.0:
- For newly created tenants, the default value of the
enable_mlog_auto_maintenanceparameter isTrue, which means that the automatic management of mlogs is enabled by default. - For tenants upgraded from earlier versions of OceanBase Database (before V4.6.0), the default value of the
enable_mlog_auto_maintenanceparameter isFalse, which means that the automatic management of mlogs is disabled by default.
obclient> ALTER SYSTEM SET enable_mlog_auto_maintenance = True;
Example 1: Automatically create an mlog
Create a table named
test_tbl1.obclient> CREATE TABLE test_tbl1(col1 INT, col2 INT, col3 INT);Create an incremental refresh materialized view named
mv_test_tbl1on thetest_tbl1table. OceanBase Database automatically creates an mlog for thetest_tbl1table on the requiredcol2column.obclient> CREATE MATERIALIZED VIEW mv_test_tbl1 REFRESH FAST AS SELECT col2, count(*) cnt FROM test_tbl1 GROUP BY col2;Query the information about the materialized view logs of the
test_tbl1table. The query result is as follows:obclient> DESC mlog$_test_tbl1;The query result is as follows:
+------------+-----------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+------+---------+-------+ | col2 | int(11) | YES | | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | | M_ROW$$ | bigint unsigned | NO | PRI | NULL | | +------------+-----------------+------+------+---------+-------+ 5 rows in set
Example 2: Automatically update the definition of an mlog
Create a table named
test_tbl2.obclient> CREATE TABLE test_tbl2(col1 INT, col2 INT, col3 INT);Create an incremental refresh materialized view named
mv1_test_tbl2on thetest_tbl2table. OceanBase Database automatically creates an mlog for thetest_tbl2table on the requiredcol2column. The query result is as follows:obclient> CREATE MATERIALIZED VIEW mv1_test_tbl2 REFRESH FAST AS SELECT col2, count(*) cnt FROM test_tbl2 GROUP BY col2;Query the information about the materialized view logs of the
test_tbl2table. The query result is as follows:obclient> DESC mlog$_test_tbl2;The query result is as follows:
+------------+-----------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+------+---------+-------+ | col2 | int(11) | YES | | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | | M_ROW$$ | bigint unsigned | NO | PRI | NULL | | +------------+-----------------+------+------+---------+-------+ 5 rows in setCreate an incremental refresh materialized view named
mv2_test_tbl2on thetest_tbl2table. OceanBase Database detects that the current mlog table contains only thecol2column and updates the existing mlog definition to add thecol3column to the mlog table. The query result is as follows:obclient> CREATE MATERIALIZED VIEW mv2_test_tbl2 REFRESH FAST AS SELECT col3, count(*) cnt FROM test_tbl2 GROUP BY col3;Query the information about the materialized view logs of the
test_tbl2table again. The query result is as follows:obclient> DESC mlog$_test_tbl2;The query result is as follows:
+------------+-----------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+------+---------+-------+ | col2 | int(11) | YES | | NULL | | | col3 | int(11) | YES | | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | | M_ROW$$ | bigint unsigned | NO | PRI | NULL | | +------------+-----------------+------+------+---------+-------+ 6 rows in set
Example 3: Automatically prune an mlog
Create a table named
test_tbl3.obclient> CREATE TABLE test_tbl3(col1 INT, col2 INT, col3 INT);Create an incremental refresh materialized view named
mv1_test_tbl3on thetest_tbl3table. OceanBase Database automatically creates an mlog for thetest_tbl3table on the requiredcol2column. The query result is as follows:obclient> CREATE MATERIALIZED VIEW mv1_test_tbl3 REFRESH FAST AS SELECT col2, count(*) cnt FROM test_tbl3 GROUP BY col2;Create an incremental refresh materialized view named
mv2_test_tbl3on thetest_tbl3table. OceanBase Database detects that the current mlog table contains only thecol2column and updates the existing mlog definition to add thecol3column to the mlog table. The query result is as follows:obclient> CREATE MATERIALIZED VIEW mv2_test_tbl3 REFRESH FAST AS SELECT col3, count(*) cnt FROM test_tbl3 GROUP BY col3;Query the information about the materialized view logs of the
test_tbl3table. The query result is as follows:obclient> DESC mlog$_test_tbl3;The query result is as follows:
+------------+-----------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+------+---------+-------+ | col2 | int(11) | YES | | NULL | | | col3 | int(11) | YES | | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | | M_ROW$$ | bigint unsigned | NO | PRI | NULL | | +------------+-----------------+------+------+---------+-------+ 6 rows in setChange the mlog pruning interval to observe the pruning results more quickly. The query result is as follows:
obclient> ALTER SYSTEM SET mlog_trim_interval = '5s';Drop the
mv1_test_tbl3materialized view. The query result is as follows:obclient> DROP MATERIALIZED VIEW mv1_test_tbl3;Wait for 5 seconds and query the information about the materialized view logs of the
test_tbl3table. The query result is as follows:obclient> DESC mlog$_test_tbl3;The query result is as follows:
+------------+-----------------+------+------+---------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+-----------------+------+------+---------+-------+ | col3 | int(11) | YES | | NULL | | | SEQUENCE$$ | bigint(20) | NO | PRI | NULL | | | DMLTYPE$$ | varchar(1) | YES | | NULL | | | OLD_NEW$$ | varchar(1) | YES | | NULL | | | M_ROW$$ | bigint unsigned | NO | PRI | NULL | | +------------+-----------------+------+------+---------+-------+ 5 rows in setDrop the
mv2_test_tbl3materialized view. The query result is as follows:obclient> DROP MATERIALIZED VIEW mv2_test_tbl3;Wait for 5 seconds and query the information about the materialized view logs of the
test_tbl3table. The query result is as follows:obclient> DESC mlog$_test_tbl3;The query result is as follows:
ERROR 1146 (42S02): Table 'test_db.mlog$_test_tbl3' doesn't existRestore the mlog pruning interval. The query result is as follows:
obclient> ALTER SYSTEM SET mlog_trim_interval = '1d';
