-
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
| if ($key == 'subquery.relation_id') { | ||
| return $this->relation_id; | ||
| } | ||
| } |
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.
Because our relation uses a subquery, we need to work around how getRelatedKeyFrom works (this will try to access $model->{'subquery.relation_id'}). If we get rid of the subquery., it will try to qualify the column and throw an error from SQL because then it can't find statamic_entries.relation_id.
All in all, it's a bit of a mess only because Laravel doesn't allow raw statements as foreign keys. However, we can do this hack here to work around this issue.
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.
Shouldn't we fall back to
parent::throwMissingAttributeExceptionIfApplicable($key);|
Please review @indykoning and @kevinmeijer97 |
| ->withoutGlobalScopes(); | ||
| } | ||
|
|
||
| public function throwMissingAttributeExceptionIfApplicable($key) |
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.
| 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?
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 ↩