Laravel中chunk方法分块处理数据
我只说从今往后,不说今日以前。Let’s imagine the situation: you have a big database table (like 10 000 rows or bigger) and you need to run an update to one column. But you cannot run just SQL query – there is some PHP logic behind it. So foreach loop could potentially take forever or bump into a default 30-second script limit. Luckily, Laravel has a neat solution for it.
Let’s look at a simple example of a problem I’m talking about:
1 | $users = User::all(); |
Do you see the problem? It’s fine for a DB table with 100 or 500 users. But what about 10000? 100000? Million?
It’s not only about the speed, but also about storing the data – you are potentially storing the whole database table into variables, you might run out of memory.
So here’s the thing: there is a function called chunk() which splits all data into separate selects, like pagination.
Let’s look at an example:
1 | User::chunk(100, function ($users) { |