简体   繁体   中英

JDBC Insert after Delete slows down performance

I run JDBC queries in a sequence INSERT , DELETE , INSERT , DELETE etc. I insert one million of records in 1000 batches then delete those million records in single query then insert it again. In this case I am interested only in insertion performance.

When I run in in a loop of ie 10 iterations, at 1st iteration the performance is fastest ie 11 seconds then after every next iteration performance of insert is few seconds slower then the previous one. However, when I run it not in the loop the insertion time is very similar.

Any idea why?

for(number of iterations){
      //process insert of million records here, batch size is 1000
      //prepared statement is used and clearBatch is called after every 1000     
      //inserts,
      //at the end prepared statement is closed and connection.commit() is  
      //called

      //Thread.sleep(1000) called here
      //everything is inserted now in the DB so delete what has been inserted  
      //in single query. connection.commit() called again after delete.

      //Thread.sleep(1000) and repeat the same actions until loop finishes.
}

Sorry, I don't have the code with me.

Any idea why at every next iteration the insertion is slower?

I can't be sure without the code, but I think you have a memory leak so that the extra time is due to garbage collection. That would explain why it is faster to run the same code several times without a loop. Run the program with GC logging enabled (-XX:+PrintGC) and see what happens.

To eliminate database issues you may want to test with another (new) table and replace the delete with truncate table, just in case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM