views:

563

answers:

3

Subject to this question, asks it all:How effective is executeBatch method? Is there a performance benchmark, that says.. 'if you have 1000 records to be inserted, using a executeBatch instead of executeUpdate saves you x amount of database cycles?'

Or Is this just a convention?

EDIT: Here is what I am working with: a DB2 V 8.1 hosted on Z/OS, a web app that would be inserting 80,000 records at one go in it's worst case scenario execution.

+1  A: 

I'm not sure what you're asking, but for inserting many thousands of rows, a batched statement is hugely faster. I can't give you numbers, though.

skaffman
@skaffman that was my question - how hugely faster is it? Can you give me an approximation?
Jay
I'd estimate somewhere between 10 and 20 times, it would depend to a large extent on the database itself. A non-batched statement involves a lot more network traffic, so a remove server would benefit more from batching than a local one, for example.
skaffman
More than 10-20x in my experience (for my particular use case).
Jack Leow
(and environment)
Jack Leow
+1  A: 

In my experience, it is significantly faster - even if you are inserting/updating just a few records at a time. If you are doing more than one update, I would almost always recommend batching them if it makes sense.

That said, you'd have to do some actual testing to figure out the performance improvement for your particular situation.

Eric Petroelje
+1  A: 

Not sure what database you are using. When I ran a test on this using db2 this is what I saw:

To write to the database:

1 insert it took 2500 microseconds.

10 inserts it took 6000 microseconds. (600 microseconds per write)

10000 inserts it took about 1 million microseconds. ( 100 microseconds per write)

Performance maxed out there. All this means is that there is a huge overhead in sending messages, and using a batch method minimizes this. Of course, sending inserts/updates in huge batches runs the risk of losing them if the application crashes.

Also of note: Exact numbers will vary depending on your DB and settings. So you will have to find your own "sweet spot." But this gives you an idea.

windfinder
@windfinder did you use executeBatch for all the above? And, you said 'huge batches run risk of being lost in application crashes' - if I am using transaction management - I should be good in this scenario?
Jay
Yes, I used executeBatch. Transaction management will not completely eliminate this risk, but it might reduce it.
windfinder
In the simplest of ways, if I were to set autocommit to false and commit only when the executeBatch returns, How am I at risk?
Jay