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;
    }
相关推荐
ladymorgana18 分钟前
【运维笔记】windows 11 中提示:无法成功完成操作,因为文件包含病毒或潜在的垃圾软件。
运维·windows·笔记
Hello.Reader1 小时前
Redis热点数据管理全解析:从MySQL同步到高效缓存的完整解决方案
redis·mysql·缓存
是程序喵呀1 小时前
MySQL备份
android·mysql·adb
指尖上跳动的旋律2 小时前
shell脚本定义特殊字符导致执行mysql文件错误的问题
数据库·mysql
一勺菠萝丶2 小时前
MongoDB 常用操作指南(Docker 环境下)
数据库·mongodb·docker
yngsqq3 小时前
一键打断线(根据相交点打断)——CAD c# 二次开发
windows·microsoft·c#
m0_748244833 小时前
StarRocks 排查单副本表
大数据·数据库·python
C++忠实粉丝3 小时前
Redis 介绍和安装
数据库·redis·缓存
wmd131643067123 小时前
将微信配置信息存到数据库并进行调用
数据库·微信
是阿建吖!3 小时前
【Linux】基础IO(磁盘文件)
linux·服务器·数据库