2

I have a piece of code that executes about 500,000 inserts on a database. It's now done in a loop calling PreparedStatement's executeUpdate at each iteration. Would it be faster to add all 500,000 inserts to a batch and and call executeBatch just once?

5
  • 1
    What does "better" mean? Simpler? Faster? Easier to maintain? Less use of network? Easier to rollback in the event of a problem? Commented Aug 17, 2009 at 18:06
  • 1
    @S.Lott: it's pretty safe to assume "better" means "faster" in 99% of questions about SQL. Commented Aug 17, 2009 at 18:22
  • @Bill Karwin: Interesting observation. How do you know that? Is it true for this question? How do you know that? Commented Aug 17, 2009 at 18:32
  • @S.Lott: Of course, in general "better" is not equal "faster". But other assumptions for "better" in this context are not really obvious. Faster is obvious when talking about JDBC/batch/prepared statement and 500,000 inserts. By keeping questions concise and not explaining obvious facts we do a great deal of favor to other users. Commented Aug 17, 2009 at 18:56
  • It was true for my question. Sorry i didn't make myself clear enough. I meant faster! Commented Aug 17, 2009 at 20:28

3 Answers 3

4

Using PreparedStatement in combination with batch update facility yields most efficient results (from Sun JDBC doc):

// turn off autocommit con.setAutoCommit(false); PreparedStatement stmt = con.prepareStatement( "INSERT INTO employees VALUES (?, ?)"); stmt.setInt(1, 2000); stmt.setString(2, "Kelly Kaufmann"); stmt.addBatch(); stmt.setInt(1, 3000); stmt.setString(2, "Bill Barnes"); stmt.addBatch(); // submit the batch for execution int[] updateCounts = stmt.executeBatch(); 
Sign up to request clarification or add additional context in comments.

Comments

3

Yes, it will be much faster. make sure you turn autoCommit off first, otherwise you get no performance benefit.

Comments

1

500.000 is way too much to add at once. Remember those records are kept in memory and sent at once.Add them in batches of a few thousands, I didn't notice any improvement between 1000 and 10000 rows in a batch(with MySQL) but I presume some other factors counts.

1 Comment

it could quite feasible for small example with prepared statement. but it really depends, i agree...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.