MongoDB:如何优化大数据聚合查询

时间:2019-05-03 05:18:29

标签: java mongodb spring-boot

在Mongodb集合中,有3000万条记录。当查询存在值时,它会在1秒内给出结果。但是不存在值的查询花了40或45秒才能得出结果为null或0。为什么会这样

String cDate = dateFormat.format(date);
String pastTime = timeFormatMin.format(new Date(System.currentTimeMillis() - 3600 * 1000));
Criteria dateQuery = Criteria.where("Date").is(cDate);
Criteria timeQuery = Criteria.where("Time").gt(pastTime);
Criteria appQuery = Criteria.where("appID").is(appId).andOperator(Criteria.where("appID").exists(true));
Criteria criteria = new Criteria().andOperator(dateQuery, timeQuery, appQuery);

MatchOperation matchOperation = match(criteria);
GroupOperation groupOperation = group("appID").count().as("idcount");
ProjectionOperation projection = project()
            .andExpression("_id").as("appID")
            .andExpression("idcount").as("Count");
SortOperation sortOperation = sort(new Sort(Sort.Direction.DESC, "_id"));
LimitOperation limitOperation = limit(1);

Aggregation aggregation = newAggregation(matchOperation, sortOperation, limitOperation);
AggregationResults<CommonLogic> logResult = mongoTemplate.aggregate(aggregation, "commonLogic", CommonLogic.class);
List<CommonLogic> list = logResult.getMappedResults();

1 个答案:

答案 0 :(得分:0)

假设字段“ appID”已在数据库中建立索引(这对聚合管道很有帮助): 对于提到的数据大小,以上查询最有可能在排序操作中消耗时间。尝试将聚合选项设置为“ allowDiskUse:true”。看一下 this SO question