eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

Duplicated Queries all the way

Open MazzMazz opened this issue 1 year ago β€’ 11 comments

I have a Landingpage with 4 Entries - nothing else. A Author is linked, also a Category and Image.

The problem I am facing is that a looooot of queries are executed, and i have no clue where to fix this, include a Cache or smth. I need a hint or something - there are 158 Queries for 4 Blogposts.

158 statements were executed, 130 of which were duplicates, 28 unique.
image image

MazzMazz avatar Mar 28 '24 16:03 MazzMazz

Can you please paste the contents of php please support:details ?

Are you using an entries field type?

ryanmitchell avatar Mar 28 '24 16:03 ryanmitchell

php please support:details

Environment
Application Name: localtest.de
Laravel Version: 10.45.1
PHP Version: 8.1.27
Composer Version: 2.5.5
Environment: debug
Debug Mode: ENABLED
URL: localtest.de.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 5
Antlers: runtime
Sites: 1
Stache Watcher: Disabled
Static Caching: Disabled
Version: 4.55.0 Solo

Statamic Addons
statamic/eloquent-driver: 3.1.2
studio1902/statamic-peak-browser-appearance: 3.3.4
studio1902/statamic-peak-commands: 4.4.0
studio1902/statamic-peak-seo: 8.2.0
studio1902/statamic-peak-tools: 4.4.2

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: file
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: file
Navigations: file
Revisions: eloquent
Taxonomies: eloquent
Terms: eloquent

Yess the Author is a Entries Field Type.

MazzMazz avatar Mar 28 '24 16:03 MazzMazz

Yep, so you're seeing the result of the queries from augmentation on the entries field type (I believe): https://github.com/statamic/cms/blob/08512240a30112a0dcf658385d404caad4d6cf0a/src/Fieldtypes/Entries.php#L329-L340

I'm hopeful some of the performance improvements coming in v5 of core will reduce the duplicate queries.

ryanmitchell avatar Mar 28 '24 16:03 ryanmitchell

Yes for the Entries this might be true - at least the Fieltypes/Entries in in the Backtrace image

However would this be the same for e.g. Assets and Taxonomies image

image

MazzMazz avatar Mar 28 '24 16:03 MazzMazz

Yes, the field types all work the same way and currently don't cache their query results.

ryanmitchell avatar Mar 28 '24 16:03 ryanmitchell

Allrighty - thank you for the explanation. Any known workaround or just wait for Statamic5? πŸ˜„

MazzMazz avatar Mar 28 '24 16:03 MazzMazz

Given how close v5 is I'd suggest waiting it out for the next couple of weeks.

ryanmitchell avatar Mar 28 '24 16:03 ryanmitchell

Allrighty - thank you. Much apprechiated πŸ™πŸ»

MazzMazz avatar Mar 28 '24 17:03 MazzMazz

Has this issue been fixed with Statamic v5? I'm still seeing hundreds of queries for each page including duplicate assets_meta queries like this one repeated 7 times:

select * from `assets_meta` where `container` = '{\"id\":\"assets\",\"title\":\"Assets\",\"handle\":\"assets\",\"disk\":\"s3\",\"blueprint\":{\"title\":\"Asset\",\"handle\":\"assets\"},\"search_index\":null,\"api_url\":null}' and `path` in ('my-filename.jpg')
-- EloquentQueryBuilder.php#58
Asset Containers: file
Assets: eloquent

clementmas avatar Jul 04 '24 08:07 clementmas

No it hasn’t. How the asset field would need to be changed in core - we can’t fix it on the driver side.

ryanmitchell avatar Jul 04 '24 08:07 ryanmitchell

@clementmas i took a quick look at Statamic5 for my project when it was released and i also still have a looot of duplicate queries. I even thought the site was slower after the upgrade πŸ˜… So it's currently still running on Statamic4.

Since my job is quite demanding currently I wasn't able to check if it may be a config problem on my side from the upgrade or investigate further/give more details about the problem/recreate or replicate the issue in a new Repo for the devs.

@ryanmitchell thanks for the update πŸ™πŸ»

MazzMazz avatar Jul 04 '24 08:07 MazzMazz

I've chatted this through a bit both on the core side and the antlers side and the solution for now is to assign the value to a variable eg

{{ $my_field_cached = my_field }}

{{ $my_field_cached }}
   // do stuff
{{ /$my_field_cached }}

this will ensure the parser gets and caches the value.

As its already blinking the result of max_items: 1 fields (see PR) this only is needed for fields that aren't max_items: 1

ryanmitchell avatar Aug 06 '24 17:08 ryanmitchell