我想问一下优化这三个查询的最佳方法。这个功能将在一个国家项目中被数百万用户使用,我希望它尽可能优化(快速)。
请添加任何有用的评论或答案:) 并提前感谢您。
try {
//so we don't execute empty batches
boolean flag=false;
conn = dataSource.getConnection();
autoCommitValue = conn.getAutoCommit();
conn.setAutoCommit(false);
stmt = conn.prepareStatement("DELETE FROM table1 WHERE input = ?");
stmt.setLong(1, input);
stmt.executeUpdate();
stmt = conn
.prepareStatement("INSERT INTO table1 (c1) values (?)");
for (Object Info : Info List) {
if (Info .getType() == 0) {
flag=true;
stmt.setInt(1, Info.getC1());
stmt.addBatch();
}
}
if(flag){
result = stmt.executeBatch();
flag=false;
}
stmt = conn.prepareStatement("INSERT INTO table1 (c1,c2,c3) values (?,?,?)");
for (Object Info : Info List) {
if (Info .getType() != 0) {
flag=true;
stmt.setLong(1, input);
stmt.setInt(2, Info.getC1());
stmt.setInt(3, resInfo.getC2());
stmt.addBatch();
}
}
if(flag){
result = stmt.executeBatch();
}
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw new SQLException();
} finally {
if (conn != null) {
// now we return the connection to its original state
conn.setAutoCommit(autoCommitValue);
}
closeDBObjects(conn, stmt);
}