JDBC批处理更新如何有用? [英] How is JDBC Batch Update helpful?

查看:94
本文介绍了JDBC批处理更新如何有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都说批量更新会减少JDBC调用的次数。有人可以解释JDBC调用的含义以及与在一次JDBC调用中携带整个加载相比,此类调用的数量增加的成本是多少。

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 call 时,他们谈论的是通过网络将信息发送到数据库。 JDBC API的批处理功能允许您在单个网络调用中提交多个离散操作,而不是调用每个SQL语句。来自 JDBC规范

Colloquially, when developer's use the phrase JDBC call they'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:


批量更新工具允许Statement对象将一组
异构SQL语句作为一个单元一起提交,或者批量,
基础数据源。

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.

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

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 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();

这篇关于JDBC批处理更新如何有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆