Laravel无法获得超过15k的记录

问题描述 投票:3回答:1

我有一个非常简单的查询来从数据库中获取记录:

\DB::table("table")->get();

当我尝试从数据库获得超过±145000条记录时,我得到:500 server error

代码如:

\DB::table("table")->take(14500)->get();

虽然有效。当我试图获得超过15k时,我立即得到错误,没有任何加载或进一步的信息。我也无法从日志中获取更多信息。奇怪的是,当我编写代码来修补时 - 我可以获得所有记录。 (雄辩的作品相同)

php laravel postgresql laravel-5.1
1个回答
4
投票

如果您检查错误日志,您很可能会看到以下内容:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 54 bytes)

chunk你的结果会更好,而不是一次性加载到内存中

\DB::table("table")->chunk(500, function($results) {
    foreach($results as $result) {
       do your thing
    }    
});
© www.soinside.com 2019 - 2024. All rights reserved.