Skip to content

Conversation

@Jade-GG
Copy link
Collaborator

@Jade-GG Jade-GG commented Jan 5, 2026

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:

  • We don't eager load the entry data for everything, so when the retrieved event is fired for each model individually we also end up loading the data for each model individually.
  • The retrieved event is fired before any eager loading has been done.1

To 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 throwMissingAttributeExceptionIfApplicable function2 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

  1. See also: github.com/Eloquent emits event retrieved prior to finishing the entire retrieval process of eager loaded relations laravel/framework#29658

  2. Same function we use for custom attributes in Rapidez v5: https://github.com/rapidez/core/blob/master/src/Models/Traits/HasCustomAttributes.php#L202-L205

if ($key == 'subquery.relation_id') {
return $this->relation_id;
}
}
Copy link
Collaborator Author

@Jade-GG Jade-GG Jan 5, 2026

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.

Copy link
Member

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);

@royduin royduin requested a review from indykoning January 6, 2026 12:02
@royduin
Copy link
Member

royduin commented Jan 13, 2026

Please review @indykoning and @kevinmeijer97

->withoutGlobalScopes();
}

public function throwMissingAttributeExceptionIfApplicable($key)
Copy link
Member

@indykoning indykoning Jan 13, 2026

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()) {
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants