Optimize data import and export
Data import and export are common tasks in database applications. This topic covers various tools for this purpose.
Test environment
The benchmarks in this document use the following test environment:
|
Parameter |
Value |
|
PolarDB-X version |
polarx-kernel_5.4.11-16282307_xcluster-20210805 |
|
Node specifications |
16 cores, 64 GB |
|
Number of nodes |
4 |
The test used the following table schema:
CREATE TABLE `sbtest1` (
`id` int(11) NOT NULL,
`k` int(11) NOT NULL DEFAULT '0',
`c` char(120) NOT NULL DEFAULT '',
`pad` char(60) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
KEY `k_1` (`k`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 dbpartition by hash(`id`);
Data import and export tools
Common data export methods for PolarDB-X include:
-
Exporting data using the
mysql -ecommand -
Exporting data using the
SELECT INTO OUTFILEstatement (disabled by default) -
Exporting data using Batch Tool (the dedicated import and export tool for PolarDB-X)
Common data import methods for PolarDB-X include:
-
Importing data using the
SOURCEstatement -
Importing data using the MySQL command
-
Importing data using a program
-
Importing data using the
LOAD DATAstatement -
Importing data using Batch Tool (the dedicated import and export tool for PolarDB-X)
Native MySQL commands
You can use the mysql -e command to connect to a local or remote server, retrieve data by executing SQL statements such as SELECT, process the tab-separated raw output to use commas (',') as delimiters, and save the result as a CSV file. For example:
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sbtest1;" >/home/data_1000w.txt
## The raw data is tab-separated. Format: 188092293 27267211 59775766593-64673028018-...-09474402685 01705051424-...-54211554755
mysql -h ip -P port -u usr -pPassword db_name -N -e "SELECT id,k,c,pad FROM sbtest1;" | sed 's/\t/,/g' >/home/data_1000w.csv
## The CSV file is comma-separated. Format: 188092293,27267211,59775766593-64673028018-...-09474402685,01705051424-...-54211554755
The raw data format is suitable for importing data using the LOAD DATA statement. For more information, see LOAD DATA. The following is an example:
LOAD DATA LOCAL INFILE '/home/data_1000w.txt' INTO TABLE sbtest1;
## LOCAL specifies that the data is imported from a local file. The local_infile parameter must be enabled.
Data in CSV format can be imported by a program. For more information, see Use a program to import data.
mysqldump
The mysqldump tool connects to a local or remote server. For detailed instructions, see mysqldump.
-
Export data example:
mysqldump -h ip -P port -u usr -pPassword --default-character-set=utf8mb4 --net_buffer_length=10240 --no-tablespaces --no-create-db --no-create-info --skip-add-locks --skip-lock-tables --skip-tz-utc --set-charset --hex-blob db_name [table_name] > /home/dump_1000w.sqlThe following are common issues and solutions for exporting data with mysqldump.
-
Issue: mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'gtid\_mode''
Solution: Add
--set-gtid-purged=OFFto disable gtid_mode. -
Issue: mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'ndbinfo\_version''
Solution: Check whether the versions of mysqldump and MySQL are consistent. Use a MySQL client that has the same version as the MySQL server.
The exported data is in the format of SQL statements, which are primarily batch INSERT statements such as
INSERT INTO `sbtest1` VALUES (...),(...), and the "net_buffer_length" parameter affects the batch size. -
-
Use the following methods to import data in SQL format:
Method 1: Use the SOURCE statement to import data source /home/dump_1000w.sql Method 2: Use the mysql command to import data mysql -h ip -P port -u usr -pPassword --default-character-set=utf8mb4 db_name < /home/dump_1000w.sql
Batch Tool
Batch Tool is a data import and export tool developed by Alibaba Cloud that supports multi-threaded operations.
-
Export data:
## By default, the number of exported files is the same as the number of shards. java -jar batch-tool.jar -h ip -P port -u usr -pPassword -D db_name -o export -t sbtest1 -s , ## Export and merge data into a single file. java -jar batch-tool.jar -h ip -P port -u usr -pPassword -D db_name -o export -t sbtest1 -s , -F 1 -
Import data:
## Import 32 files. java -jar batch-tool.jar -hpxc-spryb387va****.polarx.singapore.rds.aliyuncs.com -P3306 -uroot -pPassword -D sysbench_db -o import -t sbtest1 -s , -f "sbtest1_0;sbtest1_1;sbtest1_2;sbtest1_3;sbtest1_4;sbtest1_5;sbtest1_6;sbtest1_7;sbtest1_8;sbtest1_9;sbtest1_10;sbtest1_11;sbtest1_12;sbtest1_13;sbtest1_14;sbtest1_15;sbtest1_16;sbtest1_17;sbtest1_18;sbtest1_19;sbtest1_20;sbtest1_21;sbtest1_22;sbtest1_23;sbtest1_24;sbtest1_25;sbtest1_26;sbtest1_27;sbtest1_28;sbtest1_29;sbtest1_30;sbtest1_31" -np -pro 64 -con 32 ## Import a single file. java -jar batch-tool.jar -h ip -P port -u usr -p password -D db_name -o import -t sbtest1 -s , -f "sbtest1_0" -np
Export method comparison
The following benchmark exports 10 million rows of data, approximately 2 GB in size, from PolarDB-X.
|
Method |
Format |
File size |
Time taken |
Performance (rows/s) |
Performance (MB/s) |
|
|
Raw data format |
1998 MB |
33.417s |
299,248 |
59.8 |
|
|
CSV format |
1998 MB |
34.126s |
293,031 |
58.5 |
|
|
SQL statement format |
2064 MB |
30.223s |
330,873 |
68.3 |
|
|
SQL statement format |
2059 MB |
32.783s |
305,036 |
62.8 |
|
Batch Tool (32 files, one per shard) |
CSV format |
1998 MB |
4.715s |
2,120,890 |
423.7 |
|
Batch Tool (1 file) |
CSV format |
1998 MB |
5.568s |
1,795,977 |
358.8 |
Summary:
-
The
mysql -ecommand and themysqldumptool are primarily single-threaded, and their performance is similar. -
Batch Tool exports data using multiple threads. You can configure the concurrency to significantly improve export performance.
Import method comparison
The following benchmark imports 10 million rows of data, approximately 2 GB in size, into PolarDB-X. The source data was exported in the previous test.
|
Method |
Format |
Time taken |
Performance (rows/s) |
Performance (MB/s) |
|
|
SQL statement format |
10m 24s |
16,025 |
3.2 |
|
|
SQL statement format |
5m 37s |
29,673 |
5.9 |
|
|
SQL statement format |
10m 27s |
15,948 |
3.2 |
|
|
SQL statement format |
5m 38s |
29,585 |
5.9 |
|
|
Raw data format |
4m 0s |
41,666 |
8.3 |
|
Program (batch=1000, thread=1) |
CSV format |
5m 40s |
29,411 |
5.9 |
|
Program (batch=1000, thread=32) |
CSV format |
19s |
526,315 |
105.3 |
|
Batch Tool (32 files, one per shard) |
CSV format |
19.836s |
504,133 |
100.8 |
|
Batch Tool (1 file) |
CSV format |
10.806s |
925,411 |
185.1 |
Summary:
-
The
SOURCEstatement andmysqlcommand methods both import data by running SQL statements in a single thread. They use batchINSERTstatements, and their import performance is affected by the batch size. The batch size depends on thenet-buffer-length parameter set during themysqldumpexport. We recommend the following optimizations:-
Increase thenet-buffer-length parameter to a value no greater than 256 KB. This increases the batch size and improves insertion performance.
-
Use third-party tools that support multi-threaded operations, such as mydumper for backups and myloader for imports.
-
-
The
LOAD DATAstatement is a single-threaded operation, but it performs better than themysqlcommand andSOURCEstatement. -
Program-based imports offer high flexibility. You can configure the batch size and concurrency to achieve better performance. A batch size of 1,000 and a concurrency level of 16 to 32 are recommended.
-
Batch Tool supports multi-threaded imports and is well-suited for the distributed, multi-shard architecture of PolarDB-X, delivering excellent performance.
Conclusion
-
PolarDB-X is compatible with common data import and export methods used in MySQL operations. However, most of these methods are designed for single-node MySQL instances and only support single-threaded operations. They cannot fully utilize the distributed resources of PolarDB-X.
-
PolarDB-X provides Batch Tool, which is ideal for distributed environments. It supports multi-threaded operations and can import and export data extremely fast.