mysql execute 多个参数_MySql 的批量操作,要加rewriteBatchedStatements参数

今天在做某项目的POC测试,甲方提供了一个三十万记录的TXT数据文件,需要把该文件的记录插入到数据库中,由于项目部的同事在搭建测试环境中用的是Mysql数据库,在把数据导入到数据库中用的是JDBC的批处理。代码如下

1fa987a29c6482f53d401256f96355eb.png

ca75c07623e1b494fee67e8f316fc310.gifprivatevoidbatchParseGroup()9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png        Connection con=null;

d18c02628675d0a2c816449d98bda930.png        PreparedStatement ps=null;

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.giftry9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png            con=DbConnectionManager.getConnection();

d18c02628675d0a2c816449d98bda930.png            con.setAutoCommit(false);

d18c02628675d0a2c816449d98bda930.png            String sql="insert into jivegroup(uri,groupname,pgroupid,description,creationdate,modificationdate,priority,selfpriority) values(?,?,?,?,?,?,?,?)";

d18c02628675d0a2c816449d98bda930.png            ps=con.prepareStatement(sql);

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.giffor(inti=0;i{

d18c02628675d0a2c816449d98bda930.png                Group group=groupList.get(i);

d18c02628675d0a2c816449d98bda930.png                ps.setString(1, group.getUri());

d18c02628675d0a2c816449d98bda930.png                ps.setString(2, group.getName());

d18c02628675d0a2c816449d98bda930.png                ps.setString(3, group.getPgroupId());

d18c02628675d0a2c816449d98bda930.png                ps.setString(4, group.getName());

d18c02628675d0a2c816449d98bda930.png                ps.setString(5,""+System.currentTimeMillis());

d18c02628675d0a2c816449d98bda930.png                ps.setString(6,""+System.currentTimeMillis());

d18c02628675d0a2c816449d98bda930.png                ps.setInt(7, group.getPriority());

d18c02628675d0a2c816449d98bda930.png                ps.setInt(8, group.getPriority());

d18c02628675d0a2c816449d98bda930.png                ps.addBatch();

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gifif(i%100==0)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png                    ps.executeBatch();

ecedf933ec37d714bd4c2545da43add2.png                }ecedf933ec37d714bd4c2545da43add2.png            }d18c02628675d0a2c816449d98bda930.png            con.commit();

d18c02628675d0a2c816449d98bda930.png            ps.executeBatch();

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif        }catch(SQLException e)9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png            e.printStackTrace();

97e794c86028c5f5b5461ae5ef440a4c.png

3c6cafce68eb941a00f1998f1d3d3aa6.gif        }finally9b8a8a44dd1c74ae49c20a7cd451974e.png{

d18c02628675d0a2c816449d98bda930.png            DbConnectionManager.closeConnection(ps, con);

ecedf933ec37d714bd4c2545da43add2.png        }8f1ba5b45633e9678d1db480c16cae3f.png    }

在测试时,发现三十万的数据居然需要十分钟左右的时间。首先想到的就是Mysql的相关配置是不是有问题,反复修改了Mysql的相应配置参数,收效甚微。

在Mysql的官网上查到如下:

http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html

关于rewriteBatchedStatements参数,Mysql官方的说明:

Should the driver use multiqueries (irregardless of the setting of "allowMultiQueries") as well as rewriting of prepared statements for INSERT into multi-value inserts when executeBatch() is called? Notice that this has the potential for SQL injection if using plain java.sql.Statements and your code doesn't sanitize input correctly. Notice that for prepared statements, server-side prepared statements can not currently take advantage of this rewrite option, and that if you don't specify stream lengths when using PreparedStatement.set*Stream(), the driver won't be able to determine the optimum number of parameters per batch and you might receive an error from the driver that the resultant packet is too large. Statement.getGeneratedKeys() for these rewritten statements only works when the entire batch includes INSERT statements.

解决办法:

下载最新的JDBC的驱动程序。

MYSQL URL的配置参数如下:

jdbc:mysql://54.200.190.80:3306/ccb_ucstar?rewriteBatchedStatements=true

经过测试。三十多万的数据。70秒内搞定!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值