Difference between executeBatch() and executeLargeBatch() while using PreparedStatement
Asked Answered
M

2

14

I am confused about when to use executeBatch() and executeLargeBatch(). I went through the Java documents and the only difference I found is:

executeLargeBatch() should be used when the returned row count may exceed Integer.MAX_VALUE.

I replaced executeBatch() with executeLargeBatch() but haven't found any performance improvements.

Is handling Integer.MAX_VALUE the only purpose? Is there any performance advantage in one over the other?

Marlow answered 11/3, 2015 at 10:3 Comment(2)
I found this question useful because it's easy to overlook the text quoted in the question and the difference in return types.Gloat
Yes the answer was extremely usefulGilges
M
22

Your quoted difference is the difference: executeBatch returns int[] to return the counts of each update. executeLargeBatch returns long[] to return the counts of each update. Hence the documentation saying to use the latter if the count may exceed the capacity of int. E.g., they're the batch equivalents of executeUpdate (returns int) and executeLargeUpdate (returns long).

I wouldn't expect a performance difference of any significance; it's just a matter of not overflowing the int if you're potentially updating more than 2,147,483,647 rows.

Medeiros answered 11/3, 2015 at 10:7 Comment(1)
and I think there will be no performance enhancement if we use executeLargeBatch at the places where we can use executeBatchTeleplay
O
0

From the source code, executeBatch() actually called executeLargeBatch internally, so performance-wise, they should be the same

public int[] executeBatch() throws SQLException {
  Monitor.CloseableLock lock = this.connection.acquireCloseableLock();
  Throwable var2 = null;

  int[] var3;
  try {
     var3 = this.toIntArray(this.executeLargeBatch());
  } catch (Throwable var12) {
     var2 = var12;
     throw var12;
  } finally {
     if (lock != null) {
        if (var2 != null) {
           try {
              lock.close();
           } catch (Throwable var11) {
              var2.addSuppressed(var11);
           }
        } else {
           lock.close();
        }
     }
  }
  return var3;
}
Osmium answered 17/4 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.