Laravel 分块结果实用程序
Laravel Chunking Results utility
根据 Laravel 文档
If you need to work with thousands of database records, consider
using the chunk method. This method retrieves a small chunk of the
results at a time and feeds each chunk into a Closure for
processing. This method is very useful for writing Artisan commands
that process thousands of records. For example, let's work with the
entire users table in chunks of 100 records at a time:
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
foreach ($users as $user) {
//
}
});
是否应该减少等待时间?
我看不出这个功能有什么用
分块允许您将大查询和后续处理任务分解为重复的步骤。
此技术最常见的应用是避免达到内存限制。如果您从数据库中加载数十万行,然后对它们执行转换过程,这会占用大量内存,并且很容易达到 php.ini 中定义的限制。
根据上下文,您可以在每个块完成后立即将结果推送到屏幕,这意味着您减少了等待初始结果的时间,而整体处理时间可能会增加,而结果通知将以一个块的形式出现一次。
根据 Laravel 文档
If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing. This method is very useful for writing Artisan commands that process thousands of records. For example, let's work with the entire users table in chunks of 100 records at a time:
DB::table('users')->orderBy('id')->chunk(100, function ($users) {
foreach ($users as $user) {
//
}
});
是否应该减少等待时间?
我看不出这个功能有什么用
分块允许您将大查询和后续处理任务分解为重复的步骤。
此技术最常见的应用是避免达到内存限制。如果您从数据库中加载数十万行,然后对它们执行转换过程,这会占用大量内存,并且很容易达到 php.ini 中定义的限制。
根据上下文,您可以在每个块完成后立即将结果推送到屏幕,这意味着您减少了等待初始结果的时间,而整体处理时间可能会增加,而结果通知将以一个块的形式出现一次。