记一次百万数据插入的优化

2021-12-28  本文已影响0人  lz做过前端

说明

环境

JDBC 方式插入

statement

预处理

多线程多连接 + statement

多线程多连接 + 预处理

总结

LOAD DATA 方式插入(mysql版本8.0.18)

mysql -h 127.0.0.1 -uroot -P 3406 --local-infile=1 -p
SET GLOBAL local_infile = 1;
load data local infile '~/Downloads/order.csv' into table test.`order`
    character set utf8mb4 -- 可选,避免中文乱码问题
    fields terminated by ',' -- 字段分隔符,每个字段(列)以什么字符分隔,默认是 \t
    optionally enclosed by '' -- 文本限定符,每个字段被什么字符包围,默认是空字符
    escaped by '/' -- 转义符,默认是 \
    lines terminated by '\n' -- 记录分隔符,如字段本身也含\n,那么应先去除,否则load data
    ( `user_id`, `status`, `money`) -- 每一行文本按顺序对应的表字段,建议不要省略
    ;
Query OK, 1000000 rows affected, 0 warnings (8.61 sec)
Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0
Query OK, 1000000 rows affected, 0 warnings (4.95 sec)
Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0

importTable 方式插入(MySQL Shell mysql版本8.0.27)

## 进入 mysql shell 
mysqlsh
## 进入后 session 连接
shell.connect('mysql://root@localhost:3308')
## 然后输入密码进入
## 导出
util.exportTable("test.`order`", "~/Downloads/order.csv")
## 导入
## The parallel table import utility uses LOAD DATA LOCAL INFILE statements to upload data, 
## so the local_infile system variable must be set to ON on the target server. 
## You can do this by issuing the following statement in SQL mode before running the parallel table import utility
\sql SET GLOBAL local_infile = 1;
util.importTable("~/Downloads/order.csv", {"characterSet": "utf8mb4","schema": "test","table": "order"})
Importing from file '/Users/xuqingbin/Downloads/order.csv' to table `test`.`order` in MySQL Server at localhost:3308 using 1 thread
[Worker000] order.csv: Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0
100% (19.89 MB / 19.89 MB), 143.13 KB/s
File '/Users/xuqingbin/Downloads/order.csv' (19.89 MB) was imported in 11.5021 sec at 1.73 MB/s
Total rows affected in test.order: Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0
Importing from file '/Users/xuqingbin/Downloads/order.csv' to table `test`.`order` in MySQL Server at localhost:3406 using 1 thread
[Worker000] order.csv: Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0
100% (19.89 MB / 19.89 MB), 4.38 MB/s
File '/Users/xuqingbin/Downloads/order.csv' (19.89 MB) was imported in 4.8136 sec at 4.13 MB/s
Total rows affected in test.order: Records: 1000000  Deleted: 0  Skipped: 0  Warnings: 0

优化过程

优化思路可以归结为

以下是具体说明

Inserting indexes

foreign key

UNIQUE

columns set default values

change buffer

undo log

redo log

bin log

    [mysqld]
    skip-log-bin

max_allowed_packet

[mysqld]
## 100M
max_allowed_packet=104857600

bulk_insert_buffer_size

[mysqld]
## 100M
bulk_insert_buffer_size=104857600

最后

上一篇 下一篇

猜你喜欢

热点阅读