在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();发布于 2019-05-03 14:05:02
假设字段'appID‘已经在DB中建立了索引(这对聚合管道有很大帮助):对于上面提到的数据大小,上面的查询最有可能在排序操作中消耗时间。尝试提供聚合选项'allowDiskUse:true‘。看一看this SO question
https://stackoverflow.com/questions/55963699
复制相似问题