惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
J
Java Code Geeks
U
Unit 42
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
L
LangChain Blog
D
Docker
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
I
InfoQ
The Cloudflare Blog
小众软件
小众软件
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
V
V2EX
月光博客
月光博客
Martin Fowler
Martin Fowler

博客园 - zh7314

Go 1.23 → 1.26 升级检查文档 Pimcore 版本功能对比表(Community / Professional / Enterprise) typephp的编译核心phpx原理解析 typephp的编译原理 golang多版本控制工具支持Linux/macOS/Windows dbx低性能消耗的支持window/mac/docker的数据库客户端 关于headless和低代码平台的一些思考 docker日志监控dozzle,高性能,性能消耗小 pimcore 12版本的命令 阿里云 nas服务挂了导致docker挂载目录不存,docker无法重启和停止容器 symfony+swoole性能测试 关于小米手机使用 microsoft authenticator 无法扫码成功的解决方案 fastapi的构建cms,推荐fastapi集成框架 s3rius/FastAPI-template python 现代化包管理工具uv安装和使用 博文阅读密码验证 - 博客园 laravel在cli模式下输出格式漂亮一些 laravel计划任务和异步队列任务,拆分成不同队列,减少计划任务系统压力 debug redis里面的lua脚本 PHP 真正异步编程即将到来 首个 alpha 版本已经发布 (转) 关于his esb企业服务总线系统设计 多渠道订单,对应渠道公司的提现主体,如何设计系统 laravel 主表和从表一对一,从表是要多个选取最新的一条,性能优化 sourcetree无法获取远程所有的tag 支付宝沙盒模式商家转账经常出现 响应异常: 解包错误 laravel 使用异步队列,context带的上下文造成反序列化出问题 hyperf 的默认创建项目部署k8s docker缺失文件 在laravel使用注解自动注册 hyperf 查看所有路由的命令 线上就医全流程医药机构接入文档接口代码-医保就医接口php-demo版本 windows lm studio 0.3.8无法下载模型,更换镜像
laravel chunkById导出数据乱序问题
zh7314 · 2025-07-28 · via 博客园 - zh7314

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 要注意主表的使用有序的主键