craft-dynamic-fields icon indicating copy to clipboard operation
craft-dynamic-fields copied to clipboard

Performance issue with lots of dynamic fields

Open jmalley opened this issue 6 years ago • 8 comments

I have a matrix with a dynamic dropdown that pulls a large number of globals. It works fine until the user adds a lot of matrix blocks that all have this field in it, at which point the page is slow to load and times out frequently. It's going to the database a hundred times, presumably. Here's my code:

{ "value":"none" , "label":"N/A" },
{% for block in eventHost.eventLocations.order('locName').limit(null) %}
    { "value":"{{ block.locName }}" , "label":"{{ block.locName }}" }
    {% if not loop.last %},{% endif %}
{% endfor %}

What would be the best strategy for only loading this dropdown dynamically for the first block, and then displaying a cached or copied version of the dropdown for all the subsequent blocks. Is this even possible? Am I thinking about this problem in the right way?

jmalley avatar Jul 16 '18 16:07 jmalley

@jmalley Did you fix this issue ?

alexgilbertDG avatar Nov 30 '18 17:11 alexgilbertDG

I don't believe so. I gave up and jacked up the memory craft is allowed to use. It's still slow, but I've gotten no timeout reports from the users at least. I'm also using an older version of this plugin with craft 2 so there may be another solution for the current plugin version.

jmalley avatar Nov 30 '18 18:11 jmalley

@jmalley Okay thanks, I got a similar issue. I will keep updating if I found a good/better solution.

alexgilbertDG avatar Nov 30 '18 18:11 alexgilbertDG

@jmalley @alexgilbertDG

Does it work for you guys to cache the code? Like this:

{% cache globally using key "global-variables-in-dynamic-field" %} { "value":"none" , "label":"N/A" }, {% for block in eventHost.eventLocations.order('locName').limit(null) %} { "value":"{{ block.locName }}" , "label":"{{ block.locName }}" } {% if not loop.last %},{% endif %} {% endfor %} {% endcache %}

Tim-Wils avatar Jan 25 '19 10:01 Tim-Wils

I end up using a custom php cache solution so I can clear it on Event by Craft and exposed it with a twig variable. But I suppose it will work with this cache tag also !

alexgilbertDG avatar Jan 25 '19 14:01 alexgilbertDG

That seemed to make a very noticeable difference, thank you! Is that just saving the dropdown values for future matrices after the first db request?

jmalley avatar Jan 26 '19 03:01 jmalley

It's saving the dropdown-data template on the first request and re-using it at the next ones. I think there should be a clear-cache-plugin for named caches though, so that caches/named caches can be c;leared on save events. Maybe I'll look into that in a short while.

Tim-Wils avatar Jan 28 '19 07:01 Tim-Wils

Ah makes sense. So I only need to cache it on the first instance of that dropdown in a page?

Let me know what you find out about the cache clearing if you don't mind. Thanks again.

jmalley avatar Jan 28 '19 17:01 jmalley