常见的场景优化方案:导出Excel大批量数据的优化过程

本文介绍了在用户数据看板项目中遇到的大批量数据导出Excel效率低下的问题,详细分析了原有代码的性能瓶颈,并讲述了采用阿里EasyExcel组件进行优化的过程。优化后,10W+数据的导出时间从约8分钟缩短至8秒,显著提升了导出效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

团队目前在做一个用户数据看板(下面简称看板),基本覆盖用户的所有行为数据,并生成分析报表,用户行为由多个数据来源组成(餐饮、生活日用、充值消费、交通出行、通讯物流、交通出行、医疗保健、住房物业、运动健康...),

基于大量数据的组合、排序和统计。根据最新的统计报告,每天将近100W+的行为数据产生,所以这个数据基数是非常大的。

而这个数据中心,对接很多的业务团队,这些团队根据自己的需要,对某些维度进行筛选,然后直接从我们的中心上下载数据(excel)文档进行分析。所以下个几十万上百万行的数据是很常见的。

问题和解决方案

遇到的问题

目前遇到的主要问题是,随着行为能力逐渐的完善闭环,用户数据沉淀的也越来越多了,同时业务量的也在不断扩大。

业务团队有时候会下载超量的数据来进行分析,平台上的数据下载能力就显得尤为重要了。而我们的问题是下载效率太慢,10W的数据大约要5分钟以上才能下载下来,这显然有问题了。

解决步骤

代码是之前团队遗留的,原先功能没开放使用,没有数据量,所以没有发现问题。以下是原来的导出模块,原程序如下,我做了基本还原。

现在如何保证数据的高效导出是我们最重要的目标,这个也是业务团队最关心的。 

  1     /**
  2      * 获取导出的Excel的文件流信息
  3      * @param exportData
  4      * @return
  5      * @throws Exception
  6      */
  7     private OutputStream getExportOutPutStream(List<UBehavDto> exportData) throws Exception {
  8         JSONObject object = new JSONObject();
  9         List<ExcelCell[]> excelCells = new ArrayList<>();
 10         String[] headers = new String[] { "A字段","B字段","C字段","D","E","F","G","H","I","J","K","L",
 11                 "M","N","O","P","Q","R","S","T","U","V","W",
 12                 "X","Y","Z","AA","AB","AC","AD","AE字段","AF字段","AG字段" };
 13         ExcelCell[] headerRow = getHeaderRow(headers);
 14         excelCells.add(headerRow);
 15         String pattern = "yyyy-MM-dd hh:mm:ss";
 16         for (UBehavDto uBehavDto:exportData) {
 17             String[] singleRow = new String[] { uBehavDto.getA(),uBehavDto.getB(),uBehavDto.getC(),uBehavDto.getD(),uBehavDto.getE(),uBehavDto.getF(),
 18                     DateFormatUtils.format(uBehavDto.getAddTime(), pattern),DateFormatUtils.format(uBehavDto.getDate(), pattern),
 19                     uBehavDto.getG(),uBehavDto.getH(),uBehavDto.getI(),uBehavDto.getJ(),uBehavDto.getK(),uBehavDto.getL(),uBehavDto.getM(),
 20                     uBehavDto.getN(),uBehavDto.getO(),uBehavDto.getP(),
 21                     uBehavDto.getQ(),uBehavDto.getR(),uBehavDto.getS(),String.valueOf(uBehavDto.getT()),uBehavDto.getMemo(),uBehavDto.getU(),uBehavDto.getV(),
 22                     uBehavDto.getW(),uBehavDto.getX(),
 23                     uBehavDto.getY(),uBehavDto.getZ(),uBehavDto.getAA(),uBehavDto.getAB(),uBehavDto.getAC() };
 24             ExcelCell[] cells = new ExcelCell[singleRow.length];
 25             ExcelCell getA=new ExcelCell();getA.setValue(uBehavDto.getA());
 26             ExcelCell getB=new ExcelCell();getB.setValue(uBehavDto.getB());
 27             ExcelCell getC=new ExcelCell();getC.setValue(uBehavDto.getC());
 28             ExcelCell getD=new ExcelCell();getD.setValue(uBehavDto.getD());
 29             ExcelCell getE=new ExcelCell();getE.setValue(uBehavDto.getE());
 30             ExcelCell getF=new ExcelCell();getF.setValue(uBehavDto.getF());
 31             ExcelCell getAddTime=new ExcelCell();getAddTime.setValue(DateFormatUtils.format(uBehavDto.getAddTime(), pattern));
 32             ExcelCell getDate=new ExcelCell();getDate.setValue(DateFormatUtils.format(uBehavDto.getDate(), pattern));
 33             ExcelCell getG=new ExcelCell();getG.setValue(uBehavDto.getG());
 34             ExcelCell getH=new ExcelCell();getH.setValue(uBehavDto.getH());
 35             ExcelCell getI=new ExcelCell();getI.setValue(uBehavDto.getI());
 36             ExcelCell getJ=new ExcelCell();getJ.setValue(uBehavDto.getJ());
 37             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getK());
 38             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getL());
 39             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getM());
 40             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getN());
 41             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getO());
 42             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getP());
 43             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getQ());
 44             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getR());
 45             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getS());
 46             ExcelCell a=new ExcelCell();a.setValue(String.valueOf(uBehavDto.getT()));
 47             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getMemo());
 48             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getU());
 49             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getV());
 50             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getW());
 51             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getX());
 52             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getY());
 53             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getZ());
 54             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getAA());
 55             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getAB());
 56             ExcelCell a=new ExcelCell();a.setValue(uBehavDto.getAC());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值