1

I'm supposed to do a stress testing on my application for which I need 60 million records in a table. The approach I'm following is, first I'm initializing the instance of Connection class, then inserting the data using a for loop by PreparedStatement using that Connection, and after every 500 rows insertion I'm committing the connection and resetting it.

Using this, I'm able to insert approx 150 records per minute. I just want to know if there's any more optimized way of doing it so that time required for execution is minimal

Vishal Afre
  • 985
  • 3
  • 11
  • 36

1 Answers1

3

Use jdbc batch inserts:

PreparedStatement ps = conn.prepareStatement(sql);
int count = 0;
for(loop construct here) {
    count++;
    ps.setString(1,someString);
    // ... and so forth
    ps.addBatch();

    if (count%500 ==0)
        ps.executeBatch()
}
ps.executeBatch();
Gurwinder Singh
  • 37,207
  • 6
  • 50
  • 70
  • 2
    Hi! do i need to execute ps.clearBatch(); after the execute? – Mariano L Jul 30 '18 at 17:56
  • @MarianoL No, you shouldn't need to do that. `executeBatch` should flush the last batch. https://stackoverflow.com/questions/58522625/does-executebatch-also-clear-the-list-of-commands – Gurwinder Singh Mar 03 '22 at 16:13