Optimizing JDBC Batch Processing with rewriteBatchedStatements=true
The JDBC parameter rewriteBatchedStatements=true
significantly enhances efficiency in batch data processing by consolidating multiple SQL statements into a single network transmission, thereby reducing network latency.
Enabling this parameter instructs the JDBC driver to bundle numerous queries into a single packet rather than sending each individually. Consider the following example, where individual rows are inserted into the 'jdbc' table using separate INSERT statements:
<code class="language-java">try (Connection con = ...; PreparedStatement ps = con.prepareStatement("INSERT INTO jdbc (`name`) VALUES (?)")) { for (int i = 1; i < 1000; i++) { ps.setString(1, "Line " + i + ": ..."); ps.addBatch(); } ps.executeBatch(); }</code>
With rewriteBatchedStatements=true
activated, the JDBC driver transmits a single packet containing a combined INSERT statement like this:
<code class="language-sql">INSERT INTO jdbc (`name`) VALUES ('Line 1: ...'), ('Line 2: ...'), ... ,('Line 999: ...')</code>
Crucially, the MySQL Connector/J driver (and other compliant drivers) respects the server's max_allowed_packet
setting. It checks this limit upon connection and dynamically adjusts the batch size to prevent exceeding the maximum packet size. If a single multi-row INSERT would be too large, the driver automatically fragments it into smaller, compliant packets.
By leveraging rewriteBatchedStatements=true
and configuring an appropriate max_allowed_packet
value, developers can substantially improve database interaction performance for batch operations, minimizing network overhead and maximizing throughput.
The above is the detailed content of How Does `rewriteBatchedStatements=true` Optimize JDBC Batch Processing?. For more information, please follow other related articles on the PHP Chinese website!