mongoTemplate查詢大數據過慢


先上兩段代碼
代碼一

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);
}
邏輯很簡單,從testEntity集合中根據id列表和開始結束時間進行文檔篩選,但是在大數據量下,差別太大了!
比如testEntity集合有25萬條文檔,查詢出4萬條文檔轉換成TestEntity實體類集合,第一種直接轉換成實體要80秒,而第二種耗時1秒不到!我一度還以為是mongo的問題,后來才定位到是代碼的坑!

所以數據量大的時候還是用原生查詢手動映射成實體類比較快!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM