通过使用表中的JPA并写入文件来检索大量数据的最佳方法

时间:2016-12-09 08:53:38

标签: java performance hibernate for-loop

我使用以下代码片段来获取大约300,000条记录(列数为20)并每天写入文件

 Object total = session.getNamedQuery("countAllExportablePackagingInstructions").uniqueResult();
        int iTotal = ((Long)total).intValue();

    ScrollableResults results = session
            .getNamedQuery("listAllExportablePackagingInstructions")
            .scroll(ScrollMode.FORWARD_ONLY);

    try {
        int count = 0;            
        log.error("[R91 Export] Starting Export of R91 packaging instructions...");
        while (results.next()) {
            PackagingInstruction packins = (PackagingInstruction)results.get(0);
            try {
                writer.write(packins);
                if (++count % 1000 == 0) {
                    log.info("[R91 Export] Generated " + count + " of " + iTotal + " packaging instructions [" + ((double)count/iTotal)*100 + "%]");
                }
            } catch (Exception e) {
                log.error("Error generating CSV row for packins: " + packins.getId() + " : " + e.getMessage());
            }
        }
    } finally {
        writer.close();
        results.close();
    }

但它花了大约30到45分钟的时间。如果我还能缩短执行时间,请指导我。

如果我将结果带入对象列表并使用嵌套for循环迭代列表并写入文件

0 个答案:

没有答案