标签:basic ODB string before template 查询条件 pre criteria lte
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); }
标签:basic ODB string before template 查询条件 pre criteria lte
原文地址:https://www.cnblogs.com/tianmh/p/12344827.html