简体   繁体   English

JDBC Batch执行极其缓慢

[英]JDBC Batch executing extremely slow

Can anyone tell me what I'm doing wrong I'm executing 350 inserts in a mysql and it's taking like 40 secs. 谁能告诉我我在做什么错,我正在mysql中执行350次插入操作,大约需要40秒钟。

Here is the code 这是代码

long t0 = System.currentTimeMillis();
        Connection con = connectionProvider.getConnection();
        PreparedStatement s = con.prepareStatement("insert into domkee.friends(idFriends,friend1Id,friend2Id,friend2Name) values(?,?,?,?)");
        con.setAutoCommit(false);
        for (Friend f : friends) {
            s.setLong(1, 0);
            s.setLong(2, f.getFriend1Id());
            s.setLong(3, f.getFriend2Id());
            s.setString(4, f.getFriend2Name());
            s.addBatch();

        }
        long t1 = System.currentTimeMillis() - t0;
        s.executeBatch();
        long t2 = System.currentTimeMillis()-t0;
        con.commit();
        long t3 = System.currentTimeMillis()-t0;
        s.close();
        con.close();
        long t4 = System.currentTimeMillis()-t0;
        System.out.println(((double)t1/1000) + ";" + ((double)t2/1000) + ";" + ((double)t3/1000) + ";" + ((double)t4/1000));

and here is the console: 这是控制台:

0.156;39.251;39.376;39.486

So the .executeBatch() is taking like 40 secs, what could be the problem? 因此.executeBatch()需要40秒,这可能是什么问题?

Add ?rewriteBatchedStatements=true to the end of your JDBC url. ?rewriteBatchedStatements=true添加到JDBC URL的末尾。 It'll give you a serious performance improvement. 它会给您带来严重的性能提升。 Note that this is specific to MySql, won't have any effect with any other JDBC drivers. 请注意,这特定于MySql,对其他任何JDBC驱动程序均无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM