0

I have a client Java 7 app that has to insert large number of small data (few columns) into the database. Database (Mysql 5.6) is located on my server. Client side Java app calls stored procedure in a loop for inserting single row. Row is simple, looks like this:

int | int | varchar | varchar | int | datetime

and the current way i'm doing that is like this (pseudo code):

try(DbConnection conn = new DbConnection()) {
for(RowModel model : rowModel) {
//inserting row here
}
}
catch(SqlException e) {
//process exception...
}

As you can see, i'm opening connection once and i insert row by row. "rowModel" may have thousands of rows and to me it looks like it would be more reasonable to send 1 "LongText" to mysql stored procedure which will somehow delimit that longtext to be suitable for one single "insert" call.

Is it possible and how? What do you propose?

So, basically, to sum things: 1) is there any benefit in calling one "insert" with a lot of values instead of having "for-each" on client's side? 2) How could i write my stored procedure so it takes 1 big text and inserts it using single "INSERT" command?

4
  • You can do insert into t (col1, col2) values (1,2), (3,4), (5,6) or look into BULK insertion tipps: dev.mysql.com/doc/refman/5.5/en/… Commented Apr 20, 2016 at 19:52
  • There are relevant performance benefit calling on insert with a lot of values .. you execute just a command instead of many commands like your rows .. Commented Apr 20, 2016 at 19:58
  • @scaisEdge that could be an answer if you put it below. Points fame and fortune to follow. Commented Jun 9, 2016 at 22:09
  • @Drew . the comment is posted like an answer .. hope this is useful to someone Commented Jun 10, 2016 at 6:27

1 Answer 1

1

There are relevant performance benefit calling on insert with a lot of values .. you execute just a command one time onle instead of many commands like how many rows you have to insert ..

Typically the batch insert, so is also called this tecnique, reduce the duration of the procedure of many time ..

eg: in some empiric test inserting 1000 in insert loop the execution time was of 1 minutes .. with batch insert the time result is 2 secs

Sign up to request clarification or add additional context in comments.

1 Comment

Thx for sharing scaisEdge

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.