-
Notifications
You must be signed in to change notification settings - Fork 95
Fix outrageous query counts when using Eloquent driver and querying terms on entries #517
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
Fix outrageous query counts when using Eloquent driver and querying terms on entries #517
Conversation
|
My bad - fixing those test failures now. |
|
Thanks for this PR and all the work on it, it's definitely something I'm keen to solve as well. The question for me is whether we solve it in this driver or we wait for core to handle pivots - I want to avoid us choosing an approach that makes switching later more difficult. I'm going to leave this for @jasonvarga to make a call on when he has time. |
Well whatever you guys decide, I've fixed regressions in existing tests and added a couple of extras for the work I've carried out. Also ran Pint on all the files I changed so there should be no checks failing now. Looking forward to seeing what you guys do with this - it's been a major bug-bear for us. :) |
|
We're also experiencing this issue, which has actually caused our comwell.com site to crash due to the database being overloaded, so this is the highest priority for us right now. |
|
You can patch this PR in or even use the code here in your own repositories in the mean time - the driver makes these kind of changes quite configurable. |
|
@Michael-Stokoe I've attempted to implement this patch into my application. I've found the issue in our code when calling the value method on a Statamic\Fields\Value |
Are you able to provide a code example so I can test and implement a fix in my PR? |
|
We call the following But you can just run tinker and call \Statamic\Facades\Term::query() and see the exception. |
|
Awesome. I'll take a look as soon as I'm back at my laptop :) |
|
Would it be possible to have terms repository use Stache as if it was a file driver, but instead pull all terms from the database? |


Problem
When using collection tags in Antlers templates, entries with taxonomy fields triggered individual database queries for each taxonomy term, causing major N+1 query performance issues.
Solution
Implemented proper Eloquent relationships between entries and taxonomy terms using a pivot table, enabling eager loading to eliminate N+1 queries.
Performance Impact
Changes
entry_termpivot table for entry-taxonomy relationshipsSyncTaxonomyRelationshipscommand for existing data migrationMigration
Backward Compatibility
✅ Existing templates work unchanged
✅ API responses remain identical
✅ No breaking changes
Before and afters captured on a local site with real, live data
Before:

After:

Before:

After:
