-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.1] Fix for chunk() #9681
[5.1] Fix for chunk() #9681
Conversation
Chunk is meant to override limits and offsets. The original issue you posted shouldn't even be using chunk. |
Okay, you can treat this PR as a proposal then. Why throwing away limits and offsets is any better than actually handling them like |
@taylorotwell Why is 'chunk is meant to override limits and offsets'? I can't see any reason why there is the necessity for an intentional overwrite. |
@taylorotwell I also can't see any reason why chunk shouldn't internally respect a limit and offset that has already been set. It's not clear that chunk is meant to override limits and offsets. It seems to me that rather it happens to override limits and offsets because of how it's implemented. I think it's a valid usecase to want to chunk through a limited or offset number of records. Say I want to do some processing on 40,000 records with 4 worker threads for jobs and I want them to do 10,000 records each in batches of 500, currently you need to calculate the limits and offsets yourself, set up a loop and call the queries incrementing the skip() and take() as you go. Chunk could handle this easily if I could run the following on each worker. -1 Model::take(10000)->chunk(500,..) I think the above lines are conceptually sound and very readable. |
^ +1 |
@taylorotwell ^ +1 |
^ +1 |
Any workaround for this? |
+1 we need this! |
Work around it to just count the number of loops
|
Here's an Eloquent workaround: Model::skip(100)->take(100)->get()->chunk(10)->each(function ($chunk) {
// chunk
}); This will not chunk the Queries, but it does allow you to chunk the results. |
At the point of $idModels = [..];
// where N is an integer representing desired number of elements in a subset.
$throughput = N;
$batchSets = array_chunk($idModels, $throughput);
unset($idModels);
$idModels = $batchSets[0];
$models = Model::find($idModels);
foreach ($batchSets as $setId => $idModels) {
$models = Model::find($idModels);
} |
Just was reading this, for future readers : none of the above workarounds are good, don't use them, they all have performance issues, if you want to do something like that just re-implement the chunk yourself with |
The same happened to me right now. I'd really love to have this. |
Closes #9649
This one is relatively hard to review. Sorry about that.
As you can see in the issue limits and offsets are completely ignored if
chunk()
is used instead ofget()
.This happens because chunk overrides those properties internally.
Although it's a rare usecase, in my opinion it's a bug and should be fixed in 5.1 therefore.