赞
踩
需要对mongodb进行分页查询,但是分页查询效率很慢,mongdb中存在主键增长,故取出最大id,定义pageSize,当前传入的id如果加上pageSize小于最大值时,userId+=pageSize,简单代码如下
IntegerpageSize =200;
final LongmaxId =17202814L;
log.info("插入开始时间为{}", LocalDateTime.now());
boolean flag =true;
int pageNo =0;
while (flag) {
try {
if (userId >maxId) {
flag =false;
}
long startTime = System.currentTimeMillis();
//log.info("当前pageNo为{}", pageNo);
//int start = pageNo * pageSize;
Query query =new Query(Criteria.where("_id").gt(userId).lte(userId +pageSize));
userId +=pageSize;
//查出分页用户
List xpUsers =mongoTemplate.find(query, XpUser.class);
long endTime = System.currentTimeMillis();
log.info("从mongodb中查出数据时间{}", (endTime - startTime));
/*if (xpUsers.size() != 0) {*/
if(xpUsers.size()==0){
continue;
}
transferData(xpUsers, pageSize * (pageNo +1));
pageNo++;
startTime = System.currentTimeMillis();
log.info("封装插入mysql数据总时间{}", (startTime - endTime));
if (maxId - userId
//最后一条数据
pageSize = Math.toIntExact(maxId - userId);
if(pageSize <=0){
flag =false;
}
}
}catch (Exception e) {
flag =false;
log.error("插入出错,pageNo为{}", pageNo, e);
}
}
log.info("插入结束时间为{}", LocalDateTime.now());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。