美文网首页
导入MySQL方法对比

导入MySQL方法对比

作者: 南风nanfeng | 来源:发表于2020-02-20 11:19 被阅读0次

使用datax导入

1、使用自增id

记录数 耗时(秒) TPS
4248882 347 12244
33404813 4038 8272

2、不使用自增id,使用雪花算法生成id后导入

记录数 耗时(秒) TPS
4248882 680 6248
33404813 6555 5096

使用MySQL命令load导入

1、csv带雪花id

> select * from l_wish_log_snow_copy into outfile '/data2/l_wish_log_snow.csv' fields terminated by '\t' optionally enclosed by '"' lines terminated by '\n';
> load data infile '/data2/l_wish_log_snow.csv' into table l_wish_log_snow fields terminated by '\t' optionally enclosed by '"' lines terminated by '\n';
记录数 耗时(秒) TPS
4248882 81 52,455

2、自增主键id,load命令
先删除上个步骤中导出的雪花id

> awk -F'\t' '{print $2 "\t" $3 "\t" $4 "\t" $5 "\t" $6 "\t" $7 "\t" $8 "\t" $9 "\t" $10 "\t" $11 "\t" $12 "\t" $13 "\t" $14}' l_wish_log_snow.csv  > noid.csv

使用awk分隔符切割,不要第一列,然后连起来就好了,生成不包含id的noid.csv文件,做下一步动作,导入到自增id的表中。

load data infile '/data2/l_wish_log_bak.csv' into table l_wish_log_snow_copy fields terminated by '\t' optionally enclosed by '"' lines terminated by '\n'(deviceid,uid,kid,level,createtime,fighting,version,devicetype,sysversion,platform,num,data,castType);
记录数 耗时(秒) TPS
4248882 90 47210

使用load导入33404813行记录时,发现无法导入,报错如下:

mysql> load data infile '/data2/l_add_item_snow.csv' into table l_add_item_log_snow fields terminated by '\t' optionally enclosed by '"' lines terminated by '\n';
ERROR 1197 (HY000): Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage; increase this mysqld variable and try again

MySQL的Innodb引擎是事务型,load文件操作当作一个事务来处理,中途中断load操作,会导致回滚。
max_binlog_cache_size表示能够使用的最大cache内存大小。
当执行多语句事务时,max_binlog_cache_size如果不够大,就会报错,如上所示。
所有产生的binlog会先写入binlog_cache_size,直到load data 的操作结束后,
最后,再由binlog_cache_size 写入二进制日志,如mysql-bin.0000008等。
所以此参数的大小必须大于所要load 的文件的大小,或者当前所要进行的事务操作的大小。

改大max_binlog_cache_size 和binlog_cache_size 问题解决:

mysql> show variables like '%binlog_cache_size';
+-----------------------+------------+
| Variable_name         | Value      |
+-----------------------+------------+
| binlog_cache_size     | 20971520   |
| max_binlog_cache_size | 2147483648 |
+-----------------------+------------+
2 rows in set (0.01 sec)
mysql> SET GLOBAL max_binlog_cache_size =2147483648;
mysql> SET GLOBAL binlog_cache_size =20971520;

很可惜,33404813行记录的文件有11g,也就是说binlog的缓存区要设置大于11g。
那么问题来了,能不能关闭binlog呢,就不用写入缓冲区和binlog文件了。
但是测试环境及线上环境MySQL一般都是主从架构,不允许这么玩。

从测试结果来看,load file入库性能最高,但是主从架构入库大表需要配置很大的缓冲区,其次是使用datax导入,奇怪的是使用自增id的性能比雪花id性能稍高,使用load雪花id的写入比自增id稍高,这次对比使用的是innodb引擎,看来说自增id新能差的不可信,并没有体现出量级差的性能。

相关文章

网友评论

      本文标题:导入MySQL方法对比

      本文链接:https://www.haomeiwen.com/subject/kowofhtx.html