DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch

Trending

  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • Alternative Structured Concurrency
  • Kafka and Spark Structured Streaming in Enterprise: The Patterns That Hold Up Under Pressure
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  1. DZone
  2. Data Engineering
  3. Databases
  4. Performant Batch Inserts Using JDBC

Performant Batch Inserts Using JDBC

This simple change may significantly improve the performance of your bulk inserts into a database via JDBC.

By 
Duncan Brown user avatar
Duncan Brown
·
Feb. 25, 19 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
34.7K Views

Join the DZone community and get the full member experience.

Join For Free

The Problem

If you've ever tried to insert data via a JDBC connection — say to a MySQL database — in bulk, then you may have had to sit through several minutes or even hours for hundreds of thousands — or even millions —of records to finish being added to the database. Even without the overhead of transactions, this wait time is comparable.

So what can we do to improve the performance of these bulk inserts?  Interestingly, there is something small that can be adjusted that can have a very large impact.

Let's consider the following example:

  Connection connection = null;

  // connect to the database here and set the "connection" variable

  try {
    String query = "INSERT INTO table1 (field1, field2) VALUES (?, ?)";

    PreparedStatement preparedStatement = connection.prepareStatement(query);

    for (int i = 0; i < 1000000) {
      preparedStatement.setString(1, "value" + i);
      preparedStatement.setInt(2, i);

      preparedStatement.addBatch();
    }

    preparedStatement.executeBatch();
  } catch (SQLException e1) {
    // handle the exception
  }


The above code simply creates a batch insert using a PreparedStatement from the java.sql library.

Nothing surprising there.

However, let's take a look at how the connection to the database is established:

try {
  String connectionString = "jdbc:mysql://localhost:3306/my_db"; // be sure to add any other parameters needed to ensure an authorized connection

  Class.forName("com.mysql.cj.jdbc.Driver"); // ...or your favorite JDBC driver

  this.connection = DriverManager.getConnection(connectionString);
} catch (ClassNotFoundException | SQLException e) {
  // deal with the exceptioun
}


With the above code segments, we have a fairly typical interaction with a MySQL database via a JDBC driver.

However, most who run this code will notice how painfully slow the overall process is.

A Possible Solution

So what can be done?

One simple setting in the connection string may make a world of difference.

If we add the following to our connection string rewriteBatchedStatements=true, like the following:

  String connectionString = "jdbc:mysql://localhost:3306/my_db?rewriteBatchedStatements=true"


(...of course, being sure to augment the connection string with any other parameters, such as those to ensure an authenticated connection...)

And then, re-run the above code segments; you should notice a substantial increase in performance!

How Does It Work?

Quite simply, the connection string setting we added tells the database to take any series of individual INSERT statements and convert them to a single multi-value INSERT statement, e.g.:

INSERT INTO table1 (field1, field2) VALUES ("string1", 1);
INSERT INTO table1 (field1, field2) VALUES ("string2", 2);
INSERT INTO table1 (field1, field2) VALUES ("string3", 3);
INSERT INTO table1 (field1, field2) VALUES ("string4", 4);


becomes...

INSERT INTO table1 (field1, field2) VALUES  ("string1", 1), 
("string2", 2), 
                                            ("string3", 3), 
                                            ("string4", 4);


Give it a shot the next time you run into this situation! Let me know how it goes in the comments below.

Database

Opinions expressed by DZone contributors are their own.

Related

  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines
  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • Liquibase: Database Change Management and Automated Deployments
  • AWS Managed Database Observability: Monitoring DynamoDB, ElastiCache, and Redshift Beyond CloudWatch

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook