我正在从存储在我的机器上的文本文件中填充表格。 到目前为止,将有大约一百万条记录,但填充速度太慢,实际上需要12小时才能达到140000条记录。 使用while循环,我提取每条记录所需的信息,然后调用此函数:
public void populateDB(int pid, String id, String title, String yearPublished, String author, String summary) {
Papers p = new Papers();
p.setPid(pid);
p.setPaperId(id);
p.setTitle(title);
p.setYearPublished(yearPublished);
p.setAuthor(author);
p.setSummary(summary);
em.persist(p);
em.flush();
System.out.println("Populated paper " + id);
}
但随着迭代次数的增加,这会显着减慢。 我认为它与cpu的使用有关,似乎限制在50%。但我不知道如何增加这个。最大和最小线程池设置为10。 我怎么能阻止它减速呢?
Glassfish 3.1.2.2
答案 0 :(得分:0)
在你循环中使用flush并不会很好,而且你提交的地方也很重要。另一个要考虑的因素是您在表上有哪些索引。在进行导入时是否可以删除它们?也许还可以在
中查看一些批量插入答案 1 :(得分:0)
一些可能有助于提高绩效的技巧:
1。 如果可能的话,压缩以下七行执行多行方法调用到一行?
Papers p = new Papers();
p.setPid(pid);
p.setPaperId(id);
p.setTitle(title);
p.setYearPublished(yearPublished);
p.setAuthor(author);
p.setSummary(summary);
替换为
Papers p = new Papers(pid, id, title, yearPublished, author, summary); // Saves a lot of cycles.
2。考虑在每个请求上取消em.flush()和控制台输出。如果需要,您可能希望在if()某个计数内执行此操作,例如100。
if(count/100 == 0) { // Reducing the number of expensive IOs
em.flush()
System.out.println("Populated paper " + id);
}