欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java 下执行mysql 批量插入的几种方法及用时

时间:2020-10-09 22:42:34|栏目:JAVA代码|点击:

方法1:

Java code

复制代码 代码如下:

conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS);
        pstmt = conn
                .prepareStatement("insert into loadtest (id, data) values (?, ?)");
        for (int i = 1; i <= COUNT; i++) {
            pstmt.clearParameters();
            pstmt.setInt(1, i);
            pstmt.setString(2, DATA);
            pstmt.execute();
        }

MyISAM:246.6秒、InnoDB:360.2秒

方法2: 使用事务,不自动commit

Java code

复制代码 代码如下:

conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS);
        conn.setAutoCommit(false);
        pstmt = conn
                .prepareStatement("insert into loadtest (id, data) values (?, ?)");
        for (int i = 1; i <= COUNT; i++) {
            pstmt.clearParameters();
            pstmt.setInt(1, i);
            pstmt.setString(2, DATA);
            pstmt.execute();
            if (i % COMMIT_SIZE == 0) {
                conn.commit();
            }
        }
        conn.commit();

InnoDB:31.5秒

方法3: executeBatch

Java code

复制代码 代码如下:

conn = DriverManager.getConnection(JDBC_URL
                + "?rewriteBatchedStatements=true", JDBC_USER, JDBC_PASS);
        conn.setAutoCommit(false);
        pstmt = conn
                .prepareStatement("insert into loadtest (id, data) values (?, ?)");
        for (int i = 1; i <= COUNT; i += BATCH_SIZE) {
            pstmt.clearBatch();
            for (int j = 0; j < BATCH_SIZE; j++) {
                pstmt.setInt(1, i + j);
                pstmt.setString(2, DATA);
                pstmt.addBatch();
            }
            pstmt.executeBatch();
            if ((i + BATCH_SIZE - 1) % COMMIT_SIZE == 0) {
                conn.commit();
            }
        }
        conn.commit();

InnoDB:5.2秒

上面的使用时必须
1)rewriteBatchedStatements=true
2)useServerPrepStmts=true

方法4:先LOAD再COMMIT

Java code

复制代码 代码如下:

conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASS);
        conn.setAutoCommit(false);
        pstmt = conn.prepareStatement("load data local infile '' "
                + "into table loadtest fields terminated by ','");
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= COUNT; i++) {
            sb.append(i + "," + DATA + "\n");
            if (i % COMMIT_SIZE == 0) {
                InputStream is = new ByteArrayInputStream(sb.toString()
                        .getBytes());
                ((com.mysql.jdbc.Statement) pstmt)
                        .setLocalInfileInputStream(is);
                pstmt.execute();
                conn.commit();
                sb.setLength(0);
            }
        }
        InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
        ((com.mysql.jdbc.Statement) pstmt).setLocalInfileInputStream(is);
        pstmt.execute();
        conn.commit();

上一篇:浅谈hibernate中对象的3种状态_瞬时态、持久态、脱管态

栏    目:JAVA代码

下一篇:java 判断一个数组中的数值是否连续相邻的方法

本文标题:java 下执行mysql 批量插入的几种方法及用时

本文地址:http://www.codeinn.net/misctech/9138.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有