solr全量建索引一般瓶颈都出现在读数据源这一端。 目前数据库为Mysql,单表数据2000w,如果采用分页读取mysql的方式,大家都知道Mysql分页越靠后分页速度越慢。本人在项目中采取如下方案进行解决。
假如商品表goods存在2000w数据,其中商品id为自增长方式。读取数据源优化步骤如下
- 复制商品表及表结构到另一张表 create table goods_replication from select * from goods 大概耗时5分钟
- 生成一张表与商品结构相同 goods_tempory_record
- 将goods_replication 表数据复制10w条到goods_tempory_record 表中 (insert into goods_tempory_index select * from 1dcq_goods_replication order by goods_id asc limit 100000) 大概耗时2s
- 找出拷贝的10w条数据中最大的商品id记录为maxGoodsId (select max(t.goods_id) from (select goods_id from 1dcq_goods_replication go order by go.goods_id limit 99999,1)t) 耗时 不超过1s
- 根据maxGoodsId删除掉goods_replication中的前10w条记录 (delete from 1dcq_goods_replication where goods_id<maxGoodsId order by goods_id asc) 耗时大 概 10s
- 对goods_tempory_record中的10w数据建索引 (采用多线程解析数据,多线程提交到solr,每10w条进行一次硬提交)
- truncate table goods_tempory_record
- 重复循环 3-7这几个步骤,直到goods_replication中没有数据为止
总结如下
2000w数据,采用15个线程进行多线程提交,每10w数据进行一次硬提交,建索引耗时3小时