Skip to content
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

Entries Fieldtype: Empty field triggers queries across all collections instead of configured scope #11152

Open
yurichudaev opened this issue Nov 21, 2024 · 13 comments · May be fixed by #11184
Open

Comments

@yurichudaev
Copy link

Bug description

When using fieldtype entries with an empty field value, the system queries ALL collections instead of limiting the scope to the configured collection.

Current behavior:

  1. With attached entry: Debugbar shows specific query for selected entry only
    image
  2. With empty field: Debugbar shows queries across ALL collections, potentially impacting performance
    image

Expected behavior:
Queries should be limited to the configured collection scope even when the field is empty.

Key Question:
Does this "empty field" scenario trigger a full reindexing process across all collections in the flat-file system? If so, what are the performance implications and recommended approaches to handle this?

Looking forward to your insights on this behavior.

Thanks, Yuri

How to reproduce

  1. Create a new site using statamic-cli
  2. Add fieldtype entries field to the 'pages' collection
  3. Configure the field to accept entries from 'posts' collection
  4. View the entry with empty field in Control Panel
  5. Check Debugbar queries

Logs

No response

Environment

- Statamic Version: 5.38.1
- PHP Version: 8.3.8
- Installation: Fresh statamic/statamic site via CLI
- Laragon

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

@duncanmcclean duncanmcclean changed the title [Fieldtype Entries] Empty field triggers queries across all collections instead of configured scope Entries Fieldtype: Empty field triggers queries across all collections instead of configured scope Nov 21, 2024
@jasonvarga
Copy link
Member

You're right it's an issue.

I've thought about having a fake query builder for instances like these where we know there will be zero results.

Since we know your field is empty, there's zero reason for us to perform a query at all. The only reason we do is so you consistently get a query builder instance when using the field on the frontend.

@yurichudaev
Copy link
Author

Thanks for the clarification about the fake query builder. I have a follow-up question regarding Stache behavior:

Since Stache watches file modifications and builds indexes, could this empty field issue be causing unnecessary full index regeneration on initial page load after cache clear when page contains empty field?

Specifically, I'm concerned that when the system queries ALL collections (due to empty field), it might trigger Stache to:

  1. Scan all content files
  2. Build indexes for all collections
  3. Cache more data than necessary

This is particularly critical for our production site with approximately 4-5 thousand entries. I'm observing significant performance impact when opening a page with such field, as it seems to cause complete index regeneration instead of selective updates.

What's also puzzling is that running 'php please stache:warm' doesn't seem to resolve this issue. Shouldn't this CLI command prepare all necessary indexes upfront and prevent such regeneration on page load?

Is this assumption correct about the indexing behavior? If so, would the proposed fake query builder solution prevent this excessive Stache indexing, and why doesn't the warm command help in this case?

@jasonvarga
Copy link
Member

could this empty field issue be causing unnecessary full index regeneration

Yes. Any fields involved in the query would cause its index to be generated if it isn't already.

What's also puzzling is that running 'php please stache:warm' doesn't seem to resolve this issue. Shouldn't this CLI command prepare all necessary indexes upfront and prevent such regeneration on page load?

Not every field has an index generated in here. Just known ones like slug, uri, title, etc. You can add more in config/statamic/stache.php, though.

https://statamic.dev/stache#configuring-additional-indexes

If so, would the proposed fake query builder solution prevent this excessive Stache indexing

The idea would be that a query doesn't actually happen, so yes it should bypass any actual work.

@yurichudaev
Copy link
Author

Not every field has an index generated in here. Just known ones like slug, uri, title, etc. You can add more in config/statamic/stache.php, though.

https://statamic.dev/stache#configuring-additional-indexes

Thanks for the explanation. One more clarification about indexing configuration:

I've configured additional indexes in config/statamic/stache.php, but they seem to be created for ALL collections. Is there a way to configure field indexes for specific collections only?
image
image

For example, if I have a 'city' field that's only used in 'locations' collection, can I limit its index generation to just that collection instead of indexing this field across all collections?

This would help optimize the indexing process since not all fields are used in every collection.

@jasonvarga
Copy link
Member

No, at the moment they are across all collections, but they could probably be made more granular.

@yurichudaev
Copy link
Author

Thank you for all the detailed explanations! This really helps to understand the Stache behavior better.

Since both issues (empty field queries and collection-wide indexing) can significantly impact performance on larger sites, is there any timeline for implementing these improvements? Specifically:

  1. The fake query builder solution for empty fields
  2. More granular collection-specific indexing

Would be great to know if these are planned for any upcoming releases.

@jasonvarga
Copy link
Member

Nothing is planned yet since both issues were only brought up today. 😄

@yurichudaev
Copy link
Author

yurichudaev commented Nov 22, 2024

I'd like to ask one more question regarding Stache index generation in Statamic.
Am I understanding correctly that if there's a link in the navigation to one of the entries from a blog collection that contains 3000 entries, it will trigger index generation for the entire blog collection? In other words, will it generate indexes for all 3000 entries on first page load?

image

@jasonvarga
Copy link
Member

Yes

@yurichudaev
Copy link
Author

Thank you for the explanations, I'll be waiting for updates

@Russellsoad
Copy link

Hi, I also experience such problems on my projects, please tell me when to wait for the update ?

@yurichudaev
Copy link
Author

@jasonvarga Hi, are there any updates on this issue?

@ryanmitchell
Copy link
Contributor

@chprof #11184

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

Successfully merging a pull request may close this issue.

5 participants