Wednesday, January 2, 2013

JDBC Batch INSERT and UPDATE example in Java with PreparedStatement

JDBC API in Java allows program to batch insert and update data into database, which tends to provide better performance by simple virtue of fact that it reduce lot of database round-trip which eventually improves overall performance. In fact it’s one of JDBC best practices to insert and update data in batches. For those who doesn’t know what is batch insert and update, Java provides several ways to execute SQL queries, one of them is JDBC batch insert and update, on which instead of executing sql query one by one using either Statement or PreparedSatement, you execute query in batch and send a batch of query to database for execution instead of single query. Since multiple queries are combined into batch and one batch is sent to database instead of individual queries, it reduce database round trip by factor of batch size. Batch size can be anything but needs to be decided carefully. JDBC specification supports upto 100 but individual database e.g. Oracle, MySQL, Sybase or SQL Server has there own limit on maximum batch size, , normal jdbc batch size ranges from 50 to 100. JDBC API provides addBatch() method to add queries into batch and than later execute them using executeBatch() method. Both Statement and PreparedStatement can be used to execute batch queries in Java. By the way batch insert and update also provide performance boost to Data access Object or DAO layer,  as discussed in our last post 4 ways to improve Performance of JDBC applications.


How to run batch insert and update in JDBC

JDBC batch insert and update example in Java with preparedStatementThere are multiple ways you can run batch queries in Java application, You have choice of using plain old JDBC or you can leverage Spring's JdbcTemplate Utility class. Though both Statement and PreparedStatment can execute batch queries, It’s better to use PreparedStatement because of several benefits it provides including improved performance and prevention from SQL injection as suggested on Why you should use PreparedStatement in Java. In next section, we will compare performance of same INSERT SQL query when running as without batch and running as batch insert query. In both cases we will use PreparedStatement to make testing similar.

SQL query without JDBC batch update using PreparedStatement
Here is an example of running SQL query without using JDBC batch update. Performance of this example can be used to compare how JDBC Batch update perform.

//query for inserting batch data
        String query = "insert into employee values (?,?,NULL)";
        PreparedStatement pStatement = conn.prepareStatement(query);
     
        long startTime = System.currentTimeMillis();
        for(int count = 0; count < 1000; count++ ){
            pStatement.setString(1, Integer.toString(count));
            pStatement.setString(2, "Employee"+count);
            pStatement.executeUpdate();
        }
        long endTime = System.currentTimeMillis();
        long elapsedTime = (endTime - startTime)/1000; //in seconds
        System.out.println("Total time required to execute 1000 SQL INSERT queries using PreparedStatement without JDBC batch update is :" + elapsedTime);

Output:
Total time required to execute 1000 queries using Statement without JDBC batch update is :38

So it took 38 seconds to insert 1000 records on employee table on MySQL database running on localhost. Yes, indeed its quite high but don't bother about absolute number yet, what is important here is to find out whether JDBC batch insert or update gives better performance or not. By the way above example uses PreparedStatement and bind variables to ensure standard JDBC practices are followed.

JDBC Batch INSERT example using PreparedStatement
Now, let’s run same set of SQL query as JDBC batch INSERT. In this example, instead of running every SQL INSERT query as executeUpdate() , we are adding them in a batch using addBatch() method and once we reaches batch size, which is 100 here, we send them to database using executeBatch() method of JDBC API.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
  * Java program to demonstrate JDBC Batch Insert example. Inserting data in batch
  * seems to improve performance a lot. executeBatch() method of PreparedStatement is
  * used to run batch queries in Java JDBC.
  */

public class MySQLJdbcExample {

    public static void main(String args[]) throws SQLException {
     
        //creating JDBC Connection to mysql database
        String url="jdbc:mysql://localhost:3306/test";
        Connection conn = DriverManager.getConnection(url, "root", "root");
       // conn.setAutoCommit(false); keep auto commit false for better performance

        //query for inserting batch data
        String query = "insert into employee values (?,?,NULL)";
        PreparedStatement pStatement = conn.prepareStatement(query);
        int batchSize = 100;
     
        long startTime = System.currentTimeMillis();
        for (int count = 0; count < 1000; count++) {
            pStatement.setString(1, Integer.toString(count));
            pStatement.setString(2, "Employee" + count);
            pStatement.addBatch();
         
            if (count % batchSize == 0) {
                pStatement.executeBatch();
            }
        }

       pStatement.executeBatch() ; //for remaining batch queries if total record is odd no.
     
     // conn.commit();
        pStatement.close();
        conn.close();
        long endTime = System.currentTimeMillis();
        long elapsedTime = (endTime - startTime)/1000; //in seconds
        System.out.println("Total time required to execute 1000 queries using PreparedStatement with JDBC batch insert is :" + elapsedTime);

     
     
    }
}

Output:
Total time required to execute 1000 queries using PreparedStatement with JDBC batch insert is :28

So JDBC batch insert and update does gives us better performance over queries running without batches. One of the important thing which I have not used here is, I have not disabled auto commit mode. You should always run SQL query with auto-commit mode disabled even with JDBC Batch insert and update example and do commit() explicitly. That will further boost performance of your JDBC code. Try running above code with auto commit mode disabled and it won't take even a second to execute.

Benefits of using JDBC batch update:
Significant improvement in performance can be achieved by using JDBC batch update and insert. Since in case of batch queries, You effectively reduce database round-trip,  You save a lot of time spent on network latency, which results in better performance of Java application. Always combine JDBC batch insert or update with PreparedStatement to get best of both world and also follow these Top 10JDBC best practices while writing JDBC code in Java. e.g. running SQL query with auto-commit mode disabled.

That's all on how to run JDBC Batch insert and update in Java. We have seen how using JDBC batch insert can improve performance and  how we can execute PreparedStatement queries in batch. Choose batch size based on what suits your application and run query with auto commit mode disabled for better performance.

Other JDBC tutorials for Java programmers

3 comments :

Anonymous said...

is there any benefit using core JDBC API for running batch update queries, I am fond of using Spring framework and almost always use JdbcTemplate to group SQL queries in batch and then run. Also executeBatch() method returns an integer array to reflect number of rows affected by this batch update queries.

Rakesh said...

I think using Spring framworks and JdbcTemplate or SimpleJdbcTemplate for batch queries are more simpler example. You just need to call batchInsert() or batchUpdate() method that's it.

Fabricio said...

May be you need to replace this:
if (count % batchSize == 0) {
pStatement.executeBatch();
}
for this:
if (count % batchSize == 0) {
pStatement.executeBatch();
// conn.commit();
}
In case if you set autocomit=false.

Post a Comment