新之助meow

Kratos
专注于用户阅读体验的响应式博客主题
  1. 首页
  2. 未分类
  3. 正文

java mysql 用addBatch和executeBatch优化批量插入

2015年12月20日 572点热度 0人点赞 0条评论

注意

1 最新的jdbc驱动

2 连接url 中 rewriteBatchedStatements=true

3 每批数据大于3

http://www.cnblogs.com/chenjianjx/archive/2012/08/14/2637914.html

实验记录:打开了rewriteBatchedStatements后

打开rewriteBatchedStatements后,根据wireshark嗅探出的mysql报文可以看出

batchDelete(10条记录)  =>  发送一次请求,内容为”delete from t where id = 1; delete from t where id = 2; delete from t where id = 3; ….”

batchUpdate(10条记录)  =>  发送一次请求,内容为”update t set … where id = 1; update t set … where id = 2; update t set … where id = 3 …”

batchInsert(10条记录)  =>   发送一次请求,内容为”insert into t (…) values (…) , (…), (…)”

对delete和update,驱动所做的事就是把多条sql语句累积起来再一次性发出去;而对于insert,驱动则会把多条sql语句重写成一条风格很酷的sql语句,然后再发出去。 官方文档说,这种insert写法可以提高性能(”This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements”)

 

下载最新的JDBC的驱动程序。
MYSQL URL的配置参数如下:
jdbc:mysql://54.200.190.80:3306/ccb_ucstar?rewriteBatchedStatements=true

private void batchParseGroup(){
        Connection con= null;
        PreparedStatement ps =null;
        try {
            con= DbConnectionManager.getConnection();
            con.setAutoCommit(false);
            String sql = " insert into jivegroup(uri,groupname,pgroupid,description,creationdate,modificationdate,priority,selfpriority) values(?,?,?,?,?,?,?,?)";
            ps= con.prepareStatement(sql);
            for(int i=0;i<groupList.size();i++){
                Group group = groupList.get(i);
                ps.setString(1, group.getUri());
                ps.setString(2, group.getName());
                ps.setString(3, group.getPgroupId());
                ps.setString(4, group.getName());
                ps.setString(5, ""+System.currentTimeMillis());
                ps.setString(6, ""+System.currentTimeMillis());
                ps.setInt(7, group.getPriority());
                ps.setInt(8, group.getPriority());
                ps.addBatch();
                if(i%100==0){
                    ps.executeBatch();
                }
            }
            con.commit();
            ps.executeBatch();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DbConnectionManager.closeConnection(ps, con);
        }
    }

 

标签: 暂无
最后更新:2015年12月20日

tjccn_wx@hotmail.com

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

归档

  • 2026 年 10 月
  • 2024 年 2 月
  • 2023 年 7 月
  • 2023 年 1 月
  • 2022 年 10 月
  • 2022 年 9 月
  • 2022 年 6 月
  • 2022 年 4 月
  • 2022 年 3 月
  • 2022 年 2 月
  • 2021 年 11 月
  • 2021 年 10 月
  • 2021 年 9 月
  • 2021 年 1 月
  • 2020 年 12 月
  • 2020 年 11 月
  • 2020 年 10 月
  • 2020 年 9 月
  • 2020 年 7 月
  • 2020 年 6 月
  • 2020 年 5 月
  • 2020 年 4 月
  • 2020 年 3 月
  • 2020 年 2 月
  • 2019 年 10 月
  • 2019 年 8 月
  • 2019 年 7 月
  • 2019 年 4 月
  • 2019 年 3 月
  • 2018 年 12 月
  • 2018 年 11 月
  • 2018 年 10 月
  • 2018 年 6 月
  • 2018 年 5 月
  • 2018 年 4 月
  • 2018 年 3 月
  • 2018 年 2 月
  • 2018 年 1 月
  • 2017 年 12 月
  • 2017 年 11 月
  • 2017 年 10 月
  • 2017 年 9 月
  • 2017 年 8 月
  • 2017 年 7 月
  • 2017 年 6 月
  • 2017 年 5 月
  • 2017 年 4 月
  • 2017 年 3 月
  • 2017 年 2 月
  • 2017 年 1 月
  • 2016 年 12 月
  • 2016 年 11 月
  • 2016 年 10 月
  • 2016 年 9 月
  • 2016 年 8 月
  • 2016 年 7 月
  • 2016 年 6 月
  • 2016 年 5 月
  • 2016 年 4 月
  • 2016 年 3 月
  • 2015 年 12 月
  • 2015 年 8 月

分类

  • android
  • mysql
  • nethunter
  • proxmark
  • 未分类

COPYRIGHT © 2026 新之助meow. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang