• mongo大数据量查询优化


    mongo in查询数量较多时

    时间对比:

    4W条数据查询速度达到3-4S

    优化后可以达到0.1S-0.2S

    优化原理:mongo自动Bean转化功能性能较差,改用原生mongo游标方法读取MongoDB数据文档,并在内存中做bean转化

    优化前

    Query query = new Query();
    
    queryAfter.addCriteria(Criteria.where("id").in(idList));
    queryAfter.addCriteria(Criteria.where("time").gte(startTime).lte(endTime));
    List<TestEntity> lists = mongoTemplate.find(queryBefore,TestEntity.class);
    

     优化后

    DBObject query1 = new BasicDBObject(); //setup the query criteria 设置查询条件
     query1.put("id", new BasicDBObject("$in", idList));
     query1.put("time", (new BasicDBObject("$gte", startTime)).append("$lte", endTime));
     DBCursor dbCursor =mongoTemplate.getCollection("testEntity").find(query1);
     List<TestEntity> list=new ArrayList<>();
     while (dbCursor.hasNext()){
     DBObject object=dbCursor.next();
     TestEntity te=new TestEntity();
     te.setId(object.get("_id").toString());
     te.setTime((Date) object.get("time"));
     list.add(te); 
    }
    
  • 相关阅读:
    hdu 1312 ( Red and Black )
    hdu 1429 ( 胜利大逃亡(续) )
    zjut 小X的苹果
    hdu 1253 ( 胜利大逃亡 )
    许多事
    1198 ( Farm Irrigation )
    hdu 1241 Oil Deposits
    hdu 1242 ( Rescue )
    hdu 1240 ( Asteroids! )
    zoj2966 build the electric system
  • 原文地址:https://www.cnblogs.com/tianmh/p/12344827.html
Copyright © 2020-2023  润新知