SpringBoot+MongoDB查询大数据字段优化

记录一下

SpringBoot+MongoDB查询大数据字段,查询的单个字段或者总查询结果量太大

用 mongoTemplate.find(query,NewSnapshot.class,collectionName); 查询比较慢

刚开始是这样查询的

public List<xxx>  sss(String a,String b,String c) {

   Query query = new Query(Criteria.where("a").is(a).and("b").is(b));
   query.with(Sort.by(Sort.Order.asc("update_time")));
   List<xxx> result = mongoTemplate.find(query,collectionName);
   return result;
}

结果1M数据量,单个字段40k的情况下,压测结果,110/s

修改为

public  List<xxx>  newSnapshotPage2(String a,String c) {

   MongoCollection<Document> collection = mongoTemplate.getCollection(a);
   FindIterable<Document> findIterable = collection.find(new Document("b",b).append("c",c));
   MongoCursor<Document> mongoCursor = findIterable.iterator();
   Xxx   xxx = new Xxx ();
   List<xxx> result = new ArrayList<>();
   while (mongoCursor.hasNext()){
      Document next = mongoCursor.next();
      xxx .setA(next.get("1").toString());
      xxx .setP(next.get("2").toString());
      xxx .setU(next.get("3").toString());
      xxx .setB(next.get("4").toString());
      result.add(xxx);
   }
   return result;
}

同样的设备压测结果为,187/s

 性能是提升很多的。

新人没有逻辑,只为自己记录。

相关文章

文章浏览阅读752次。关系型数据库关系型数据库是一个结构化的...
文章浏览阅读687次,点赞2次,收藏5次。商城系统中,抢购和秒...
文章浏览阅读1.4k次。MongoTemplate开发spring-data-mongodb...
文章浏览阅读887次,点赞10次,收藏19次。1.背景介绍1. 背景...
文章浏览阅读819次。MongoDB连接失败记录_edentialmechanisn...
文章浏览阅读470次。mongodb抽取数据到ES,使用ELK内部插件无...