0
我想詢問優化這三個查詢的最佳方式。 該功能將被國家項目中的數百萬用戶使用,我希望它儘可能優化(快速)。在一個事務中執行兩個不同的批量查詢和刪除查詢(Java)
請添加任何有用的評論或回答:) 並預先感謝您。
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);
}
我對代碼的命名和錯誤,我複製我的原代碼,不得不「面具」它真的很抱歉。 我想我會選擇這個作爲我接受的答案,因爲你提到了一些新的東西,這是「不成熟的優化」。 謝謝JB Nizet :) – Faris 2013-05-09 10:00:58