目录

数据量过大的时候导出数据很慢

数据量过大的时候导出数据很慢

原因解析

速度慢无非两个原因:

  • sql取数很慢
  • 程序很慢

sql很慢有3种原因:

  • sql本身查询不合理,需要优化
  • 数据库没有索引
  • 多次频繁访问数据,造成了不必要的开销

取消多次获取数据,一次获取

  • 框定一个大致的范围,获取此次查询的所有数据
  • 使用map设置数据,没有主键使用傅和主键拼接数据
// 使用deliId作为key,将所有未分摊折扣存储在一个Map中
Map<Long, List<SemisteelDiscountPerrow>> discountMap = allDisocuntList.stream().collect(Collectors.groupingBy(SemisteelDiscountPerrow::getDeliId));

// 将数据回填到excel中
map.forEach((key,value) -> {

    valMap[0] = new HashMap(12);
    valMap[0].put("company", value.getCompany());
    valMap[0].put("zyear", value.getZyear());
    valMap[0].put("zmonth", value.getZmonth());
    valMap[0].put("customer", value.getCustomer());
    valMap[0].put("saleOrder", value.getSaleOrder());
    valMap[0].put("deliveryOrder", value.getDeliveryOrder());
    valMap[0].put("wlCode", value.getWlCode());

    dataListChild[0] = new ArrayList<>();
    Map<String, Object> discMap = new HashMap<String, Object>();
    SemisteelDiscountPerrow entity = new SemisteelDiscountPerrow();
    entity.setDeliId(value.getDeliId());
    // List<SemisteelDiscountPerrow> discountList = queryNoZero(entity);
    // 避免使用数据库多次查询,直接使用上面的allDisocuntList查询的数据
    List<SemisteelDiscountPerrow> discountList = discountMap.get(value.getDeliId());
    for (SemisteelDiscountPerrow discount2 : discountList) {
        discMap.put(discount2.getDiscountType(),discount2.getRowDiscount());
    }
    dataListChild[0].add(discMap);
    valMap[0].put("discount", dataListChild[0]);



    list.add(valMap[0]);
});