MySQL批量插入4千万条数据,想问下各位大佬还有没有优化的可能

目前有15个txt文件,前14个都有300万行插入语句,插入语句示例如下

INSERT INTO sys_user(id,username,nickname,gender,password,avatar,mobile,status,email,is_deleted,content) VALUES(null,'15664152532','激动的篮球',1,'123456','http://','15664152532',1,'15664152532@qq.com',0,'null');
INSERT INTO sys_user(id,username,nickname,gender,password,avatar,mobile,status,email,is_deleted,content) VALUES(null,'18209582100','跳跃的汽车',1,'123456','http://','18209582100',1,'18209582100@qq.com',0,'null');
INSERT INTO sys_user(id,username,nickname,gender,password,avatar,mobile,status,email,is_deleted,content) VALUES(null,'15278230034','暴躁的啤酒',1,'123456','http://','15278230034',1,'15278230034@qq.com',0,'null');


我采用了3个线程同时处理三个txt文件,900万条数据一共花了2个小时,

private static void batchInsert(String filePath) throws SQLException {
Connection connection = null;
Statement statement = null;
try {
// 连接到MySQL数据库
connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);

// 关闭自动提交,提高性能
connection.setAutoCommit(false);

// 从txt文件中读取插入语句
List<String> insertStatements = readInsertStatementsFromTxt(filePath);

// 使用 Statement,不支持动态参数设置
statement = connection.createStatement();
int i = 0;
// 插入数据
for (String sql : insertStatements) {
i++;
// 添加到批处理
statement.addBatch(sql);
//每1000条提交一次并清空
if (i % 100000 == 0) {
// 执行批处理
statement.executeBatch();
connection.commit();
statement.clearBatch();
}
}
statement.executeBatch();
connection.commit();
System.out.println(filePath + insertStatements.size() + "Data inserted successfully.");
} catch (SQLException | IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) throws SQLException {
long start = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(3);
//Future<?> future1 = executor.submit(() -> {
// try {
// batchInsert("sql.txt");
// System.out.println("sql文件写入成功");
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//});
//Future<?> future2 = executor.submit(() -> {
// try {
// batchInsert("sql1.txt");
// System.out.println("sql1文件写入成功");
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//});
//Future<?> future3 = executor.submit(() -> {
// try {
// batchInsert("sql2.txt");
// System.out.println("sql2文件写入成功");
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//});
Future<?> future4 = executor.submit(() -> {
try {
batchInsert("sql3.txt");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Future<?> future5 = executor.submit(() -> {
try {
batchInsert("sql4.txt");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Future<?> future6 = executor.submit(() -> {
try {
batchInsert("sql5.txt");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
try {
//future1.get();
//future2.get();
//future3.get();
future4.get();
future5.get();
future6.get();
System.out.println("所有任务执行完毕");
} catch (InterruptedException | ExecutionException e) {
System.err.println("An error occurred while executing tasks: " + e.getMessage());
e.printStackTrace();
}
System.out.println("合计用时:" + (System.currentTimeMillis() - start));
}



0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
清风
内容推荐
物流智能体工程师-基于浏览器操作的多平台箱号查询与表单提交方案
3
突然有点迷茫了,本科毕业4年,专业是计算机类,干过客服、干过技术支持,现在在干巡查性质的工作,下完班回来能抽个两三小时学习,目前学完了Java基础、面向对象、集合、异常、IO流、线程等内容,也完成了Mysql的语法学习、知道了索引、事务相关的内容。现在正在攻略学习路线里的SQL之母的自定义关卡(实战进阶),进度完成了2/3,到明天就能结束SQL部分的学习。我三年前找过王道培训班学的Java后端,但
5
问题描述我们评测一个功能是否正确,应该怎么写测试呢?特别是在AI时代,不人工review代码的情况 如何更好的使用测试来验收代码呢具体疑问平时都用AI写代码,随着需求增加,每次生成的代码都看不过来,只能借助AI生成测试来验收一下。但是测试也是AI生成的,我并不知道AI生成的测试是否符合我的需求,我也不太清楚,应该生成哪几种类型的测试才能更好的评估我们开发的功能。已有理解目前让AI写的有单元测试,集
4
想问一下大家,之前是在星球上充值了,然后俩年前没续费了,最近想学习所以在原来星球上续费了,发现没有人和项目提问和更新,才发现编程导航现在单独有一个网站,续费的会员不会同步吗
0
我想问一下大家简历的字体是多少号的呢,是2页还是3页
2
下载 APP