JDBC操作MySQL(2)—批量操作

在数据迁移、批量等业务场景中,经常会需要批量insert、update与delete,如果逐条操作,在数据量较大时性能往往不能满足要求,熟悉JDBC工程师首先会想到batch接口,使用方法如下:

    public static  void batchInsert() throws SQLException{
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        connection.setAutoCommit(false);
        PreparedStatement statement = connection.prepareStatement("insert into test(id,name) values(?,?)");

        long begin = System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            statement.setInt(1, i);
            statement.setString(2, "hello");
            statement.addBatch();
        }
        statement.executeBatch();
        connection.commit();
        long end = System.currentTimeMillis();
        System.out.println("batchInsert span time="+(end-begin)+ "ms");
    

不过这段代码在操作MySQL时其实并没有真正生效。查看MySQL服务器通用日志:

 1 Connect  root@localhost on test
            1 Query /* mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e ) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
            1 Query SET NAMES latin1
            1 Query SET character_set_results = NULL
            1 Query SET autocommit=1
            1 Query SET autocommit=0
            1 Query select @@session.tx_read_only
            1 Query insert into test(id,name) values(0,'hello')
            1 Query insert into test(id,name) values(1,'hello')
            1 Query insert into test(id,name) values(2,'hello')
            1 Query insert into test(id,name) values(3,'hello')
            1 Query insert into test(id,name) values(4,'hello')
            1 Query commit

可以看到MySQL其实仍然还是一条条顺序执行的insert,与非batch方式是一样的。那么具体问题出在哪儿呢?老办法,直接翻MySQL驱动中com.mysql.jdbc.PreparedStatment的源码。

public int[] executeBatch() throws SQLException {
        synchronized (checkClosed().getConnectionMutex()) {
        
            if (this.connection.isReadOnly()) {
                throw new SQLException(Messages.getString("PreparedStatement.25") //$NON-NLS-1$
                        + Messages.getString("PreparedStatement.26"), //$NON-NLS-1$
                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
            }

            if (this.batchedArgs == null || this.batchedArgs.size() == 0) {
                return new int[0];
            }

            // we timeout the entire batch, not individual statements
            int batchTimeout = this.timeoutInMillis;
            this.timeoutInMillis = 0;
        
            resetCancelledState();
            
            try {
                statementBegins();
                
                clearWarnings();

                if (!this.batchHasPlainStatements
                        && this.connection.getRewriteBatchedStatements()) {
                    
                    
                    if (canRewriteAsMultiValueInsertAtSqlLevel()) {
                        return executeBatchedInserts(batchTimeout);
                    }
                    
                    if (this.connection.versionMeetsMinimum(4, 1, 0) 
                            && !this.batchHasPlainStatements
                            && this.batchedArgs != null 
                            && this.batchedArgs.size() > 3 /* cost of option setting rt-wise */) {
                        return executePreparedBatchAsMultiStatement(batchTimeout);
                    }
                }

                return executeBatchSerially(batchTimeout);
            } finally {
                this.statementExecuting.set(false);
                
                clearBatch();
            }
        }
    }
Insert操作

由上代码可以看出,MySQL驱动默认是不支持批量操作,但如果this.connection.getRewriteBatchedStatements()为true时(通过设置参数rewriteBatchedStatements=true),则会进入批量提交逻辑分支。
我们增加该参数,然后再执行,发现执行速度大大提高

public static  void batchInsertWithRewriteBatchedStatements() throws SQLException{
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true", "root", "123456");
        connection.setAutoCommit(false);
        PreparedStatement statement = connection.prepareStatement("insert into test(id,name) values(?,?)");

        long begin = System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            statement.setInt(1, i);
            statement.setString(2, "hello");
            statement.addBatch();
        }
        statement.executeBatch();
        connection.commit();
        long end = System.currentTimeMillis();
        System.out.println("batchInsertWithRewriteBatchedStatements span time="+(end-begin)+ "ms");
    }

通过我们看MySQL服务器端日志:

            2 Connect   root@localhost on test
            2 Query /* mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e ) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
            2 Query SET NAMES latin1
            2 Query SET character_set_results = NULL
            2 Query SET autocommit=1
            2 Query SET autocommit=0
            2 Query select @@session.tx_read_only
            2 Query insert into test(id,name) values(0,'hello'),(1,'hello'),(2,'hello'),(3,'hello'),(4,'hello')
            2 Query commit

可见MySQL驱动其实只把insert转变成了insert values(),(),()形式,然后一次性发送到MySQL服务器执行。

我们继续看canRewriteAsMultiValueInsertAtSqlLevel()方法,该方法真正的实现在canRewrite方法中,其代码如下:

protected static boolean canRewrite(String sql, boolean isOnDuplicateKeyUpdate, int locationOfOnDuplicateKeyUpdate, int statementStartPos) {
       // Needs to be INSERT, can't have INSERT ... SELECT or
       // INSERT ... ON DUPLICATE KEY UPDATE with an id=LAST_INSERT_ID(...)

       boolean rewritableOdku = true;

       if (isOnDuplicateKeyUpdate) {
           int updateClausePos = StringUtils.indexOfIgnoreCase(
                   locationOfOnDuplicateKeyUpdate, sql, " UPDATE ");

           if (updateClausePos != -1) {
               rewritableOdku = StringUtils
                       .indexOfIgnoreCaseRespectMarker(updateClausePos,
                               sql, "LAST_INSERT_ID", "\"'`", "\"'`",
                               false) == -1;
           }
       }

       return StringUtils
               .startsWithIgnoreCaseAndWs(sql, "INSERT",
                       statementStartPos)
               && StringUtils.indexOfIgnoreCaseRespectMarker(
                       statementStartPos, sql, "SELECT", "\"'`",
                       "\"'`", false) == -1 && rewritableOdku;
   }

由此方法可以看出,并不是所有insert都可以进行多值插入批量优化的,INSERT ... SELECT 以及INSERT ... ON DUPLICATE就不支持。

执行效率对比

插入1w条数据,顺序插入与批量插入时间对比

batchInsert span time=1879ms
batchInsertWithRewriteBatchedStatements span time=199ms

结论:

可见使用批量方式insert,可以大大提高执行效率,MySQL对此做了真正的优化,本地测试可以提高10倍左右。
对于批量插入,网上很多推荐直接使用insert into table values(),()...,尤其是使用MyBatis时,建议使用foreach进行SQL拼接,例如:

    <insert id="insertBatch" parameterType="java.util.List">
        insert into test(id, name) values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id},
             #{item.name})
        </foreach>
    </insert>

但这种方式有个问题,如果插入的条数较多,可能会导致报文超过MySQL最大允许报文长度时,MySQL驱动会直接抛出异常

    Exception in thread "main" com.mysql.jdbc.PacketTooBigException: Packet for query is too large (16888926 > 4194304). You can change this value on the server by setting the max_allowed_packet' variable.
    at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:3681)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2512)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1197)
    at com.cmbc.dap.dao.test.MysqlBatchTest.batchInsertWithMultiValues(MysqlBatchTest.java:125)
    at com.cmbc.dap.dao.test.MysqlBatchTest.main(MysqlBatchTest.java:18)

MySQL报文允许最大长度可以通过以下命令查看
mysql> show VARIABLES like '%max_allowed_packet%';
+--------------------+-----------+
| Variable_name | Value |
+--------------------+-----------+
| max_allowed_packet | 167772160 |
+--------------------+-----------+
1 row in set (0.00 sec)

因此不建议开发时通过这种方式进行批量操作,正确安全的方式还是通过JDBC batch接口进行操作,MySQL驱动会根据报文最大长度自动拆分包。

如果使用Mybatis,可指定ExcutorType为Batch模式来完成批量操作。

Update、Delete操作

对于update、delete类型SQL,当SQL数目大于3时,通过多条语句用分号;相隔,然后一起发送MySQL服务器的方式实现批量执行。具体代码可见generateMultiStatementForBatch方法:

    private String generateMultiStatementForBatch(int numBatches) throws SQLException {
        synchronized (checkClosed().getConnectionMutex()) {
            StringBuffer newStatementSql = new StringBuffer((this.originalSql
                    .length() + 1) * numBatches);
                    
            newStatementSql.append(this.originalSql);
    
            for (int i = 0; i < numBatches - 1; i++) {
                newStatementSql.append(';');
                newStatementSql.append(this.originalSql);
            }
    
            return newStatementSql.toString();
        }
    }

写代码亲测:

public static  void batchUpdate() throws SQLException{
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
        connection.setAutoCommit(false);
        
        PreparedStatement statement = connection.prepareStatement("update test set name= ? where id =?");
        long begin = System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            statement.setString(1, "hello");
            statement.setInt(2, i);
            statement.execute();
        }

        connection.commit();
        long end = System.currentTimeMillis();
        System.out.println("batchUpdate span time="+(end-begin)+ "ms");
    }   
    public static  void batchUpdateWithRewriteBatchedStatements() throws SQLException{
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true", "root", "123456");
        connection.setAutoCommit(false);
        
        PreparedStatement statement = connection.prepareStatement("update test set name= ? where id =?");
        long begin = System.currentTimeMillis();
        for(int i=0;i<COUNT;i++){
            statement.setString(1, "hello");
            statement.setInt(2, i);
            statement.addBatch();
        }
        statement.executeBatch();

        connection.commit();
        long end = System.currentTimeMillis();
        System.out.println("batchUpdateWithRewriteBatchedStatements span time="+(end-begin)+ "ms");
    }

对应的后台MySQL服务器端通用日志:

            190104 13:12:24     1 Connect   root@localhost on test
            1 Query /* mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e ) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
            1 Query SET NAMES latin1
            1 Query SET character_set_results = NULL
            1 Query SET autocommit=1
            1 Query SET autocommit=0
190104 13:12:25     1 Query select @@session.tx_read_only
            1 Query update test set name= 'hello' where id =0
190104 13:12:26     1 Query select @@session.tx_read_only
            1 Query update test set name= 'hello' where id =1
            1 Query select @@session.tx_read_only
            1 Query update test set name= 'hello' where id =2
190104 13:12:27     1 Query select @@session.tx_read_only
            1 Query update test set name= 'hello' where id =3
190104 13:12:28     1 Query select @@session.tx_read_only
            1 Query update test set name= 'hello' where id =4
            1 Query commit
            2 Connect   root@localhost on test
            2 Query /* mysql-connector-java-5.1.46 ( Revision: 9cc87a48e75c2d2e87c1a293b2862ce651cb256e ) */SELECT  @@session.auto_increment_increment AS auto_increment_increment, @@character_set_client AS character_set_client, @@character_set_connection AS character_set_connection, @@character_set_results AS character_set_results, @@character_set_server AS character_set_server, @@collation_server AS collation_server, @@init_connect AS init_connect, @@interactive_timeout AS interactive_timeout, @@license AS license, @@lower_case_table_names AS lower_case_table_names, @@max_allowed_packet AS max_allowed_packet, @@net_buffer_length AS net_buffer_length, @@net_write_timeout AS net_write_timeout, @@query_cache_size AS query_cache_size, @@query_cache_type AS query_cache_type, @@sql_mode AS sql_mode, @@system_time_zone AS system_time_zone, @@time_zone AS time_zone, @@tx_isolation AS transaction_isolation, @@wait_timeout AS wait_timeout
            2 Query SET NAMES latin1
            2 Query SET character_set_results = NULL
            2 Query SET autocommit=1
            2 Query SET autocommit=0
            2 Query select @@session.tx_read_only
            2 Query select @@session.tx_read_only
            2 Query update test set name= 'hello' where id =0;
190104 13:12:29     2 Query update test set name= 'hello' where id =1;
190104 13:12:30     2 Query update test set name= 'hello' where id =2;
            2 Query update test set name= 'hello' where id =3;
190104 13:12:31     2 Query update test set name= 'hello' where id =4
190104 13:12:32     2 Query commit

由上可见,这种多条SQL一起发送到MySQL服务器后,依然是一条条执行,相比客户端顺序执行,仅仅是节省了多次的网络开销。

执行效率对比

插入1w条数据,顺序插入与批量插入时间对比

batchUpdate span time=63192ms
batchUpdateWithRewriteBatchedStatements span time=60946ms

结论:

可见,对于update、delete,MySQL的批量操作其实是一种"伪"批量,性能有提升,但非常有限。

综上,可看出MySQL对批量insert做了很多优化,所以对于批量插入,强烈建议通过JDBC Batch进行操作,但update与delete批量操作则无太大提升。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,186评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,858评论 3 387
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,620评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,888评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,009评论 6 385
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,149评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,204评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,956评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,385评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,698评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,863评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,544评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,185评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,899评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,141评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,684评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,750评论 2 351

推荐阅读更多精彩内容

  • MySQL技术内幕:InnoDB存储引擎(第2版) 姜承尧 第1章 MySQL体系结构和存储引擎 >> 在上述例子...
    沉默剑士阅读 7,404评论 0 16
  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,470评论 0 4
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 31,915评论 2 89
  • 乌镇时光 一、雨巷 说不清这样的石板路 是宽,还是窄 容得下千年时光穿行而过 却始终拧不过一个细细的腰身 那女子 ...
    诗人静娴阅读 430评论 5 8
  • 如果你想把我忘记 请把我埋在你影子够得着的地方 好让我感觉到你的呼吸 放开你的脚步 去天涯海角吧 当你用尽脚步的疲...
    舒严阅读 131评论 0 0