0

I have a requirement to read an Excel file with its headers and data and create a table in a Database (MySQL) on the basis of header and put value which is extracted from file. For that I am using JDBC for creating and inserting data (used prepared statement) in DB Table.

It works nicely but when the number of records are increased -suppose file contains 200000 or more records- it's going to be slow. Please guide me how I am able to optimize the processing speed of inserting data into an DB Table.

Thanks, Sameek

4
  • Change to Oracle DB, with a mainframe. Commented Jun 27, 2011 at 7:30
  • .. I thought, a single excel table can't hold more then 65535 records (rows) Commented Jun 27, 2011 at 7:36
  • @Andreas_D: Indeed. I guess he's inserting multiple rows for each Excel row or something... (or maybe he's using multiple sheets) Commented Jun 27, 2011 at 7:45
  • Additionaly to reusing prepared-statements and batches (that two are the most important ones) I think you could set the constraints off for inserting and then activate them when all records are inplace. That should check the constraints in faster way than doing it for every single record you add. But I don't know how to do it in MySQL or if it can be done inside a transaction or you must commit before setting checks on/off. Commented Jun 27, 2011 at 7:49

3 Answers 3

7

To optimize it you should first use the same PreparedStatement object in all of the inserts.

To further optimize the code you can send batches of updates.

e.g. batches of 5:

//create table
PreparedStatement ps = conn.prepareStatement(sql);
for(int i =0; i < rows.length; ++i) {
  if(i != 0 && i%5 == 0) {
    pstmt.executeBatch();
  }
  pstmt.setString(1, rows[i].getName());
  pstmt.setLong(2, rows[i].getId());
  pstmt.addBatch();
}
pstmt.executeBatch();
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - I learned that people sometimes forget, that we prepare a statement to (re-)use it again and again (sure, might not be the case here)
1

Wrap your inserts in a transaction. Pseudo code:

1) Begin transaction 2) Create prepared statement 3) Loop for all inserts, setting prepared statement parameters and executing for each insert 4) Commit transaction

Comments

0

I'll take the example of hibernate. Hibernate have a concept called HibernateSession which stores the SQL command that are not yet sent to DB. With Hibernate you can do inserts and flush the session every 100 inserts which means sending SQL queries every 100 inserts. This helps to gain performance because it communicates with database every 100 inserts and not each insert.

So you can make the same thing by executing the executeUpdate every 100 (or what ever you want) times or use preparedStatement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.