美文网首页
MongoTemplate实现分页查询大量数据及效率优化

MongoTemplate实现分页查询大量数据及效率优化

作者: 南瓜pump | 来源:发表于2023-03-13 13:42 被阅读0次
1. 关于分页查询,可以使用Pageable构建条件实现分页查询:
    public IPage<AIBoxDTO> pageRecords(AIBoxCond cond) {
        // 分页参数
        Pageable pageable = PageRequest.of(cond.getCurrent(), cond.getSize());

        // 构造查询条件
        Query query = new Query();

        // 判断是否需要按照报警时间查询
        if (!ObjectUtils.isEmpty(cond.getBeginTime()) && !ObjectUtils.isEmpty(cond.getEndTime())) {
            Criteria criteria = new Criteria();
            criteria.andOperator(Criteria.where("dts").gte(cond.getBeginTime().getTime()), Criteria.where("dts").lte(cond.getEndTime().getTime()));
            query.addCriteria(criteria);
        }
        // 查询总数
        long totalCount = mongoTemplate.count(query, AIBoxDTO.class);

        // 去除photo字段显示,避免数据量过大导致查询失败,要查看图片通过详情接口查询(/flow/api/aibox/getById)
        // query.fields().exclude("photo");

        // 增加分页条件
        query.with(pageable);
        // 按照报警时间排序倒排
        query.with(Sort.by(Sort.Direction.DESC, "dts"));
        List<AIBoxDTO> dtos = mongoTemplate.find(query, AIBoxDTO.class);

        // 构造分页返回
        Page<AIBoxDTO> page = new Page<>(cond.getCurrent(), cond.getSize());
        page.setTotal(totalCount).setRecords(dtos);

        return page;
    }
2. 关于查询效率优化,以上分页查询虽能实现分页功能但是查询效率很低,笔者排查之后发现是mongoTemplate.count()方法查询过慢导致的,查询资料之后优化如下:
  • 首先给查询条件增加索引,在对应的属性上加上@Indexed注解,
  • 如果查询条件不为空,使用countDocuments查询统计数量,
  • 如果查询条件为空,使用estimatedDocumentCount查询统计数量,
  • 另外,可以使用query.fields().exclude()方法去除不需要的字段。
    public IPage<AIBoxDTO> pageRecords(AIBoxCond cond) {
        // 分页参数
        Pageable pageable = PageRequest.of(cond.getCurrent(), cond.getSize());

        // 总数
        long totalCount;

        // 构造查询条件
        Query query = new Query();

        // 判断是否需要按照报警时间查询
        if (!ObjectUtils.isEmpty(cond.getBeginTime()) && !ObjectUtils.isEmpty(cond.getEndTime())) {
            Criteria criteria = new Criteria();
            criteria.andOperator(Criteria.where("dts").gte(cond.getBeginTime().getTime()), Criteria.where("dts").lte(cond.getEndTime().getTime()));
            query.addCriteria(criteria);
            // 查询总数,当查询条件不为空时用countDocuments统计数量,并给dts加索引以优化查询速度
            totalCount = mongoTemplate.count(query, AIBoxUploadMsgDTO.class);
        } else {
            // 查询总数,当查询条件为空时用estimatedDocumentCount统计数量以优化查询速度
            totalCount = mongoTemplate.getCollection("aiBoxRecord").estimatedDocumentCount();
        }

        // 去除photo字段显示,避免数据量过大导致查询失败,要查看图片通过详情接口查询(/flow/api/aibox/getById)
        query.fields().exclude("photo");

        // 增加分页条件
        query.with(pageable);
        // 按照报警时间排序倒排
        query.with(Sort.by(Sort.Direction.DESC, "dts"));
        List<AIBoxUploadMsgDTO> dtos = mongoTemplate.find(query, AIBoxUploadMsgDTO.class);

        // 构造分页返回
        Page<AIBoxUploadMsgDTO> page = new Page<>(cond.getCurrent(), cond.getSize());
        page.setTotal(totalCount).setRecords(dtos);

        return page;
    }

相关文章

  • ASP.NET MVC5 实现分页查询

    对于大量数据的查询和展示使用分页是一种不错的选择,这篇文章简要介绍下自己实现分页查询的思路。 分页需要三个变量:数...

  • MySQL 面试系列:MySQL 常见的开放性问题

    有一个超级大表,如何优化分页查询? 超级大表的分页优化分有以下两种方式: 数据库层面优化:利用子查询优化超多分页场...

  • 64MySQL-分页查询&表连接&count统计&索引优化总结

    1 Mysql 分页查询sql 执行原理? 2,千万级数据mysql 分页查询如何优化 3,Mysql表连接底层实...

  • mysql深入篇

    目录 优化篇 学习 优化 慢查询优化,我终于在生产踩到了这个坑!! 学习 数据量很大,分页查询很慢,有什么优化方案...

  • 6

    结构 1:Android中数据库的创建 2:ListView的优化 3:数据库的分页查询以及ListView分页展示

  • SQL limt优化

    避免数据量大时扫描过多的记录解决:子查询的分页方式或者JOIN分页方式。JOIN分页和子查询分页的效率基本在一个等...

  • JAVA Web学习(16)___10.4 JDBC在Jave

    10.4 JDBC在Jave Web中的应用 分页查询 通过MySQL数据库提供的分页机制,实现商品信息的分页查询...

  • sql优化

    1. 大量数据的分页查询 有一张财务流水表,未分库分表,目前的数据量为9555695,分页查询使用到了limit,...

  • MongoTemplate spring实现分页

  • 一个SQL优化技巧

    一个SQL优化技巧 我们系统中存在大量类似如下查询: 这些查询在数据量小的时候,不会出现效率瓶颈,但是数据量一旦增...

网友评论

      本文标题:MongoTemplate实现分页查询大量数据及效率优化

      本文链接:https://www.haomeiwen.com/subject/xvscrdtx.html