Skip to content

Prevent n+1 queries on asset meta exists #446

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

Merged
merged 3 commits into from
Jun 9, 2025

Conversation

ryanmitchell
Copy link
Contributor

Closes #445

->container($model->container)
->path(Str::replace('//', '/', $model->folder.'/'.$model->basename))
->hydrateMeta($model->meta)
->syncOriginal();

Blink::put('asset-meta-'.$asset->id(), $model->meta);
Copy link

@Jubeki Jubeki Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Blink::put('asset-meta-'.$asset->id(), $model->meta);
Blink::put('eloquent-asset-'.$asset->id(), $model->meta);

It works with the above change (you used a different key in metaExists).

You could also use Blink::put('eloquent-asset-meta-exists-'.$asset->id(), true) and remove the additional Blink::has('eloquent-asset-'.$this->id()) in metaExists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - not quite but I've changed it around some more.
We need to cache both the model and the meta (core caches the meta so we need to do the same)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Blink::put('asset-meta-'.$asset->id(), $model->meta);
Blink::put($asset->metaCacheKey(), $model->meta);

metaCacheKey is a public method of Assets, so you could use this in a unified way:
https://github.com/statamic/cms/blob/eba307923eb5fe9ce7b0954bf6ceecc0357be8c1/src/Assets/Asset.php#L324-L327

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes thanks. Can you confirm you are still seeing a reduction in queries with the latest changes?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the reduction still works with the latest changes.

@ryanmitchell ryanmitchell merged commit f578871 into statamic:4.x Jun 9, 2025
19 checks passed
@ryanmitchell ryanmitchell deleted the fix/reduce-asset-queries branch June 9, 2025 17:35
@macaws
Copy link

macaws commented Jun 10, 2025

Moving my comment here:

Something in this change breaks sites that are running custom models for Assets @ryanmitchell - when I get more time I can try to figure out what.

We got:

  • A 5xx error at first because we didn't have the AssetContract methods on our model (oops)
  • After fixing that, half the assets no longer load - seemingly randomly (some do, some don't)

In the meantime, we're downgrading to 4.23.4 - if I can figure out what breaks here I'll create issue or PR.

@ryanmitchell
Copy link
Contributor Author

@macaws that’s surprising - nothing here seems like it should break that.

If you are able to share your repo I can take a look into it. Or if you are able to provide a test that shows the breakage so I can understand the issue then I can investigate further.

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.

(n+1) performance issue using assets
3 participants