mysql批量提交效率优化

文章讨论了在Java中进行数据批量提交时,批处理的实际执行情况。通过Wireshark分析发现,即使使用PreparedStatement的executeBatch()方法,SQL语句在数据库端并未真正合并为单个请求。为实现真正的批处理,需要在建立连接时设置MySQLJDBC连接参数rewriteBatchedStatements为true。优化后,3000条数据的分发时间从4000毫秒降低到400毫秒,显著提升了性能。PostgreSQL也有类似参数reWriteBatchedInserts,需设为true以启用批量处理功能。
摘要由CSDN通过智能技术生成

 在java代码中,写数据的批量提交的时候都是如下代码。首先,获取连接conn, 再通过PreparedStatement 去执行批处理操作。

@Service
public class ServerOfMysql
        extends ServerAbs implements ServerInter{
    @Override
    public void connect(String classname, String drivername, String serverIP, int port, String dbname, String userName, String password, String unicode)
            throws Exception {
        String[] driveandurl = getUrl("Mysql", serverIP, port, dbname, userName, password, unicode);
        driveandurl[1] = driveandurl[1] + "&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true";
        connect(classname,driveandurl[0], driveandurl[1], userName, password);
    }
}
ps.executeBatch();

我们都以为该方法帮我们封装好,将批量执⾏的⼀组 sql Insert 语句,改写为一条 batched 语句 insert into table02 (col1,col2) values (value1,value2),(value1,value2),(value1,value2), 并通过一次请求发送给数据库服务器的。

那么事实又是如何呢?

通过wireshark的分析抓到的包可以发现,sql是分开的。每次都是独立的。截取出来看如下。

 这并不是我们需要的批处理。那么改如何优化呢?

就是再获取连接conn时设置rewriteBatchedStatements参数为true。

@Service
public class ServerOfMysql
        extends ServerAbs implements ServerInter{
    @Override
    public void connect(String classname, String drivername, String serverIP, int port, String dbname, String userName, String password, String unicode)
            throws Exception {
        String[] driveandurl = getUrl("Mysql", serverIP, port, dbname, userName, password, unicode);
        driveandurl[1] = driveandurl[1] + "&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true";
        connect(classname,driveandurl[0], driveandurl[1], userName, password);
    }
}

现在才是我们想要的批处理sql。 

优化前,3000条的数据分发耗时4000毫秒左右 。

 优化后,3000条的数据分发耗时400毫秒左右 。

 核心:

Mysql 提供了其特有的 JDBC 连接参数 rewriteBatchedStatements,当把该参数置为 true 时, mysql jdbc 驱动会在客户端重写用户提交的原始 SQL,并将重写后的 SQL “send the batched statements in a single request”。

postgreSql 与oracle是默认批量的。但是pg 在9.4.1208 版本后,又提供了参数 reWriteBatchedInserts,该参数默认值为 FALSE;要想支持批量,要改成true。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值