黑鹰猎手 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 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝