简体   繁体   English

删除后JDBC插入会降低性能

[英]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. 我按INSERTDELETEINSERTDELETE等顺序运行JDBC查询。我以1000个批次插入一百万条记录,然后在单个查询中删除那些百万条记录,然后再次插入。 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. 当我以10次迭代的循环运行时,在第一次迭代中,性能最快,即11秒,然后在插入下一个迭代之后,性能比上一次慢几秒钟。 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. 运行启用了GC日志记录(-XX:+ PrintGC)的程序,然后查看会发生什么。

To eliminate database issues you may want to test with another (new) table and replace the delete with truncate table, just in case. 为了消除数据库问题,您可能需要用另一个(新)表进行测试,并用truncate表替换delete,以防万一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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