Laravel 使用 chunkById 遍历大表

发布时间: 2019-01-25 12:03:05 作者: 大象笔记

问题

现在需要定期遍历物流快递表,以更新物流的状态。但是,这个表在线上生产环境可能非常大,一次性取出来遍历可能爆掉服务器内存。

而使用 chunk 方法,会出现漏掉一半数据未处理的情况:

例如,有 A B C D E 五个物流快递单,每次 chunk 取出两条数据。

使用 chunkById

Laravel 5.5 文档中,并没有说明有 chunkById  这个方法;而 Laravel 5.7 中有专门的说明:

If you are updating database records while chunking results, your chunk results could change in unexpected ways. So, when updating records while chunking, it is always best to use the chunkById method instead. This method will automatically paginate the results based on the record's primary key:

DB::table('users')->where('active', false)
    ->chunkById(100, function ($users) {
        foreach ($users as $user) {
            DB::table('users')
                ->where('id', $user->id)
                ->update(['active' => true]);
        }
    });

但是,我在 Laravel 5.5 的代码里还是找到了对应的 chunkById 的实现。

grep chunkById -r vendor/laravel/
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:    public function chunkById($count, callable $callback, $column = null, $alias = null)
vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:    public function chunkById($count, callable $callback, $column = 'id', $alias = null)

        $lastId = 0;

        do {
            $clone = clone $this;

            $results = $clone->forPageAfterId($count, $lastId, $column)->get();

            $countResults = $results->count();

            if ($countResults == 0) {
                break;
            }

            if ($callback($results) === false) {
                return false;
            }

	// 记录了 last id,避免遗漏
            $lastId = $results->last()->{$alias};

            unset($results);
        } while ($countResults == $count);

参考

https://github.com/laravel/framework/issues/23277

我是一名山东烟台的开发者,联系作者