- Docs Home
- About TiDB
- Quick Start
- Develop
- Overview
- Quick Start
- Build a TiDB Cluster in TiDB Cloud (Developer Tier)
- CRUD SQL in TiDB
- Build a Simple CRUD App with TiDB
- Example Applications
- Connect to TiDB
- Design Database Schema
- Write Data
- Read Data
- Transaction
- Optimize
- Troubleshoot
- Reference
- Cloud Native Development Environment
- Third-party Support
- Deploy
- Software and Hardware Requirements
- Environment Configuration Checklist
- Plan Cluster Topology
- Install and Start
- Verify Cluster Status
- Test Cluster Performance
- Migrate
- Overview
- Migration Tools
- Migration Scenarios
- Migrate from Aurora
- Migrate MySQL of Small Datasets
- Migrate MySQL of Large Datasets
- Migrate and Merge MySQL Shards of Small Datasets
- Migrate and Merge MySQL Shards of Large Datasets
- Migrate from CSV Files
- Migrate from SQL Files
- Migrate from One TiDB Cluster to Another TiDB Cluster
- Migrate from TiDB to MySQL-compatible Databases
- Advanced Migration
- Integrate
- Maintain
- Monitor and Alert
- Troubleshoot
- TiDB Troubleshooting Map
- Identify Slow Queries
- Analyze Slow Queries
- SQL Diagnostics
- Identify Expensive Queries Using Top SQL
- Identify Expensive Queries Using Logs
- Statement Summary Tables
- Troubleshoot Hotspot Issues
- Troubleshoot Increased Read and Write Latency
- Save and Restore the On-Site Information of a Cluster
- Troubleshoot Cluster Setup
- Troubleshoot High Disk I/O Usage
- Troubleshoot Lock Conflicts
- Troubleshoot TiFlash
- Troubleshoot Write Conflicts in Optimistic Transactions
- Troubleshoot Inconsistency Between Data and Indexes
- Performance Tuning
- Tuning Guide
- Configuration Tuning
- System Tuning
- Software Tuning
- SQL Tuning
- Overview
- Understanding the Query Execution Plan
- SQL Optimization Process
- Overview
- Logic Optimization
- Physical Optimization
- Prepare Execution Plan Cache
- Control Execution Plans
- Tutorials
- TiDB Tools
- Overview
- Use Cases
- Download
- TiUP
- Documentation Map
- Overview
- Terminology and Concepts
- Manage TiUP Components
- FAQ
- Troubleshooting Guide
- Command Reference
- Overview
- TiUP Commands
- TiUP Cluster Commands
- Overview
- tiup cluster audit
- tiup cluster check
- tiup cluster clean
- tiup cluster deploy
- tiup cluster destroy
- tiup cluster disable
- tiup cluster display
- tiup cluster edit-config
- tiup cluster enable
- tiup cluster help
- tiup cluster import
- tiup cluster list
- tiup cluster patch
- tiup cluster prune
- tiup cluster reload
- tiup cluster rename
- tiup cluster replay
- tiup cluster restart
- tiup cluster scale-in
- tiup cluster scale-out
- tiup cluster start
- tiup cluster stop
- tiup cluster template
- tiup cluster upgrade
- TiUP DM Commands
- Overview
- tiup dm audit
- tiup dm deploy
- tiup dm destroy
- tiup dm disable
- tiup dm display
- tiup dm edit-config
- tiup dm enable
- tiup dm help
- tiup dm import
- tiup dm list
- tiup dm patch
- tiup dm prune
- tiup dm reload
- tiup dm replay
- tiup dm restart
- tiup dm scale-in
- tiup dm scale-out
- tiup dm start
- tiup dm stop
- tiup dm template
- tiup dm upgrade
- TiDB Cluster Topology Reference
- DM Cluster Topology Reference
- Mirror Reference Guide
- TiUP Components
- PingCAP Clinic Diagnostic Service
- TiDB Operator
- Dumpling
- TiDB Lightning
- TiDB Data Migration
- About TiDB Data Migration
- Architecture
- Quick Start
- Deploy a DM cluster
- Tutorials
- Advanced Tutorials
- Maintain
- Cluster Upgrade
- Tools
- Performance Tuning
- Manage Data Sources
- Manage Tasks
- Export and Import Data Sources and Task Configurations of Clusters
- Handle Alerts
- Daily Check
- Reference
- Architecture
- Command Line
- Configuration Files
- OpenAPI
- Compatibility Catalog
- Secure
- Monitoring and Alerts
- Error Codes
- Glossary
- Example
- Troubleshoot
- Release Notes
- Backup & Restore (BR)
- TiDB Binlog
- TiCDC
- Dumpling
- sync-diff-inspector
- TiSpark
- Reference
- Cluster Architecture
- Key Monitoring Metrics
- Secure
- Privileges
- SQL
- SQL Language Structure and Syntax
- SQL Statements
ADD COLUMN
ADD INDEX
ADMIN
ADMIN CANCEL DDL
ADMIN CHECKSUM TABLE
ADMIN CHECK [TABLE|INDEX]
ADMIN SHOW DDL [JOBS|QUERIES]
ADMIN SHOW TELEMETRY
ALTER DATABASE
ALTER INDEX
ALTER INSTANCE
ALTER PLACEMENT POLICY
ALTER TABLE
ALTER TABLE COMPACT
ALTER USER
ANALYZE TABLE
BACKUP
BATCH
BEGIN
CHANGE COLUMN
COMMIT
CHANGE DRAINER
CHANGE PUMP
CREATE [GLOBAL|SESSION] BINDING
CREATE DATABASE
CREATE INDEX
CREATE PLACEMENT POLICY
CREATE ROLE
CREATE SEQUENCE
CREATE TABLE LIKE
CREATE TABLE
CREATE USER
CREATE VIEW
DEALLOCATE
DELETE
DESC
DESCRIBE
DO
DROP [GLOBAL|SESSION] BINDING
DROP COLUMN
DROP DATABASE
DROP INDEX
DROP PLACEMENT POLICY
DROP ROLE
DROP SEQUENCE
DROP STATS
DROP TABLE
DROP USER
DROP VIEW
EXECUTE
EXPLAIN ANALYZE
EXPLAIN
FLASHBACK TABLE
FLUSH PRIVILEGES
FLUSH STATUS
FLUSH TABLES
GRANT <privileges>
GRANT <role>
INSERT
KILL [TIDB]
LOAD DATA
LOAD STATS
MODIFY COLUMN
PREPARE
RECOVER TABLE
RENAME INDEX
RENAME TABLE
REPLACE
RESTORE
REVOKE <privileges>
REVOKE <role>
ROLLBACK
SELECT
SET DEFAULT ROLE
SET [NAMES|CHARACTER SET]
SET PASSWORD
SET ROLE
SET TRANSACTION
SET [GLOBAL|SESSION] <variable>
SHOW ANALYZE STATUS
SHOW [BACKUPS|RESTORES]
SHOW [GLOBAL|SESSION] BINDINGS
SHOW BUILTINS
SHOW CHARACTER SET
SHOW COLLATION
SHOW [FULL] COLUMNS FROM
SHOW CONFIG
SHOW CREATE PLACEMENT POLICY
SHOW CREATE SEQUENCE
SHOW CREATE TABLE
SHOW CREATE USER
SHOW DATABASES
SHOW DRAINER STATUS
SHOW ENGINES
SHOW ERRORS
SHOW [FULL] FIELDS FROM
SHOW GRANTS
SHOW INDEX [FROM|IN]
SHOW INDEXES [FROM|IN]
SHOW KEYS [FROM|IN]
SHOW MASTER STATUS
SHOW PLACEMENT
SHOW PLACEMENT FOR
SHOW PLACEMENT LABELS
SHOW PLUGINS
SHOW PRIVILEGES
SHOW [FULL] PROCESSSLIST
SHOW PROFILES
SHOW PUMP STATUS
SHOW SCHEMAS
SHOW STATS_HEALTHY
SHOW STATS_HISTOGRAMS
SHOW STATS_META
SHOW STATUS
SHOW TABLE NEXT_ROW_ID
SHOW TABLE REGIONS
SHOW TABLE STATUS
SHOW [FULL] TABLES
SHOW [GLOBAL|SESSION] VARIABLES
SHOW WARNINGS
SHUTDOWN
SPLIT REGION
START TRANSACTION
TABLE
TRACE
TRUNCATE
UPDATE
USE
WITH
- Data Types
- Functions and Operators
- Overview
- Type Conversion in Expression Evaluation
- Operators
- Control Flow Functions
- String Functions
- Numeric Functions and Operators
- Date and Time Functions
- Bit Functions and Operators
- Cast Functions and Operators
- Encryption and Compression Functions
- Locking Functions
- Information Functions
- JSON Functions
- Aggregate (GROUP BY) Functions
- Window Functions
- Miscellaneous Functions
- Precision Math
- Set Operations
- List of Expressions for Pushdown
- TiDB Specific Functions
- Clustered Indexes
- Constraints
- Generated Columns
- SQL Mode
- Table Attributes
- Transactions
- Garbage Collection (GC)
- Views
- Partitioning
- Temporary Tables
- Cached Tables
- Character Set and Collation
- Placement Rules in SQL
- System Tables
mysql
- INFORMATION_SCHEMA
- Overview
ANALYZE_STATUS
CLIENT_ERRORS_SUMMARY_BY_HOST
CLIENT_ERRORS_SUMMARY_BY_USER
CLIENT_ERRORS_SUMMARY_GLOBAL
CHARACTER_SETS
CLUSTER_CONFIG
CLUSTER_HARDWARE
CLUSTER_INFO
CLUSTER_LOAD
CLUSTER_LOG
CLUSTER_SYSTEMINFO
COLLATIONS
COLLATION_CHARACTER_SET_APPLICABILITY
COLUMNS
DATA_LOCK_WAITS
DDL_JOBS
DEADLOCKS
ENGINES
INSPECTION_RESULT
INSPECTION_RULES
INSPECTION_SUMMARY
KEY_COLUMN_USAGE
METRICS_SUMMARY
METRICS_TABLES
PARTITIONS
PLACEMENT_POLICIES
PROCESSLIST
REFERENTIAL_CONSTRAINTS
SCHEMATA
SEQUENCES
SESSION_VARIABLES
SLOW_QUERY
STATISTICS
TABLES
TABLE_CONSTRAINTS
TABLE_STORAGE_STATS
TIDB_HOT_REGIONS
TIDB_HOT_REGIONS_HISTORY
TIDB_INDEXES
TIDB_SERVERS_INFO
TIDB_TRX
TIFLASH_REPLICA
TIKV_REGION_PEERS
TIKV_REGION_STATUS
TIKV_STORE_STATUS
USER_PRIVILEGES
VIEWS
METRICS_SCHEMA
- UI
- TiDB Dashboard
- Overview
- Maintain
- Access
- Overview Page
- Cluster Info Page
- Top SQL Page
- Key Visualizer Page
- Metrics Relation Graph
- SQL Statements Analysis
- Slow Queries Page
- Cluster Diagnostics
- Search Logs Page
- Instance Profiling
- Session Management and Configuration
- FAQ
- CLI
- Command Line Flags
- Configuration File Parameters
- System Variables
- Storage Engines
- Telemetry
- Errors Codes
- Table Filter
- Schedule Replicas by Topology Labels
- FAQs
- Release Notes
- All Releases
- Release Timeline
- TiDB Versioning
- v6.1
- v6.0
- v5.4
- v5.3
- v5.2
- v5.1
- v5.0
- v4.0
- v3.1
- v3.0
- v2.1
- v2.0
- v1.0
- Glossary
Merge and Migrate Data from Sharded Tables in the Pessimistic Mode
This document introduces the sharding support feature provided by Data Migration (DM) in the pessimistic mode (the default mode). This feature allows you to merge and migrate the data of tables with the same table schema in the upstream MySQL or MariaDB instances into one same table in the downstream TiDB.
Restrictions
DM has the following sharding DDL usage restrictions in the pessimistic mode:
- For a logical sharding group (composed of all sharded tables that need to be merged and migrated into one same downstream table), it is limited to use one task containing exactly the sources of sharded tables to perform the migration.
- In a logical sharding group, the same DDL statements must be executed in the same order in all upstream sharded tables (the schema name and the table name can be different), and the next DDL statement cannot be executed unless the current DDL operation is completely finished.
- For example, if you add
column A
totable_1
before you addcolumn B
, then you cannot addcolumn B
totable_2
before you addcolumn A
. Executing the DDL statements in a different order is not supported.
- For example, if you add
- In a sharding group, the corresponding DDL statements should be executed in all upstream sharded tables.
- For example, if DDL statements are not executed on one or more upstream sharded tables corresponding to
DM-worker-2
, then other DM-workers that have executed the DDL statements pause their migration task and wait forDM-worker-2
to receive the upstream DDL statements.
- For example, if DDL statements are not executed on one or more upstream sharded tables corresponding to
- The sharding group migration task does not support
DROP DATABASE
/DROP TABLE
.- The sync unit in DM-worker automatically ignores the
DROP DATABASE
/DROP TABLE
statement of upstream sharded tables.
- The sync unit in DM-worker automatically ignores the
- The sharding group migration task does not support
TRUNCATE TABLE
.- The sync unit in DM-worker automatically ignores the
TRUNCATE TABLE
statement of upstream sharded tables.
- The sync unit in DM-worker automatically ignores the
- The sharding group migration task supports
RENAME TABLE
, but with the following limitations (online DDL is supported in another solution):- A table can only be renamed to a new name that is not used by any other table.
- A single
RENAME TABLE
statement can only involve a singleRENAME
operation.
- The sharding group migration task requires each DDL statement to involve operations on only one table.
- The table schema of each sharded table must be the same at the starting point of the incremental replication task, so as to make sure the DML statements of different sharded tables can be migrated into the downstream with a definite table schema, and the subsequent sharding DDL statements can be correctly matched and migrated.
- If you need to change the table routing rule, you have to wait for the migration of all sharding DDL statements to complete.
- During the migration of sharding DDL statements, an error is reported if you use
dmctl
to changerouter-rules
.
- During the migration of sharding DDL statements, an error is reported if you use
- If you need to
CREATE
a new table to a sharding group where DDL statements are being executed, you have to make sure that the table schema is the same as the newly modified table schema.- For example, both the original
table_1
andtable_2
have two columns (a, b) initially, and have three columns (a, b, c) after the sharding DDL operation, so after the migration the newly created table should also have three columns (a, b, c).
- For example, both the original
- Because the DM-worker that has received the DDL statements will pause the task to wait for other DM-workers to receive their DDL statements, the delay of data migration will be increased.
Background
Currently, DM uses the binlog in the ROW
format to perform the migration task. The binlog does not contain the table schema information. When you use the ROW
binlog to migrate data, if you have not migrated multiple upstream tables into the same downstream table, then there only exist DDL operations of one upstream table that can update the table schema of the downstream table. The ROW
binlog can be considered to have the nature of self-description. During the migration process, the DML statements can be constructed accordingly with the column values and the downstream table schema.
However, in the process of merging and migrating sharded tables, if DDL statements are executed on the upstream tables to modify the table schema, then you need to perform extra operations to migrate the DDL statements so as to avoid the inconsistency between the DML statements produced by the column values and the actual downstream table schema.
Here is a simple example:
In the above example, the merging process is simplified, where only two MySQL instances exist in the upstream and each instance has only one table. When the migration begins, the table schema version of two sharded tables is marked as schema V1
, and the table schema version after executing DDL statements is marked as schema V2
.
Now assume that in the migration process, the binlog data received from the two upstream sharded tables has the following time sequence:
- When the migration begins, the sync unit in DM-worker receives the DML events of
schema V1
from the two sharded tables. - At
t1
, the sharding DDL events from instance 1 are received. - From
t2
on, the sync unit receives the DML events ofschema V2
from instance 1; but from instance 2, it still receives the DML events ofschema V1
. - At
t3
, the sharding DDL events from instance 2 are received. - From
t4
on, the sync unit receives the DML events ofschema V2
from instance 2 as well.
Assume that the DDL statements of sharded tables are not processed during the migration process. After DDL statements of instance 1 are migrated to the downstream, the downstream table schema is changed to schema V2
. But for instance 2, the sync unit in DM-worker is still receiving DML events of schema V1
from t2
to t3
. Therefore, when the DML statements of schema V1
are migrated to the downstream, the inconsistency between the DML statements and the table schema can cause errors and the data cannot be migrated successfully.
Principles
This section shows how DM migrates DDL statements in the process of merging sharded tables based on the above example in the pessimistic mode.
In this example, DM-worker-1
migrates the data from MySQL instance 1 and DM-worker-2
migrates the data from MySQL instance 2. DM-master
coordinates the DDL migration among multiple DM-workers. Starting from DM-worker-1
receiving the DDL statements, the DDL migration process is simplified as follows:
DM-worker-1
receives the DDL statement from MySQL instance 1 att1
, pauses the data migration of the corresponding DDL and DML statements, and sends the DDL information toDM-master
.DM-master
decides that the migration of this DDL statement needs to be coordinated based on the received DDL information, creates a lock for this DDL statement, sends the DDL lock information back toDM-worker-1
and marksDM-worker-1
as the owner of this lock at the same time.DM-worker-2
continues migrating the DML statement until it receives the DDL statement from MySQL instance 2 att3
, pauses the data migration of this DDL statement, and sends the DDL information toDM-master
.DM-master
decides that the lock of this DDL statement already exists based on the received DDL information, and sends the lock information directly toDM-worker-2
.- Based on the configuration information when the task is started, the sharded table information in the upstream MySQL instances, and the deployment topology information,
DM-master
decides that it has received this DDL statement of all upstream sharded tables to be merged, and requests the owner of the DDL lock (DM-worker-1
) to migrate this DDL statement to the downstream. DM-worker-1
verifies the DDL statement execution request based on the DDL lock information received at Step #2, migrates this DDL statement to the downstream, and sends the results toDM-master
. If this operation is successful,DM-worker-1
continues migrating the subsequent (starting from the binlog att2
) DML statements.DM-master
receives the response from the lock owner that the DDL is successfully executed, and requests all other DM-workers (DM-worker-2
) that are waiting for the DDL lock to ignore this DDL statement and then continue to migrate the subsequent (starting from the binlog att4
) DML statements.
The characteristics of DM handling the sharding DDL migration among multiple DM-workers can be concluded as follows:
- Based on the task configuration and DM cluster deployment topology information, a logical sharding group is built in
DM-master
to coordinate DDL migration. The group members are DM-workers that handle each sub-task divided from the migration task). - After receiving the DDL statement from the binlog event, each DM-worker sends the DDL information to
DM-master
. DM-master
creates or updates the DDL lock based on the DDL information received from each DM-worker and the sharding group information.- If all members of the sharding group receive a same specific DDL statement, this indicates that all DML statements before the DDL execution on the upstream sharded tables have been completely migrated, and this DDL statement can be executed. Then DM can continue to migrate the subsequent DML statements.
- After being converted by the table router, the DDL statement of the upstream sharded tables must be consistent with the DDL statement to be executed in the downstream. Therefore, this DDL statement only needs to be executed once by the DDL owner and all other DM-workers can ignore this DDL statement.
In the above example, only one sharded table needs to be merged in the upstream MySQL instance corresponding to each DM-worker. But in actual scenarios, there might be multiple sharded tables in multiple sharded schemas to be merged in one MySQL instance. And when this happens, it becomes more complex to coordinate the sharding DDL migration.
Assume that there are two sharded tables, namely table_1
and table_2
, to be merged in one MySQL instance:
Because data comes from the same MySQL instance, all the data is obtained from the same binlog stream. In this case, the time sequence is as follows:
- The sync unit in DM-worker receives the DML statements of
schema V1
from both sharded tables when the migration begins. - At
t1
, the sync unit in DM-worker receives the DDL statements oftable_1
. - From
t2
tot3
, the received data includes the DML statements ofschema V2
fromtable_1
and the DML statements ofschema V1
fromtable_2
. - At
t3
, the sync unit in DM-worker receives the DDL statements oftable_2
. - From
t4
on, the sync unit in DM-worker receives the DML statements ofschema V2
from both tables.
If the DDL statements are not processed particularly during the data migration, when the DDL statement of table_1
is migrated to the downstream and changes the downstream table schema, the DML statement of schema V1
from table_2
cannot be migrated successfully. Therefore, within a single DM-worker, a logical sharding group similar to that within DM-master
is created, except that members of this group are different sharded tables in the same upstream MySQL instance.
But when a DM-worker coordinates the migration of the sharding group within itself, it is not totally the same as that performed by DM-master
. The reasons are as follows:
- When the DM-worker receives the DDL statement of
table_1
, it cannot pause the migration and needs to continue parsing the binlog to get the subsequent DDL statements oftable_2
. This means it needs to continue parsing betweent2
andt3
. - During the binlog parsing process between
t2
andt3
, the DML statements ofschema V2
fromtable_1
cannot be migrated to the downstream until the sharding DDL statement is migrated and successfully executed.
In DM, the simplified migration process of sharding DDL statements within the DM worker is as follows:
- When receiving the DDL statement of
table_1
att1
, the DM-worker records the DDL information and the current position of the binlog. - DM-worker continues parsing the binlog between
t2
andt3
. - DM-worker ignores the DML statement with the
schema V2
schema that belongs totable_1
, and migrates the DML statement with theschema V1
schema that belongs totable_2
to the downstream. - When receiving the DDL statement of
table_2
att3
, the DM-worker records the DDL information and the current position of the binlog. - Based on the information of the migration task configuration and the upstream schemas and tables, the DM-worker decides that the DDL statements of all sharded tables in the MySQL instance have been received and migrates them to the downstream to modify the downstream table schema.
- DM-worker sets the starting point of parsing the new binlog stream to be the position saved at Step #1.
- DM-worker resumes parsing the binlog between
t2
andt3
. - DM-worker migrates the DML statement with the
schema V2
schema that belongs totable_1
to the downstream, and ignores the DML statement with theschema V1
schema that belongs totable_2
. - After parsing the binlog position saved at Step #4, the DM-worker decides that all DML statements that have been ignored in Step #3 have been migrated to the downstream again.
- DM-worker resumes the migration starting from the binlog position at
t4
.
You can conclude from the above analysis that DM mainly uses two-level sharding groups for coordination and control when handling migration of the sharding DDL. Here is the simplified process:
- Each DM-worker independently coordinates the DDL statements migration for the corresponding sharding group composed of multiple sharded tables within the upstream MySQL instance.
- After the DM-worker receives the DDL statements of all sharded tables, it sends the DDL information to
DM-master
. DM-master
coordinates the DDL migration of the sharding group composed of the DM-workers based on the received DDL information.- After receiving the DDL information from all DM-workers,
DM-master
requests the DDL lock owner (a specific DM-worker) to execute the DDL statement. - The DDL lock owner executes the DDL statement and returns the result to
DM-master
. Then the owner restarts the migration of the previously ignored DML statements during the internal coordination of DDL migration. - After
DM-master
confirms that the owner has successfully executed the DDL statement, it asks all other DM-workers to continue the migration. - All other DM-workers separately restart the migration of the previously ignored DML statements during the internal coordination of DDL migration.
- After finishing migrating the ignored DML statements again, all DM-workers resume the normal migration process.