MySQL调优--11--分批插入/更新数据 ---案例

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 案例1--Lists.partition
  • [案例2 --while 循环](#案例2 --while 循环)
      • [while (true){](#while (true){)
      • [while (!CollectionUtils.isEmpty(detailList)) {](#while (!CollectionUtils.isEmpty(detailList)) {)
      • }
      • [while (true) 配合分页插件](#while (true) 配合分页插件)
  • [案例3-- do while](#案例3-- do while)
      • [do{ } while (!CollectionUtils.isEmpty());](#do{ } while (!CollectionUtils.isEmpty());)
      • do{
      • [if (CollectionUtils.isEmpty())) {](#if (CollectionUtils.isEmpty())) {)
      • break;
      • }
      • [} while ()](#} while ())

案例1--Lists.partition

Lists.partition()
java 复制代码
    private void updateStatus(List<WsapModel> wsapModelList) {
          if (CollectionUtils.isEmpty(wsapModelList)) {
               return;
          }

        try {
            List<List<WsapModel>> partList = Lists.partition(wsapModelList, 1000);
            for (int i = 0; i < partList.size(); i++) {
                List<WsapModel> list = partList.get(i);
                List<Long> idlist = list.stream().map(WsapModel::getId).collect(Collectors.toList());
                personalSubscribeWsapMapper.updateStatus(idlist, LocalDateTime.now());
            }
        } catch (Exception e) {
            logger.error("更新t_personal_subscribe_wsap status为1 异常:{} >>{}", DateUtils.getNow(),
                    e.getMessage());
        }
    }

案例2 --while 循环

while (true){

}

java 复制代码
        while (true) {
            try {
                goodsMediaDetailExportVOList = getGoodsMediaDetailExportVOList(goodsMediaDetailRepDTO, goodsMediaVO, goodsMediaDetailListApiRes);
                log.info("查询物媒视图参数:{},条数:{}", goodsMediaDetailRepDTO, goodsMediaDetailExportVOList.size());            
                if (CollUtil.isNotEmpty(goodsMediaDetailExportVOList)) {
                    totalRow = totalRow + goodsMediaDetailExportVOList.size();
                    listLists = getExportData(goodsMediaDetailExportVOList);
                    if (totalRow % fileRowSize == 0) {
                        fileIndex++;
                        fileName = "物媒明细数据报表" + fileIndex + ".csv";
                        path = syncExportDTO.getExportFilePath(downLoadFolder) + fileName;
                        fileNames.add(path);
                        ExportUtil.writeCsvFile(fileName, headerList, path, listLists, false);
                    } else {
                        ExportUtil.writeCsvFile(fileName, new ArrayList<>(), path, listLists, true);
                    }
                } else {
                    break;
                }
            } catch (Exception e) {
                log.error("物媒明细数据报表异常", e);
            }
        }

while (!CollectionUtils.isEmpty(detailList)) {

}

java 复制代码
    private final Integer BATCH_SIZE = 1000;

        queryWrapper.last("limit " + BATCH_SIZE);
        List<ActivityListApplyDetailModel> detailList = activityListApplyDetailMapper.selectList(queryWrapper);
        int finallyTransferCount = 0;
        while (!CollectionUtils.isEmpty(detailList)) {
            // 将数据插入到迁移表
            activityListApplyDetailMapper.newInsertBatchTransfer(tableName, detailList);
            // 删除已迁移的数据
            List<Long> detailIdList = detailList.stream().map(ActivityListApplyDetailModel::getId).collect(Collectors.toList());
            activityListApplyDetailMapper.deleteBatchIds(detailIdList);
            finallyTransferCount += detailList.size();
            log.info("报名明细迁移已完成迁移量:{}", finallyTransferCount);
            detailList = activityListApplyDetailMapper.selectList(queryWrapper);
        }

while (true) 配合分页插件

java 复制代码
            int pageNum = 1, pageSize = 1000;
            long count = 0;
            while (true) {
                pageQuery.setPageNum(pageNum);
                pageQuery.setPageSize(pageSize);

                List<GoodsMediaModel> goodsMediaModelList = PageHelperUtil.listByPage(() -> goodsMediaMapper.selectList(queryWrapper), pageQuery).getLists();
                // 统计总数
                count += goodsMediaModelList.size();

                // 写入文件
                FileLineHelper.appendWrite(file, parseToString(goodsMediaModelList, specifyDate));

                if (goodsMediaModelList.size() < pageSize)
                    break;
                else
                    pageNum++;
            }

案例3-- do while

do{ } while (!CollectionUtils.isEmpty());

java 复制代码
   int pageNo = 1;
   int pageSize = 1000;
   PageBean<PersonalMediaDetailRespDTO> pageBean = null;

         do {
                 // ck查询客户明细
                try {
                    ApiResponse<PageBean<PersonalMediaDetailRespDTO>> pageBeanApiResponse = viewFeign.selectPersonalMediaViewDetailList(viewDetailRequestDTO);
                    pageBean = pageBeanApiResponse.getData();
                    if (pageBean == null || CollectionUtils.isEmpty(pageBean.getLists())) {
                        if (pageNo == 1) {
                            log.warn("【人媒明细报表导出】查询浏览事件为空");
                            List<List<Object>> dataList = Lists.newArrayList();
                            List<Object> data = Lists.newArrayList("未查找到符合条件的数据,请修改查询条件");
                            dataList.add(data);
                            ExportUtil.writeCsvFile(fileName, null, path, dataList, true);
                        }
                        break;
                    }
                } catch (Exception e) {
                    log.error("【人媒明细报表导出】,统计view汇总,请求参数:{}, 错误信息:", JSONObject.toJSONString(viewDetailRequestDTO), e);
                    break;
                }
                List<PersonalMediaDetailRespDTO> viewDetailRespDTOList = pageBean.getLists();
                
                pageNo++;
                viewDetailRequestDTO.setPageNum(pageNo);
                
            } while (!CollectionUtils.isEmpty(pageBean.getLists()));

do{

if (CollectionUtils.isEmpty())) {

break;

}

} while ()

java 复制代码
    @Override
    public List<ChannelOrgDTO> selectActiveChannelList(String date) {
        List<ChannelOrgDTO> result = Lists.newArrayList();
        long pageCount;
        int pageNum = 0;
        PageQuery pageQuery = new PageQuery();
        pageQuery.setPageSize(5000);
        do {
            pageNum++;
            pageQuery.setPageNum(pageNum);
            PageBean<ChannelOrgDTO> page =
                    PageHelperUtil.listByPage(() -> channelMapper.selectActiveChannelList(date), pageQuery);
            if (CollectionUtils.isEmpty(page.getLists())) {
                break;
            }
            result.addAll(page.getLists());
            pageCount = page.getPageCount();
        } while (pageCount > pageNum);
        return result;
    }
相关推荐
零炻大礼包27 分钟前
【SQL server】数据库远程连接配置
数据库
zmgst36 分钟前
canal1.1.7使用canal-adapter进行mysql同步数据
java·数据库·mysql
令狐少侠201137 分钟前
explain执行计划分析 ref_
mysql
随心............38 分钟前
python操作MySQL以及SQL综合案例
数据库·mysql
€☞扫地僧☜€39 分钟前
docker 拉取MySQL8.0镜像以及安装
运维·数据库·docker·容器
CopyDragon43 分钟前
设置域名跨越访问
数据库·sqlite
xjjeffery44 分钟前
MySQL 基础
数据库·mysql
写bug的小屁孩1 小时前
前后端交互接口(三)
运维·服务器·数据库·windows·用户界面·qt6.3
恒辉信达1 小时前
hhdb数据库介绍(8-4)
服务器·数据库·mysql
齐 飞2 小时前
MongoDB笔记01-概念与安装
前端·数据库·笔记·后端·mongodb