鍍金池/ 問答/Java  數(shù)據(jù)庫  網(wǎng)絡(luò)安全/ 測試JDBC批處理效率沒變化

測試JDBC批處理效率沒變化

我在學(xué)習(xí) JDBC 批處理時(shí), 為比較 Statement, PreparedStatement 和 JDBC Batch 操作之間的效率,寫一個(gè)例子:向數(shù)據(jù)庫中插入 100,000 條記錄,并記錄消耗的時(shí)間。然后分別用這三種方式實(shí)現(xiàn),可每次測試的結(jié)果卻都是幾乎相同沒變化,請教大神這是為什么?

import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class BatchTest {

    /**
     * 使用 Statement 進(jìn)行批處理操作
     */
    @Test
    public void testBatchWithStatement(){
        Connection connection = null;
        Statement statement;
        String sql;

        try {
            connection = JDBCUtils.getConnection();
            JDBCUtils.beginTx(connection);

            statement = connection.createStatement();
            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                sql = "INSERT INTO batch VALUES(" + i +")";
                statement.executeUpdate(sql);
            }
            long end = System.currentTimeMillis();

            System.out.println(end - start);

            JDBCUtils.commit(connection);
        } catch (Exception e) {
            e.printStackTrace();
            JDBCUtils.rollback(connection);
        }
    }

    /**
     * 使用 PreparedStatement 進(jìn)行批處理操作
     */
    @Test
    public void testBatchWithPreparedStatement(){
        Connection connection = null;
        PreparedStatement statement;
        String sql;

        try {
            connection = JDBCUtils.getConnection();
            JDBCUtils.beginTx(connection);

            sql = "INSERT INTO batch VALUES(?)";
            statement = connection.prepareStatement(sql);

            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                statement.setInt(1, i + 1);
                statement.executeUpdate();
            }
            long end = System.currentTimeMillis();

            System.out.println(end - start);

            JDBCUtils.commit(connection);
        } catch (Exception e) {
            e.printStackTrace();
            JDBCUtils.rollback(connection);
        }
    }

    /**
     * 使用 Batch 進(jìn)行批處理操作
     */
    @Test
    public void testBatch(){
        Connection connection = null;
        PreparedStatement statement;
        String sql;
        try {
            connection = JDBCUtils.getConnection();
            JDBCUtils.beginTx(connection);

            sql = "INSERT INTO batch VALUES(?)";
            statement = connection.prepareStatement(sql);

            long start = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
                statement.setInt(1, i + 1);
                statement.addBatch();
                if ((i + 1) % 500 == 0){
                    statement.executeBatch();
                    statement.clearBatch();
                }
            }
            if (100000 % 300 != 0){
                statement.executeBatch();
                statement.clearBatch();
            }
            long end = System.currentTimeMillis();

            System.out.println(end - start);
            JDBCUtils.commit(connection);
        } catch (Exception e) {
            e.printStackTrace();
            JDBCUtils.rollback(connection);
        }
    }

}
回答
編輯回答
貓小柒

jdbc URL里加上rewriteBatchedStatements=true這個(gè)參數(shù)就可以了。

或者,在程序?qū)懗梢粭lINSERT插入多行數(shù)據(jù)的形式,不用依賴jdbc的batch功能,這樣是最靠譜的,也適合移植到MySQL以外的數(shù)據(jù)庫。

2017年3月25日 18:01