Introduction
In the era of digital transformation, real-time analysis of vast business data has become a necessity. Traditional databases often struggle to meet the performance and real-time analysis demands. As the ClickHouse community grows, developers seek to accelerate analysis by syncing data from MySQL to ClickHouse. π‘
While the MaterializedMySQL engine offers synchronization capabilities, it has several limitations:
- π« Only supports ClickHouse clusters of version 20.8 and later (Community Edition)
- π΅οΈ Lacks synchronization process visualization and performance monitoring
- π Doesn’t support tables without primary keys
- π Requires the MySQL and ClickHouse clusters to be in the same VPC network
- β No support for ETL, object name mapping, or appended columns
- π« Doesn’t allow data filtering or selective synchronization
- βΈοΈ Synchronization tasks cannot be paused or resumed
- π« Limited support for certain DDL operations
To overcome these challenges, the Data Transmission Service (DTS) team has developed a synchronization link from MySQL to ClickHouse. π
Advantages of Alibaba Cloud DTS for MySQL-to-ClickHouse Synchronization
Alibaba Cloud DTS offers a powerful solution for MySQL-to-ClickHouse synchronization, addressing the limitations of the MaterializedMySQL engine:
- π High compatibility with ApsaraDB for ClickHouse Community Edition
- π Visualized synchronization process with performance monitoring
- π Supports migration of tables without primary keys
- π Flexible selection of VPC networks for source and destination clusters
- βοΈ Supports features like ETL, object name mapping, and appended columns
- οΏ½ulty_man: π§βπ» Allows data filtering and selective synchronization
- β―οΈ Synchronization tasks can be paused and resumed
- π Supports common DDL operations like table creation, alteration, truncation, and dropping
With these advantages, Alibaba Cloud DTS offers a smooth and reliable experience for syncing data from MySQL to ClickHouse, facilitating efficient data analysis. π
Learning to Use Alibaba Cloud DTS for MySQL-to-ClickHouse Synchronization
When using Alibaba Cloud DTS for MySQL-to-ClickHouse synchronization, you may encounter challenges during preparation, task creation, and ongoing maintenance. This guide aims to provide best practices and address common pitfalls, ensuring a worry-free data migration experience and efficient data analysis with ClickHouse. π
1. Best Practices
1.1 Add Permissions to a Database Account
Insufficient permissions for the database account can cause DTS tasks to fail. Before configuring a task, grant the following permissions to the source and destination database accounts:
| Database | Schema Synchronization | Full Synchronization | Incremental Synchronization |
|---|---|---|---|
| MySQL | SELECT permission | SELECT permission | SELECT permission on objects to be synchronizedREPLICATION CLIENTREPLICATION SLAVESHOW VIEWPermissions to create databases and tables |
| ClickHouse | Read and write permissions |
1.2 Use the Alibaba Cloud DTS Console to Create Tasks
DTS MySQL-to-ClickHouse links have specific requirements for dblist. Using the DTS console will provide a standardized dblist format. If you need to use DTS OpenAPIs, you must specify dblist in the following format:
dbList={ “source_db_name”: { “name”: “target_db_name”, “all”: false, “state”: “open”, “Table”: { “source_table_name”: { “name”: “target_table_name”, “all”: true, “primary_key”: “id”, “message_key”: “id”, “partition_key”: “sipHash64(id)”, “part_key”: “id”, “type”: “partition” } }, “num”: 1 } }
1.3 Use DTS for Schema Migration
The ClickHouse schema greatly impacts data writing performance and stability. When migrating data from MySQL to ClickHouse with DTS, use the provided schema migration feature to avoid errors caused by unsupported schemas.
If you need to create a custom schema, it must meet the following DTS requirements:
- For ClickHouse Community Edition, create a local table and a distributed table. The distributed table name must match the destination table name in
dblist. The local table name must bedistributed_table_name_local. - Add two appended columns:
_signand_version:
| Column | Data Type | Default Value | Description |
|---|---|---|---|
_sign | Int8 | 1 | Record DML operation type:Insert: 1Delete: -1Update: 1 |
_version | UInt64 | 1 | Timestamp when data is written to ClickHouse |
- DTS supports only
ReplacingMergeTreeengines:
| CK Type | Cluster Type | Source Has Primary Key | Destination CK Engine |
|---|---|---|---|
| Community Edition | Single Replica | Yes | MergeTree |
| No | ReplacingMergeTree | ||
| Multiple Replicas | Yes | ReplicatedMergeTree | |
| No | ReplicatedReplacingMergeTree |
- For data type mapping, see: https://www.alibabacloud.com/help/en/dts/user-guide/data-type-mappings-for-schema-synchronization
1.4 Select the Appropriate Partition Key
When configuring a MySQL-to-ClickHouse migration task, you can specify a column as the partition key to separate data. ClickHouse creates a new file directory for each key value, logically separating data within a table (e.g., by day). DROP PARTITION allows quick deletion of a data subset.
However, an inappropriate partition key may cause ClickHouse to exceed the limits of the parts_to_throw_insert and max_parts_in_total parameters, leading to the “Too many inactive parts (N). Parts cleaning are processing significantly slower than inserts” error.
To avoid this issue, DTS schema migration includes optimizations:
- If no partition key is specified, no
partition by xxxstatement is added when creating a table. - If a partition key is specified:
- For
BIGINTsource types,partition by intDiv(XXX, 18014398509481984)is added. - For
TinyInt,SmallInt, orMediumIntsource types,partition by intDiv(XXX, 4194304)is added. - For time-based source types (
date,datetime,timestamp),partition by toYYYYMM(update_time)is added. - For other source types, no
partition by xxxstatement is added.
- For
If you don’t use DTS to migrate the tables you created, select an appropriate partition key to avoid the “Too many inactive parts” error during data writing to DTS.
1.5 Select an Appropriate Shard Key
During data writing, the distributed table distributes data to the local table of each host node based on the shard key rules. The shard key must return an integer value, which can be a specific integer column field. If the shard key value is not an integer type, an error will occur when creating a table for schema migration (e.g., Sharding expression has type String, but should be one of integer type.).
To avoid this issue, DTS schema migration includes optimizations:
- If no shard key is specified,
rand()is used. - If a shard key is specified:
- For multiple columns,
sipHash64(A, B, C)is used. - For a single integer column, the column is used.
- For a single non-integer column,
sipHash64(A)is used.
- For multiple columns,
2. Trials and Errors
2.1 What to Do When Data is Duplicated?
ClickHouse doesn’t require a unique primary key, allowing multiple rows with the same primary key value. DTS may introduce duplicate data in the following scenarios:
- During full migration, DTS divides a table’s data into slices for concurrent pulling and writing. If you pause and restart the task while migrating a slice, DTS will re-migrate data from that slice, causing duplication.
- During incremental migration, DTS uses the same solution as the MaterializeMySQL engine, updating/deleting versioning based on the
ReplacingMergeTreeengine. When updating data, an identical row with_sign=1is inserted. When deleting data, an identical row with_sign=-1is inserted. During partition merging,ReplacingMergeTreedeletes duplicate data and retains the row with the largest_versionvalue.
To remove duplicate data, you can:
- Execute the
OPTIMIZE TABLE xxxstatement to force ClickHouse to merge partitions and remove duplicates. - Add
FINALwhen querying data. To filter deleted data, add_sign>0. For example,SELECT * FROM table_name FINAL WHERE _sign>0;
2.2 Why Is the DateTime Data Inconsistent with the Source?
ClickHouse’s time types (Date, Date32, DateTime, and DateTime64) are based on Unix timestamps. If the time in ApsaraDB RDS for MySQL is not within the following ranges, it will be converted to these ranges, potentially causing inconsistency with the source:
| ClickHouse Data Type | Minimum Value | Maximum Value |
|---|---|---|
DateTime64 | 1925-01-01 08:00:00 | 2283-11-12 07:59:59 |
DateTime | 1970-01-01 08:00:00 | 2106-02-07 14:28:15 |
Date32 | 1925-01-01 | 2283-11-11 |
Date | 1970-01-01 | 2149-06-06 |
