简体   繁体   English

JDBC批处理查询的高性能

[英]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: 我想做批处理查询DB以获得高性能,例如sql根据不同的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. 我不知道如何使用JDBC语句来完成它。 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. 我知道我可以使用存储过程来实现这个目的,但是如果我可以在Java应用程序而不是SP中编写sql,那就更好了。
I'm using DBCP for my Java client and MySQL DB. 我正在使用DBCP作为我的Java客户端和MySQL DB。

The JDBC Specification 4.0 describes a mechanism for batch updates. JDBC Specification 4.0描述了批量更新的机制。 As such, the batch features in JDBC can be used for insert or update purposes. 因此,JDBC中的批处理功能可用于插入或更新目的。 This is described in chapter 14 of the specification. 这在说明书的第14章中描述。

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. AFAIK没有选择批处理的机制,可能是因为没有明显的需要,因为正如其他人所建议的那样,您可以通过正确构建查询来简单地检索所需的所有行。

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? 你有没有理由认为你需要像批量选择语句这样的东西?

It is really does not matter what is your SQL statement (you can use as many nested joins as your DB can handle). 你的SQL语句是什么并不重要(你可以使用尽可能多的嵌套连接,因为你的数据库可以处理)。 Below is basic Java example (not DBCP). 下面是基本的Java示例(不是DBCP)。 For DBCP example which is pretty similar you can check out their example . 对于非常相似的DBCP示例,您可以查看他们的示例

Connection connect = DriverManager.getConnection(YOUR_CONNECTION_STRING);
// Statements allow to issue SQL queries to the database
Statement statement = connect.createStatement();

ResultSet resultSet = statement.executeQuery("select order_id, cost 
                                                from customer c 
                                                join order o using(id) 
                                                where c.id = ... 
                                            order by");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM