java 下執行mysql 批量插入

來源:互聯網
上載者:User

1000次插入方法的比較。

方法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();

 這個最快4.6秒

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.