zoukankan      html  css  js  c++  java
  • 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的问题,后来才定位到是代码的坑!

    所以数据量大的时候还是用原生查询手动映射成实体类比较快!

  • 相关阅读:
    Python3与Python2的区别(转载)
    Python——通过斐波那契数列来理解生成器
    Solr4.8.1与Tomcat7整合
    Solr配置与简单Demo
    lucene 4.4 demo
    企业级搜索引擎Solr使用入门指南
    Solr使用入门指南
    使用solr搭建你的全文检索
    Solr 1.3 安装步骤
    Lucene/Solr开发经验
  • 原文地址:https://www.cnblogs.com/exmyth/p/10770964.html
Copyright © 2011-2022 走看看