我有一些效率问题。我正在开发一个作为EAR归档部署在jboss EAP 6.1服务器上的企业应用程序。我在while循环中基于实体创建新对象并将它们写入文件。我以有限的量获得这些实体(在EJB DAO的帮助下)(例如,每个步骤2000)。问题是我需要处理数以百万计的对象,前一百万行很顺利,但进一步的循环越慢越好。谁能告诉我为什么这个工作越来越慢,随着循环的进展?我怎样才能让它工作顺利?这里是代码的一些关键部分:lucene在while循环中创建文档的速度越来越慢
public void createFullIndex(int stepSize) {
int logsNumber = systemLogDao.getSystemLogsNumber();
int counter = 0;
while (counter < logsNumber) {
for (SystemLogEntity systemLogEntity : systemLogDao.getLimitedSystemLogs(counter, stepSize)) {
addDocument(systemLogEntity);
}
counter = counter + stepSize;
}
commitIndex();
}
public void addDocument(SystemLogEntity systemLogEntity) {
try {
Document document = new Document();
document.add(new NumericField("id", Field.Store.YES, true).setIntValue(systemLogEntity.getId()));
document.add(new Field("resource", (systemLogEntity.getResource() == null ? "" : systemLogEntity
.getResource().getResourceCode()), Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("operationType", (systemLogEntity.getOperationType() == null ? "" : systemLogEntity
document.add(new Field("comment",
(systemLogEntity.getComment() == null ? "" : systemLogEntity.getComment()), Field.Store.YES,
Field.Index.ANALYZED));
indexWriter.addDocument(document);
} catch (CorruptIndexException e) {
LOGGER.error("Failed to add the following log to Lucene index:\n" + systemLogEntity.toString(), e);
} catch (IOException e) {
LOGGER.error("Failed to add the following log to Lucene index:\n" + systemLogEntity.toString(), e);
}
}
我希望你的帮助!
你看过你的堆统计数据吗? – 2014-09-02 12:18:31
@HotLicks我想过,但说实话,我不太清楚该怎么做。 – AjMeen 2014-09-02 12:22:27
什么是'indexWriter'?看来你正在将所有的文档都添加到它,并且它会保留对它们的引用,并将它们保存在内存中。 – 2014-09-02 12:41:05