最近系统新上线,有个近4百万的数据需要导出导入。试过Navicat数据传输,发现很慢,而且导入导出的时候会影响数据库性能,系统卡顿
查询资料mysql有load data infile功能,这种导入速率比较快。
导出
select * from table1 into outfile '/home/temp/table1.txt'
导出的时候可能会提示报错:需要导出到mysql指定导出目录
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
查看指定目录:
show variables like 'secure_file_priv'
导入
load data infile '/home/temp/table2.txt' into table table2
-- 指定字段导入
load data infile '/home/temp/table2.txt' into table table2(column1, column2)
结果

上述导入会出现id冲突,如果需要特殊处理有两种选择:
ignore:忽略id唯一键冲突的,不执行导入
load data infile '/home/temp/table2.txt' ignore into table table2
replace:覆盖id唯一键冲突的,不执行导入
load data infile '/home/temp/table2.txt' replace into table table2

文章讲述了在新系统上线时遇到的大规模数据导入导出问题,通过使用MySQL的loaddatainfile功能提高速度,但遇到`secure_file_priv`权限限制和ID冲突问题,介绍了ignore和replace两种处理方式。

被折叠的 条评论
为什么被折叠?



