Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC batch query for high performance

I want to do batch query DB for high performance, example sql to query based on different customer_id:

select order_id, 
       cost 
from customer c 
  join order o using(id) 
where c.id = ... 
order by

I'm not sure how to do it using JDBC statement. I know I can use stored procedure for this purpose, but it's much better if I can just write sql in Java app instead of SP.
I'm using DBCP for my Java client and MySQL DB.

like image 598
superche Avatar asked Mar 24 '12 16:03

superche


People also ask

Can we perform batch SQL processing in JDBC?

Batch Processing Using Statement. With JDBC, the simplest way to execute queries on a database is via the Statement object. First, using addBatch() we can add all SQL queries to a batch and then execute those SQL queries using executeBatch().

Which method is used to perform batch processing in JDBC?

The addBatch() method of Statement, PreparedStatement, and CallableStatement is used to add individual statements to the batch. The executeBatch() is used to start the execution of all the statements grouped together.

What is the max JDBC batch size?

batch is limited to 32K, keep track of the batch elements in the application, then issue an execute command before the 32K limit is reached.


1 Answers

The JDBC Specification 4.0 describes a mechanism for batch updates. As such, the batch features in JDBC can be used for insert or update purposes. This is described in chapter 14 of the specification.

AFAIK there is not a mechanism for select batches, probably because there is no apparent need for that since, as others have recommended, you can simply retrieve all the rows that you want at once by properly constructing your query.

int[] ids = { 1, 2, 3, 4 };
StringBuilder sql = new StringBuilder();
sql.append("select jedi_name from jedi where id in(");
for (int i = 0; i < ids.length; i++) {
    sql.append("?");
    if(i+1 < ids.length){
        sql.append(",");
    }
}
sql.append(")");
System.out.println(sql.toString());

try (Connection con = DriverManager.getConnection(...)) {

    PreparedStatement stm = con.prepareStatement(sql.toString());
    for(int i=0; i < ids.length; i++){
        stm.setInt(i+1, ids[i]);
    }

    ResultSet rs = stm.executeQuery();
    while (rs.next()) {
        System.out.println(rs.getString("jedi_name"));
    }

} catch (SQLException e) {
    e.printStackTrace();
}

Output

select jedi_name from jedi where id in(?,?,?,?)
Luke, Obiwan, Yoda, Mace Windu

Is there any reason why you would consider that you need a thing like a batch-select statement?

like image 110
Edwin Dalorzo Avatar answered Sep 21 '22 04:09

Edwin Dalorzo