首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MongoDB:如何优化大数据聚合查询

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

Stack Overflow用户
提问于 2019-05-03 13:18:29
回答 1查看 85关注 0票数 0

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

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

发布于 2019-05-03 14:05:02

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55963699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档