-
Notifications
You must be signed in to change notification settings - Fork 6
Fix N+1 query by using relation data #179
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
base: master
Are you sure you want to change the base?
Conversation
|
Please review @indykoning and @kevinmeijer97 |
| protected function entry(): Attribute | ||
| public function entry(): BelongsTo | ||
| { | ||
| if (!app()->runningInConsole()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure this can go?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume this was for making sure no database queries get done when doing commands in pipelines right? I don't see where this would be a problem in this case with it being a relation now.
ref: GT-2423
We ran into an issue with a massive N+1 query on the overview page of a runway resource. This was even more apparent when showing 500 results per page.
The reason for this is twofold:
retrievedevent is fired for each model individually we also end up loading the data for each model individually.retrievedevent is fired before any eager loading has been done.1To fix this, I've turned the entry attribute into a relation that gets eager loaded, and I've also deferred the actual retrieving of the data from this relation to the
throwMissingAttributeExceptionIfApplicablefunction2 so that we only start querying this data when the relation is loaded.Note that the relation is a bit hacky because we need to get the related key from the data json. Laravel doesn't allow for raw statements to be used as foreign keys, so this uses a table joined onto itself to add the variable from the json column as a regular column, which we can then use as regular foreign key.
Footnotes
See also: github.com/Eloquent emits event retrieved prior to finishing the entire retrieval process of eager loaded relations laravel/framework#29658 ↩
Same function we use for custom attributes in Rapidez v5: https://github.com/rapidez/core/blob/master/src/Models/Traits/HasCustomAttributes.php#L202-L205 ↩