craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.2k stars 617 forks source link

[4.x]: customField $contentService->setFieldContext ignored? #13992

Closed aloco closed 6 months ago

aloco commented 7 months ago

What happened?

Description

We have a rather big install with some big formie forms, therefore a lot of custom fields in the table (1622). When making any web request, there is a call to customFields https://github.com/craftcms/cms/blob/4.5/src/elements/db/ElementQuery.php#L2110-L2119 which takes almost half a second to return and consumes a lot of CPU. The result contains all custom fields, including formie and matrix related custom fields. I ruled out that the query is slow, but was wondering, because the query, doesn´t filter global which is set as fieldContext. Is this what it should do? This happens on frontend requests and also in CP, no matter which page. As far as I understand from the callstack, this is initiated by checking the current User

Currently our site is relative slow because of that - do you know how to work around that issue besides not using big formie forms adding a lot of custom fields?

Screenshot 2023-12-05 at 15 03 08 Screenshot 2023-12-05 at 15 06 53

Steps to reproduce

  1. Create a lot of custom fields (>1500)
  2. Check response time in profiler

Expected behavior

Load only required custom fields, less CPU usage

Actual behavior

Long response times and excessive CPU usage

Craft CMS version

4.5.11.1

PHP version

8.1.21

Operating system and version

Linux 5.15.107+

Database type and version

MariaDB 10.5.23

Image driver and version

Imagick 3.7.0 (ImageMagick 7.1.0-50)

Installed plugins and versions

brandonkelly commented 6 months ago

It’s expected that all fields are queried up front, however it wasn’t necessary to be actually instantiating all of them right away.

I’ve just made a change so fields are only instantiated the first time they are actually needed for something. Until then they only will exist as the raw config data pulled from the database, which should speed things up for you.

If you’d like to test it before the next release, you can change your craftcms/cms requirement in composer.json to dev-develop as 4.5.11.1, and run composer update.

aloco commented 6 months ago

Hi,

thank you! This helped!

brandonkelly commented 6 months ago

Thanks for testing :)

brandonkelly commented 6 months ago

Craft 4.5.12 is out with that change!