【问题标题】:Difference between executeBatch() and executeLargeBatch() while using PreparedStatement使用 PreparedStatement 时 executeBatch() 和 executeLargeBatch() 之间的区别
【发布时间】:2015-05-13 01:27:47
【问题描述】:

我对何时使用executeBatch()executeLargeBatch() 感到困惑。 我浏览了 Java 文档,发现的唯一区别是:

当返回的行数可能超过Integer.MAX_VALUE时,应使用executeLargeBatch()

我将executeBatch() 替换为executeLargeBatch(),但没有发现任何性能改进。

处理Integer.MAX_VALUE 是唯一目的吗?一个比另一个有性能优势吗?

【问题讨论】:

  • 我发现这个问题很有用,因为很容易忽略问题中引用的文本以及返回类型的差异。
  • 是的,答案非常有用

标签: java sql batch-processing


【解决方案1】:

您引用的差异差异:executeBatch 返回 int[] 以返回每次更新的计数。 executeLargeBatch 返回 long[] 以返回每次更新的计数。因此,如果计数可能超过int 的容量,则文档说使用后者。例如,它们是executeUpdate(返回int)和executeLargeUpdate(返回long)的批处理等价物。

我不认为会有任何显着的性能差异;如果您可能更新超过 2,147,483,647 行,这只是不溢出 int 的问题。

【讨论】:

  • 我认为如果我们在可以使用executeBatch的地方使用executeLargeBatch,性能不会有任何提升
【解决方案2】:

executeBatch() 和 executeLargeUpdate() 有不同的用法。 executeBatch() 用于执行多条 SQL 语句,而 executeLargeUpdate() 适用于单个更新语句。

单一更新声明: int executeUpdate()

当您期望它更新比 int 可以处理的更多记录时,单个更新语句: long executeLargeUpdate()

多条 SQL 语句: int[] executeBatch()

当您期望一个或多个 SQL 更新比 int 可以处理的更多记录时,使用多个 SQL 语句: long[] 执行LargeUpdate()

largeUpdate 没有性能增强,只是更新的记录数,更新语句可能返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多