gpt4 book ai didi

用于海量数据的 Laravel 惰性集合

转载 作者:行者123 更新时间:2023-12-01 21:52:40 25 4
gpt4 key购买 nike

我正在从表中查询一个大型数据集,然后通过循环迭代创建一个 json 文件。

$user = App\User::all();

foreach($user as $val){
// logic goes here for creating the json file
}

现在我面临的问题是,当遍历循环时它正在消耗内存并且我收到错误“允许的内存大小耗尽”。而且服务器的 cpu 使用率变得如此之高。我的问题是我应该如何使用 laravel 惰性集合来解决这个问题。我已经阅读了官方文档,但找不到方法。

最佳答案

只需将 all 方法替换为 cursor 方法即可。

$user = App\User::cursor();

foreach($user as $val){
// logic goes here for creating the json file
}

有关可链接的方法的更多信息,请参阅官方 documentation

关于用于海量数据的 Laravel 惰性集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58948807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com