mongo大数据量查询优化
Posted 初心tianmh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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); }
以上是关于mongo大数据量查询优化的主要内容,如果未能解决你的问题,请参考以下文章