简体   繁体   English

JDBC:是否最好使用executeBatch或executeUpdate来检查每个UPDATE语句是否成功?

[英]JDBC: Is it better to use executeBatch or executeUpdate to check if each UPDATE statement is successful?

I have a MySQL database where I need to do a 1k or so updates, and I am contemplating whether it would be more appropriate to use executeBatch or executeUpdate . 我有一个MySQL数据库,需要在其中进行1k左右的更新,并且我正在考虑使用executeBatch还是executeUpdate是否更合适。 The preparedstatement is to be built on an ArrayList of 1k or more ids (which are PKs of the table to be updated). preparestatement将基于1k或更多ID(它们是要更新的表的PK)的ArrayList构建。 For each update to the table I need to check if it was updated or not (it's possible that the id is not in the table). 对于表的每次更新,我都需要检查它是否已更新(ID可能不在表中)。 In the case that the id doesn't exist, I need to add that id to a separate ArrayList which will be used to do batch inserts. 如果id不存在,则需要将该ID添加到单独的ArrayList中,该ArrayList将用于批量插入。

Given the above, is it more appropriate to do: 鉴于上述情况,这样做是否更合适:

  1. Various separate executeUpdate() and then store the id if it is not updated, or 各种单独的executeUpdate() ,然后存储ID(如果未更新),或者

  2. Simply create a batch and use executeBatch() , which will return an array of either a 0 or 1 for each separate statement/id. 只需创建一个批处理并使用executeBatch() ,它将为每个单独的语句/ id返回一个0或1的数组。

In case two, the overhead would be an additional array to hold all the 0 or 1 return values. 在第二种情况下,开销将是一个额外的数组来保存所有0或1返回值。 In case one, the overhead would be due to executing each UPDATE separately. 在第一种情况下,开销将归因于分别执行每个UPDATE。

我会批处理,因为除非您以某种方式在同一设备上运行网络延迟,否则就需要考虑网络延迟。

Definitely executeBatch(), and make sure that you add "rewriteBatchedStatements=true" to your jdbc connection string. 绝对executeBatch(),并确保将“ rewriteBatchedStatements = true”添加到jdbc连接字符串中。

The increase in throughput is hard to exaggerate. 吞吐量的增加很难夸大。 Your 1K updates will likely take barely longer than a single update, assuming that you have proper indexes and a WHERE clause that makes use of them. 假设您具有适当的索引和使用它们的WHERE子句,则1K更新可能仅比单个更新花费更长的时间。

Without the extra setting on the connection string, the time to do the batch update is going to be about the same as to do each update individually. 如果在连接字符串上没有额外的设置,则进行批处理更新的时间将与单独进行每个更新的时间相同。

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

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