【问题标题】:How to prevent slowing down when populating a table?填充表格时如何防止变慢?
【发布时间】:2013-08-18 08:22:49
【问题描述】:

我正在从存储在我机器上的文本文件中填充表格。 到此结束时将有大约 100 万条记录,但填充速度太慢,实际上需要 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。 如何阻止它变慢?

玻璃鱼 3.1.2.2

【问题讨论】:

  • 愚蠢的问题......是否涉及交易?你能提前提交交易吗?可以批量更新吗?
  • 为每条记录调用刷新会非常慢。
  • 另外,显示您的提取码。
  • OMG jthalborn,当我没有为每条记录调用 flush 时,它变得如此之快 :O 我不确定它稍后会不会变慢,现在尝试一下 :) MadProgrammer,我的引擎是非交易性的。

标签: java mysql glassfish myisam


【解决方案1】:

在你的循环中刷新并不好,而且你在哪里提交也很重要。另一个要考虑的因素是您在表上有哪些索引。进行导入时是否可以删除它们?也许还可以查看一些批量插入,如

http://viralpatel.net/blogs/batch-insert-in-java-jdbc/

【讨论】:

    【解决方案2】:

    一些可能有助于提高性能的提示:

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多