拨乱反正:“通过RAMDirectory缓写提高性能”是错误的!

本文指出在使用Lucene进行索引构建时常见的两个错误:一是ramwriter关闭时机不当,应在合并前关闭;二是认为该方法能提升性能但实际上可能适得其反。通过不同版本的性能对比,展示了直接写入文件系统的效率更高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误有二:

其一:程序错误,ramwriter的关闭时机有问题,应在合并之前关闭。

FSDirectory fsDir = FSDirectory.getDirectory("/data/index", true);
RAMDirectory ramDir = new RAMDirectory();
IndexWriter fsWriter = new IndexWriter(fsDir, new StandardAnalyzer(), true);
IndexWriter ramWriter = new IndexWriter(ramDir, new StandardAnalyzer(), true);
while (there are documents to index)
{
... create Document ...
ramWriter.addDocument(doc);
if (condition for flushing memory to disk has been met)
{
fsWriter.addIndexes(new Directory[] { ramDir });
ramWriter.close();//这一行位置错误,应移到上一行的上面
ramWriter = new IndexWriter(ramDir, new StandardAnalyzer(), true);
}
}

 




其二:这样并不能提高性能,反而会更慢。

测试数据如下:
文件系统现有文件大小            内存索引大小        lucene版本    用时       
767M            <1k        2.2    272203    134250   
770K                        297    391                           

2.4-1.4G                    2.4.1    164781    66938    67578


2.4版本的确比2.2的快一倍。

而如果直接写入文件系统,1.5G的现有索引文件,写入一条document只需要31ms。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值