我正在通过 JDBC Prepared 语句更新远程数据库。当我执行相同的代码来更新本地数据库时,它需要几毫秒,但对于远程数据库来说,大约 900 行的时间太长了。所有行的总大小为 160 KB。远程主机是 Hostgator,表的数据库引擎是 InnoDB。
这是我的代码:
String insertTableSQL = "INSERT INTO abc(zzz,c,d,e,timestamp) VALUES (?,?,?,?,?)";
String delSt = "DELETE FROM abc WHERE zzz= ?";
del = connection.prepareStatement(delSt);
preparedStatement = connection.prepareStatement(insertTableSQL);
connection.setAutoCommit(false);
Iterator<DataObject> itr = updationList.iterator();
while (itr.hasNext()) {
DataObject dO = itr.next();
del.setString(1, dO.a + ":" + dO.b);
for (SubObject sO : dO.getsO.values()) {
try {
preparedStatement.setString(1, sO.a + ":" + sO.b);
preparedStatement.setString(2, sO.c);
preparedStatement.setInt(3, sO.d);
preparedStatement.setFloat(4, sO.e);
preparedStatement.setTimestamp(5, getCurrentTimeStamp());
preparedStatement.addBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
try {
del.execute();
preparedStatement.executeBatch();
} catch (Exception e) {
e.printStackTrace();
}
try {
connection.commit();
}
...