当前位置: 首页 > ai >正文

laravel chunkById导出数据乱序问题

2025年7月28日17:47:29

这几天在做数据导出优化,使用xlswriter作为导出组件,但是发现在 使用

$base->chunkById(2000, function ($list) use ($writer, $sheet1) {

发现导出的数据是乱的,偶尔有些重复,偶尔有些少了,很奇怪,把数据打印出来的时候,发现模型的主表的id是乱序的
查看了一下chunkById的代码

public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id'){$this->orders = $this->removeExistingOrdersFor($column);if (! is_null($lastId)) {$this->where($column, '>', $lastId);}return $this->orderBy($column, 'asc')->limit($perPage);}

这里用的是id的 > 所以说,主表的id是 主键,但是返回的id是乱序,导出的数据就是乱的,
这里使用chunk方法是使用forpage

public function chunk($count, callable $callback){$this->enforceOrderBy();$page = 1;do {// We'll execute the query for the given page and get the results. If there are// no results we can just break and return from here. When there are results// we will call the callback with the current chunk of these results here.$results = $this->forPage($page, $count)->get();$countResults = $results->count();if ($countResults == 0) {break;}// On each chunk result set, we will pass them to the callback and then let the// developer take care of everything within the callback, which allows us to// keep the memory low for spinning through large result sets for working.if ($callback($results, $page) === false) {return false;}unset($results);$page++;} while ($countResults == $count);return true;}

所以可以避免导出数据混乱的问题,这里使用 chunkById 要注意主表的使用有序的主键

http://www.xdnf.cn/news/16416.html

相关文章:

  • Open CV图像基本操作可莉版
  • 学习游戏制作记录(改进剑投掷状态)7.28
  • Leetcode_349.两个数组的交集
  • SPI通信协议
  • 分布式渲染效能探析:关键网络性能要素
  • 机器学习基础-matplotlib
  • linux系统----Ansible中的playbook简单应用
  • 计算机视觉---Halcon概览
  • 【算法训练营Day17】二叉树part7
  • 【学习路线】Android开发2025:从入门到高级架构师
  • linux命令grep的实际应用
  • MySQL 与 PostgreSQL 对比
  • Redis四种GetShell方式完整教程
  • 【WRF-Chem第二期】WRF-Chem有关 namelist 详解
  • 1.DRF 环境安装与配置
  • 零基础学习性能测试第九章:全链路追踪-系统中间件节点监控
  • LeetCode 刷题【18. 四数之和】
  • 计算机网络编程-Socket通信以及实战
  • 企业级JWT验证最佳方案:StringUtils.hasText()
  • 使用Ettus USRP X440对雷达和EW系统进行原型验证
  • 分布式IO选型指南:2025年分布式无线远程IO品牌及采集控制方案详解
  • Python动态规划:从基础到高阶优化的全面指南(2)
  • socketpair函数详解
  • Qt小组件 - 8 图片浏览器
  • go‑cdc‑chunkers:用 CDC 实现智能分块 强力去重
  • SpringBoot轻松集成豆包AI
  • 精简 RT-Thread 工程并查找占用空间大的模块
  • 中国汽车能源消耗量(2010-2024年)
  • java里List链式编程
  • 【智慧物联网平台】安装部署教程——仙盟创梦IDE