Oracle ojdbc executeBatch太慢了

时间:2017-02-14 03:48:26

标签: oracle jdbc

我想批量插入20000条记录,代码是自爆的:

            while ((line = reader.readLine()) != null) {
                line = line.substring(0, line.lastIndexOf(";"));
                System.out.println(line);
                st.addBatch(line);

                i += 1;
                if (i % 1000 == 0) {
                    System.out.println("execute:" + i);
                    Date beginBatch = new Date();
                    st.executeBatch();
                    Date endBatch = new Date();
                    System.out.println("Start at: " + beginBatch);
                    System.out.println("End at: " + endBatch);
                }
            }

我花了一个小时才完成,但是当我在Sql Developer控制台中运行这个sql文件时,它只花了我3分钟。有什么不对的吗。有人可以帮我找出根本原因。

2 个答案:

答案 0 :(得分:0)

Oracle数据库JDBC使用数组参数来实现JDBC批处理执行。没有参数,所以没有批处理。您的批处理执行只是对语句进行排队并一次执行一个语句,而不是批处理。

即使数据库必须对每个SQL进行硬解析,执行20,000个SQL语句仍需要一个小时听起来有点慢。由于SQLDev可以在3分钟内完成相同的工作,因此JDBC也应该这样做。最大的不同是批处理。驱动程序批处理代码不是免费的。它带来了巨大的开销。我建议你用执行调用替换addBatch调用。那可能会更快。

答案 1 :(得分:-1)

您永远不会清除您的批次。所以可能在第一次执行时你执行了1000个语句,在2000年第二次等等......

您应该添加类似

的内容
                Date beginBatch = new Date();
                st.executeBatch();
                st.clearBatch();
相关问题