Compared with minor compaction, major compaction is usually a more significant operation that takes a longer time. In best practices, it is generally expected to perform major compaction only once a day during off-peak hours. Therefore, major compaction is sometimes referred to as daily compaction.
Major compaction is the process of merging static and dynamic data, which can be time-consuming. When the incremental data generated by minor compactions accumulates to a certain extent, major compaction is performed using major freeze. The key difference between major compaction and minor compaction is that major compaction involves merging the data of a tenant with its corresponding static data at a unified snapshot point. This process ultimately forms a tenant-level snapshot.
Minor Compaction |
Minor Compaction |
Major Compaction |
|---|---|---|
| At the partition or tenant level, it involves the materialization of MemTables. | At the partition level. | At the tenant level, it generates a tenant-level snapshot. |
| Each tenant on each OBServer node independently decides when to freeze its MemTables, and the primary and standby partitions are not synchronized. | Each partition performs minor compaction based on the number of SSTables (including Mini SSTables and Minor SSTables) in the partition. | All partitions of a tenant perform MemTable freezing together, requiring synchronization between primary and standby partitions. During major compaction, data consistency is verified. |
| May contain multiple versions of data rows. | May contain multiple versions of data rows. | Contains only the version of data rows at the snapshot point. |
| Persists one or more MemTables into Mini SSTables. | Merges multiple Mini SSTables into one Mini SSTable or merges multiple Mini SSTables with one Minor SSTable to generate a new Minor SSTable containing only incremental data. Rows to be deleted need special marking. | Merges the current major version's SSTables and MemTables with the full static data of the previous major version to generate new full data. |
Although major compaction is time-consuming, it provides an operational window for the database. During this window, OceanBase Database can leverage major compaction to handle multiple compute-intensive tasks, thereby improving overall resource utilization efficiency.
Data compression
During major compaction, OceanBase Database performs two levels of compression. The first level is semantic-based encoding compression within the database, and the second level is general compression based on the user-specified compression algorithm. The encoded data is further compressed using algorithms such as lz4. Compression not only saves storage space but also significantly enhances query performance. Currently, OceanBase Database supports compression algorithms such as snappy, lz4, lzo, and zstd, allowing users to balance compression ratio and decompression time. While MySQL and Oracle also support data compression to some extent, traditional databases with fixed-length pages inevitably create storage gaps during compression, which affects compression efficiency. More importantly, for LSM-tree-based storage systems like OceanBase Database, compression has almost no impact on data write performance.
Data verification
Major compaction using tenant-level consistent snapshots makes it easy for OceanBase Database to verify data consistency across multiple replicas. After major compaction, replicas can directly compare baseline data to ensure business data consistency across different replicas. Additionally, this snapshot baseline data can be used to verify data consistency between the main table and index tables, ensuring data consistency between them.
Schema changes
For schema changes such as adding or removing columns, OceanBase Database can complete data change operations during major compaction, making DDL operations smoother for business operations.
Types of major compactions
OceanBase Database supports the following types of major compactions:
Full major compaction
A full major compaction is the original major compaction algorithm of OceanBase Database. It is similar to the major compaction process in HBase and RocksDB. During a full major compaction, all static data is read and merged with dynamic data in memory. The merged data is then written to disk as new static data. In this process, all data is rewritten. A full major compaction consumes a large amount of disk I/O and storage space. Therefore, OceanBase Database does not perform a full major compaction unless a DBA explicitly specifies it.
Incremental major compaction
In the storage engine of OceanBase Database, a macroblock is the basic I/O unit. In many cases, not all macroblocks are modified. If a macroblock has no incremental modifications, the major compaction can directly reuse the data macroblock. OceanBase Database refers to this type of major compaction as an incremental major compaction. An incremental major compaction significantly reduces the workload of major compactions and is the default major compaction algorithm of OceanBase Database. Furthermore, OceanBase Database divides macroblocks into smaller microblocks. In many cases, not all microblocks are modified. Therefore, microblocks can be reused instead of being rewritten. Microblock-level incremental major compactions further reduce the time required for major compactions.
Progressive major compaction
To support the rapid development of businesses, users inevitably perform DDL operations such as adding and dropping columns and creating indexes. These DDL operations are usually expensive for databases. MySQL did not support online DDL operations for a long time (until version 5.6) and even today, online DDL operations in MySQL 5.7 are still risky for DBAs because a large DDL operation can cause replication lag between the primary and standby nodes of MySQL.
OceanBase Database considers the need for online DDL operations from the beginning of its design. Currently, OceanBase Database supports online DDL operations such as adding and dropping columns and creating indexes without blocking read and write operations, and without affecting Paxos synchronization between multiple replicas. Changes to columns are immediately effective, and changes to stored data are delayed until daily major compactions. However, for some DDL operations such as adding and dropping columns, all data must be rewritten. If all data is rewritten during a single daily major compaction, it will be a significant challenge for storage space and major compaction time. To solve this problem, OceanBase Database introduces progressive major compactions, which distribute the data rewriting caused by DDL operations across multiple daily major compactions. For example, if the progressive rounds are set to 60, only 1/60 of the data is rewritten during each major compaction. After 60 rounds of major compactions, all data is rewritten. Progressive major compactions reduce the burden on DBAs when performing DDL operations and make DDL operations smoother.
Major compaction triggers
Major compactions can be triggered in three ways: automatically, periodically, and manually.
A major compaction is automatically triggered when the number of minor freezes for a tenant exceeds the threshold.
You can set a parameter to trigger a major compaction during off-peak hours every day.
obclient> ALTER SYSTEM SET major_freeze_duty_time = '02:00' TENANT = t1;You can manually trigger a major compaction by using the following maintenance commands:
Trigger a major compaction for other tenants from the
systenant.Trigger a major compaction for the
systenant.obclient> ALTER SYSTEM MAJOR FREEZE TENANT = sys;Trigger a major compaction for all user tenants.
obclient> ALTER SYSTEM MAJOR FREEZE TENANT = all_user;Trigger a major compaction for all META tenants.
obclient> ALTER SYSTEM MAJOR FREEZE TENANT = all_meta;Trigger a major compaction for tenants t1 and t2.
obclient> ALTER SYSTEM MAJOR FREEZE TENANT = t1,t2;
Trigger a major compaction for the current tenant from a user tenant.
obclient> ALTER SYSTEM MAJOR FREEZE;
References
For more information about major compactions, see Major compactions.
