I have a database thread that sorts all insert queries into a single transaction every 500ms. Currently, that application does a single insert for every piece of data. Would it be more efficient or less efficient to sort those many insert statements into alike inserts and do a fewer amount of inserts with a lot more elements? I'm trying to reduce the amount of CPU power needed for the inserts.
IE (But in actuality it's on a much larger scale with many more elements)
Start Transaction
INSERT (A,B,C) INTO TABLE VALUES(1,2,3)
INSERT (A,B,C) INTO TABLE VALUES(4,5,6)
INSERT (A,B,C) INTO TABLE VALUES(7,8,9)
INSERT (A,B,C) INTO TABLE VALUES(10,11,12)
INSERT (A,B,C) INTO TABLE VALUES(13,14,15)
INSERT (A,B,C) INTO TABLE2 VALUES(1,2,3)
INSERT (A,B,C) INTO TABLE2 VALUES(4,5,6)
INSERT (A,B,C) INTO TABLE2 VALUES(7,8,9)
INSERT (A,B,C) INTO TABLE2 VALUES(10,11,12)
INSERT (A,B,C) INTO TABLE2 VALUES(13,14,15)
End Transaction
Versus
Start Transaction
INSERT(A,B,C) INTO TABLE VALUES((1,2,3),(4,5,6),(7,8,9),(10,11,12),(13,14,15))
INSERT(A,B,C) INTO TABLE2 VALUES((1,2,3),(4,5,6),(7,8,9),(10,11,12),(13,14,15))
End Transaction
Code as requested:
public void dbCallInsert(List<String> query) {
// Good practice to create a new statement and result set instead of reusing
Statement stmt = null;
Connection conn = null;
try {
// Fetch the query and the request out of the QueryRequest object
conn = cpds.getConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
String temp;
for (String a: query) {
stmt.execute(a);
}
conn.commit();
stmt.close();
conn.close();
} catch (Exception e) {
errorLog.error("Failed to issue database command " + query + ": " + e);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
errorLog.error("Failed to close JDBC statement.");
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
errorLog.error("Failed to close JDBC connection.");
}
}
}
}