java JDBC 批量更新有何帮助?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14264953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:40:21  来源:igfitidea点击:

How is JDBC Batch Update helpful?

javajdbc

提问by cooper

Everybody says batch updates reduce the number of JDBC calls. Could someone explain what "JDBC call" means and how increased number of such calls are costlier compared to carrying entire load in one JDBC call.

每个人都说批量更新减少了 JDBC 调用的数量。有人能解释一下“JDBC 调用”是什么意思,以及与在一个 JDBC 调用中承载整个负载相比,增加的此类调用的成本如何更高。

回答by jonathan.cone

Colloquially, when developer's use the phrase JDBC callthey're talking about sending information over the wire to the database. The batch feature of the JDBC API allows you to submit multiple discrete operations in a single network call, as opposed to a call for each SQL statement. From the JDBC spec:

通俗地说,当开发人员使用短语JDBC 调用时,他们指的是通过线路将信息发送到数据库。JDBC API 的批处理特性允许您在单个网络调用中提交多个离散操作,而不是对每个 SQL 语句进行调用。从JDBC 规范

The batch update facility allows a Statement object to submit a set of heterogeneous SQL statements together as a single unit, or batch, to the underlying data source.

批处理更新工具允许 Statement 对象将一组异构 SQL 语句作为一个单元或批处理一起提交给底层数据源。

In the case of a PreparedStatement, the added benefit of only having to create a single statement exists. This allows you to execute the same query with multiple sets of bind parameters without adding the statement itself to the batch multiple times. The end result is less network traffic and its associated overhead.

在 PreparedStatement 的情况下,存在只需创建单个语句的额外好处。这允许您使用多组绑定参数执行相同的查询,而无需多次将语句本身添加到批处理中。最终结果是更少的网络流量及其相关的开销。

An example from the spec:

规范中的一个例子:

PreparedStatement stmt = con.prepareStatement(
"INSERT INTO employees VALUES (?, ?)");

stmt.setInt(1, 2000);
stmt.setString(2, "Kelly Kaufmann");
stmt.addBatch();

stmt.setInt(1, 3000);
stmt.setString(2, "Bill Barnes");
stmt.addBatch();

// submit the batch for execution
int[] updateCounts = stmt.executeBatch();

回答by knowbody

As described in IBM's documentation:

IBM 的文档中所述

The JDBC drivers that support JDBC 2.0 and above support batch updates. With batch updates, instead of updating rows of a DB2(R) table one at a time, you can direct JDBC to execute a group of updates at the same time. Statements that can be included in the same batch of updates are known as batchable statements.

If a statement has input parameters or host expressions, you can include that statement only in a batch that has other instances of the same statement. This type of batch is known as a homogeneous batch. If a statement has no input parameters, you can include that statement in a batch only if the other statements in the batch have no input parameters or host expressions. This type of batch is known as a heterogeneous batch. Two statements that can be included in the same batch are known as batch compatible.

支持 JDBC 2.0 及以上版本的 JDBC 驱动程序支持批量更新。通过批量更新,您可以指示 JDBC 同时执行一组更新,而不是一次更新一个 DB2(R) 表的行。可以包含在同一批更新中的语句称为可批处理语句。

如果语句具有输入参数或宿主表达式,则只能将该语句包含在具有同一语句的其他实例的批处理中。这种类型的批次称为均质批次。如果语句没有输入参数,则只有当批处理中的其他语句没有输入参数或宿主表达式时,您才能将该语句包含在批处理中。这种类型的批次称为异构批次。可以包含在同一批次中的两个语句称为批次兼容。

回答by Fahad

On calling JDBC update, the java program connects to the database server and executes the query. It means for each update, the java program contacts the database server and executes the query. Now, if we use JDBC batch-update, the java application needs to contact the database server only once and executes all the queries at the database server and returns to the java application.

在调用 JDBC 更新时,java 程序连接到数据库服务器并执行查询。这意味着对于每次更新,java 程序都会联系数据库服务器并执行查询。现在,如果我们使用 JDBC 批量更新,java 应用程序只需要联系数据库服务器一次,并在数据库服务器上执行所有查询并返回给 java 应用程序。

In brief, it will reduce the round trip between java application and database server. Normally both will be on a different server, so it will reduce a lot of network resources. Hence, it achieves better performance.

简而言之,它将减少java应用程序和数据库服务器之间的往返。通常两者都在不同的服务器上,因此会减少很多网络资源。因此,它实现了更好的性能。