虽然这个问题要求使用JDBC有效地插入Oracle,但我目前正在玩DB2(在IBM大型机上),概念上插入将是相似的,所以认为看到我的指标可能会有所帮助
这里是指标
1) 一次插入一条记录
public void writeWithCompileQuery(int records) {
    PreparedStatement statement;
    try {
        Connection connection = getDatabaseConnection();
        connection.setAutoCommit(true);
        String compiledQuery = "INSERT INTO TESTDB.EMPLOYEE(EMPNO, EMPNM, DEPT, RANK, USERNAME)" +
                " VALUES" + "(?, ?, ?, ?, ?)";
        statement = connection.prepareStatement(compiledQuery);
        long start = System.currentTimeMillis();
        for(int index = 1; index < records; index++) {
            statement.setInt(1, index);
            statement.setString(2, "emp number-"+index);
            statement.setInt(3, index);
            statement.setInt(4, index);
            statement.setString(5, "username");
            long startInternal = System.currentTimeMillis();
            statement.executeUpdate();
            System.out.println("each transaction time taken = " + (System.currentTimeMillis() - startInternal) + " ms");
        }
        long end = System.currentTimeMillis();
        System.out.println("total time taken = " + (end - start) + " ms");
        System.out.println("avg total time taken = " + (end - start)/ records + " ms");
        statement.close();
        connection.close();
    } catch (SQLException ex) {
        System.err.println("SQLException information");
        while (ex != null) {
            System.err.println("Error msg: " + ex.getMessage());
            ex = ex.getNextException();
        }
    }
}
100 个事务的指标 :
each transaction time taken = 123 ms
each transaction time taken = 53 ms
each transaction time taken = 48 ms
each transaction time taken = 48 ms
each transaction time taken = 49 ms
each transaction time taken = 49 ms
...
..
.
each transaction time taken = 49 ms
each transaction time taken = 49 ms
total time taken = 4935 ms
avg total time taken = 49 ms
第一个事务是绕着哪个是查询解析然后执行,后续的事务只带。(它仍然很高,但我的数据库在另一台服务器上(我需要对网络进行故障排除))120-150ms50ms
2)批量插入(高效) - 通过以下方式实现preparedStatement.executeBatch()
public int[] writeInABatchWithCompiledQuery(int records) {
    PreparedStatement preparedStatement;
    try {
        Connection connection = getDatabaseConnection();
        connection.setAutoCommit(true);
        String compiledQuery = "INSERT INTO TESTDB.EMPLOYEE(EMPNO, EMPNM, DEPT, RANK, USERNAME)" +
                " VALUES" + "(?, ?, ?, ?, ?)";
        preparedStatement = connection.prepareStatement(compiledQuery);
        for(int index = 1; index <= records; index++) {
            preparedStatement.setInt(1, index);
            preparedStatement.setString(2, "empo number-"+index);
            preparedStatement.setInt(3, index+100);
            preparedStatement.setInt(4, index+200);
            preparedStatement.setString(5, "usernames");
            preparedStatement.addBatch();
        }
        long start = System.currentTimeMillis();
        int[] inserted = preparedStatement.executeBatch();
        long end = System.currentTimeMillis();
        System.out.println("total time taken to insert the batch = " + (end - start) + " ms");
        System.out.println("total time taken = " + (end - start)/records + " s");
        preparedStatement.close();
        connection.close();
        return inserted;
    } catch (SQLException ex) {
        System.err.println("SQLException information");
        while (ex != null) {
            System.err.println("Error msg: " + ex.getMessage());
            ex = ex.getNextException();
        }
        throw new RuntimeException("Error");
    }
}
一批 100 个事务的指标为
total time taken to insert the batch = 127 ms
和 1000 个事务
total time taken to insert the batch = 341 ms
因此,在 中进行 100 个事务(一次使用一个 trxn)将减少到(一批 100 条记录)。~5000ms~150ms
注意 - 忽略我的网络,这是超级慢的,但指标值将是相对的。