黑鹰猎手 2019-03-12 20:07 采纳率: 0%
浏览 1021

MongoDB 内嵌文档聚合分组性能优化

查询需求:

# 根据describe下的ruleType 字段分组,查询各个ruleType对应的个数。

1.文档内容如下

{ 
    "_id" : "593700197", 
    "_class" : "com.sefon.demo", 
    "describe" : [
        {
            "ruleType" : "FieldFormatVerify", 
            "ruleDetails" : "内容的格式或者长度不正确,正确格式为 c.. 8.0"
        }, 
        {
            "ruleType" : "SFFieldDefect", 
            "ruleDetails" : "内容为空"
        }
    ], 
    "findTime" : NumberLong(1550928738469)
},
{ 
    "_id" : "593700198", 
    "_class" : "com.sefon.demo", 
    "describe" : [
        {
            "ruleType" : "FieldFormatVerify", 
            "ruleDetails" : "内容的格式或者长度不正确,正确格式为 c.. 8.0"
        }
    ], 
    "findTime" : NumberLong(1550928738469)
}

2.本人使用的查询语句

  Aggregation agg = newAggregation(
                    Aggregation.match(criteria),
                    Aggregation.unwind("describe"),
                    Aggregation.group("describe.ruleType").count().as("count")
            );
            List<DBObject> describeGroup = mongoOperations.aggregate(agg, ProblemInfo.class, DBObject.class).getMappedResults();

3.数据总量是200万,此聚合查询需要5秒左右,如何做能提升查询速度呢?
我已做如下索引创建,没有任何提升,求大神提供解决方案

db.problemInfo.ensureIndex({"ruleType":1});
db.problemInfo.ensureIndex({"describe":1});
db.problemInfo.ensureIndex({"describe.ruleType":1});
  • 写回答

1条回答 默认 最新

  • weixin_38498513 2022-04-24 14:05
    关注

    同问

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?