Optimize queries for Components listing#18251
Merged
snipe merged 1 commit intogrokability:developfrom Nov 26, 2025
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
A customer reported that listing large numbers of components seemed to cause error 500's from the API endpoint. Taking a quick look through debugbar, it seems like we were trying to load every asset associated with every component on the page, which I can easily imagine exhausting memory.
This change removes those
with()entries, and uses thewithSum()Eloquent method to make the database dynamically calculate those counts for us, and modifies thenumCheckedOut()method to try to use that value if it exists - or to dynamically calculate it if it doesn't. And that dynamic calculation had been working on the 'Collection' object, rather than the database, so this adds the()to the relationship to make it a DB-level calculation rather than loading up all of the related objects and summing up through that collection.The challenge here is that, because that value is now cached, if you were doing something with components and checking the value of that
numCheckedOut()value when it should have changed, it will stay the same due to the caching.I looked through the code and it doesn't look like we do that anywhere. Still, just in case, I added a new optional parameter to
numCheckedOut()- which defaults tofalse- that will force it to recalculate the result and save that to the cache.Another thing I did was reduce the number of times we called
components->count()by throwing the result into a variable.Tests
Tests do pass, and I did a make sure that the components index page loads correctly and displays the right values.