mysql数据库批量高速插入
近期在处理一批数据,须要从库中表里的字段进行处理然后导出到一个新表中。只是这个表的数据量有近500w条。
这数据量出现的的问题是须要处理的时间好长。
首先想到,一句一句的插入,大数据量处理时间好长,忽略。
其次想到,多线程插入,想到数据库连接是须要同步的所以感觉用处不大。
最后想到,使用 PreparedStatement 预编译sql 进行批量插入 batch 处理。
好吧。如今就进行批处理插入測试。
1、使用简单的 batch
public static void main(String[] args) {
Connection conn = getConn(lsqlurl, luser, lpassword);
long startTime = System.currentTimeMillis();
try {
PreparedStatement pst = conn.prepareStatement("insert into testmy (id,name,age) values (?
,?,?
)");
for (int i = 0; i < 2000; i++) {
pst.setInt(1, 3);
pst.setString(2, "xx");
pst.setInt(3, 10);
pst.addBatch();
}
pst.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime)/1000+"s");
System.out.println("test sql batch--->2000.....");
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
你会发现时间会是30s 左右。
2k行的数据插入就30秒 。
2w行数据插入时间为940秒(约16min)。
2、改动自己主动提交的 batch
public static void main(String[] args) {
Connection conn = getConn(lsqlurl, luser, lpassword);
long startTime = System.nanoTime();
try {
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement("insert into test (id,name,age) values (?,?,?
)");
for (int i = 0; i < 2000; i++) {
pst.setInt(1, 3);
pst.setString(2, "xx");
pst.setInt(3, 10);
pst.addBatch();
}
pst.executeBatch();
conn.commit();
long endTime = System.nanoTime();
System.out.println((endTime - startTime)/1000000+"ms");
System.out.println("test sql batch--->2000.....");
} catch (SQLException e) {
try {
conn.rollback();
最新文章
- 基于Mono跨平台移动应用开发框架发布Xamarin 3.0
- 【转】Android Https服务器端和客户端简单实例
- 第八篇.Bootstrap下拉菜单
- 从双十一战报看传统大家电如何O2O转型?
- Android MVP理解
- php编写简单的页面跳转功能
- vs2015上配置Armadillo+openBlas
- Oracle函数题
- 很好的复习资料: SQL语句到底怎么写 ?
- Android 优化APP 构建速度的17条建议
- vfd with stm8
- 我们来谈谈最近最热门的微信小程序
- P2279 [HNOI2003]消防局的设立
- vs中正常IIS发布网站后css样式、图片丢失jQuery报错 $ is not defined
- Robust Real-time Object Detection学习
- 算法笔记--斜率优化dp
- oracle参数文件与启动过程
- java 错误:无法找到或装入主类
- HDU 1104 Remainder (BFS求最小步数 打印路径)
- pg_buffercache
热门文章
- [WDS] Warnings while compiling. vue项目运行控制台输出太多警告信息
- 无法完成安装:&#39;Cannot access storage file &#39;/
- vue开发调试工具vue-devtools安装
- Web项目ConcurrentModificationException异常
- [GXOI/GZOI2019]与或和(单调栈)
- 关于Integer,127和128的问题
- wepy 编译警告去除办法
- 客户端用plsql进行中文条件查询时无结果的解决办法
- Spring3.2+mybatis3.2+Struts2.3整合配置文件大全
- Linux网络设置