[7.x] Significant performance issue in Eloquent Collection loadCount() method #34177
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It appears as though the performance of
\Illuminate\Database\Eloquent\Collection::loadCount()
has been significantly impacted by this change 5ee0e4dI was debugging a performance issue in one of our applications and noticed that a call to
loadCount()
was taking minutes. I profiled with Blackfire.io and saw hundreds of thousands of calls to thegetAttribute()
method on the Eloquent Model.I've put together a repository to validate a performance fix: https://github.com/markokeeffe/laravel-load-count-performance
You can follow the steps in
README.md
to validate the performance impact.The repo essentially seeds 500
Item
models, each with 1000 relatedItemLog
models. Then two console commands can be run to see the performance difference between the current method and the proposed fix in this PR:load-count-laraval
load-count-improved
Check this screenshot from Blackfire.io to see how calling
loadCount()
on a collection of 500 models results in 501,000 calls togetAttribute()
Here's a screenshot of the Blackfire.io profile after the fix is applied: