Reputation: 139
I tried to batch insert 10K record by jdbc connection in MSSQL, it took around 18 to 20 secs to bulk insert. I want to insert in less than 3 seconds. Is there any way to insert quickly.
My code given below
public void BatchInsert() {
PreparedStatement pStmnt = null;
try {
final int batchSize = 1000;
int count = 0;
Connection con = createConnection();
long end = System.currentTimeMillis();
System.out.println("connection creation time :: " + (end - start));
start = System.currentTimeMillis();
pStmnt = con.prepareStatement("insert into testtable values(?)");
for (int i = 1; i <= 10000; i++) {
pStmnt.setInt(1, i);
pStmnt.addBatch();
if (++count % batchSize == 0) {
pStmnt.executeBatch();
}
}
pStmnt.executeBatch(); // insert remaining records
end = System.currentTimeMillis();
System.out.println("total insert time :: " + (end - start));
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
pStmnt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Thanks in advance for yr help.
Upvotes: 1
Views: 4898
Reputation: 123829
If your connection is defaulting to AutoCommit mode then you may get some significant improvement in performance by doing
con.setAutoCommit(false);
before entering the loop to addBatch
, and then doing
con.commit();
after the final executeBatch
.
Upvotes: 0
Reputation: 29168
As far I think that, you have used more indexing in your database table. That's why it is taking more time. For this reason, in inserting case, at first you need to stop indexing somehow(maybe deleting and recreating or removing unnecessary indexing).
For operational purpose, you can remove all indexing from your testtable
and run your batch. Hopefully you will get minimal insertion time.
Credit goes to @RBarryYoung
Upvotes: 0