Home › Forums › General Issues › Optimize slow queries
Hey,
I’ve 545 queries from the acf plugin. Sometimes the query monitor detect some of this as slow queries. Is there an way to speed up or optimize the queries for faster page load?
It depends on the query. All queries based base on meta_value are going to be slower. The reason for this is that the meta_value field in the DB is not an indexed field.
The more meta values you’re searching on, the slower your query will be.
Some types of queries are even most costly, for example “LIKE” queries on the meta_value is going to be slower than ‘=’
There really isn’t a way to “optimize” these queries any more than what is being done. The only way to improve these is to
1) Write your own SQL to get what you need and query the db directly instead of depending on WP
2) Pre index the search results for each possible search and store these results for use instead of doing the query.
On the other hand…
If you’re talking about the queries that ACF runs when you call get_field() or one of the other functions for getting values, it depends on the type of field. There are some fields that will be slower than others.
For example, an image field, if you have ACF set to return and array or image object, this will be slow because ACF has to do a lot of queries to build that image array. When it comes to image fields you are better off only returning the attachment ID and then using WP functions to only get what you need. The same is true of any of the fields that can return WP objects like taxonomy fields, post object fields, relationship fields. For performance you will always be better off getting the ID and then using it to call WP functions yourself. The reason is that ACF is probably getting a lot more information than you really need and doing extra queries to get this unneeded information.
In addition to this, if you are getting values for a post that is not the current post in the loop… example:
if (have_posts()) {
while (have_posts()) {
the_post();
// now you get several values for another post
// that is not the global $post in this loop
}
}
This will add additional queries, before getting any values using get_field(), get_sub_field(), etc you might see a reduction in the number of queries performed by calling
get_post_meta($other_post_id);
What this does is to get all of the meta values for the other post in a single query rather than doing a query for each field. This may or may not give you an improvement, I have yet to figure out under what conditions WP does this automatically and when it does not, but if WP has already loaded all of the meta values for a post, calling the function in the manner will not create any additional queries, so it can’t really hurt.
@hube2 Thank you for taking time to helping me. I’ve figured out some interesting articles and examples, how I can optimize the queries:
https://www.billerickson.net/advanced-custom-fields-frontend-dependency/
https://support.advancedcustomfields.com/forums/topic/get_post_meta-works-but-get_fields-doesnt/
https://codelight.eu/improving-advanced-custom-fields-repeater-performance/
http://blog.codebusters.pl/en/wordpress-advanced-custom-fields-optimization/
https://support.advancedcustomfields.com/forums/topic/optimize-slow-queries/
http://codegist.net/search/wordpress-acf-get-field/6
I wanted to respond to this even with it being resolved because I think everyone should read Eliza Witkowsk article and look at the ACF-JSON solution. Its literally a 2 minute implementation.
We are building an ACF driven modular website requiring Flexible Content. We did a test with a page having all 15 modules added and it originally showing 175 Queries and a 10 second load time locally (we use a remote DB). Our remote site was varying from 2-4 seconds.
We did the JSON technique and we are now 50 Queries, with a 2.75s locally and 1.75s remotely. This is UNCACHED.
Big thanks to Wootimes for posting this and Eliza for writing it!
Hey @fugamma,
I’m creating themes with ACF. In my case, the JSON variant is slower as the PHP variant.
This are my results:
Loading fields from PHP exported file: Total Queries > 704
Loading fields from JSON: Total Queries > 851
Loading fields without JSON or PHP exported file: Total Queries > 1197
The winner is loading fields from PHP exported file 🙂
Hi @wootimes
Just out of my interest, would you mind sharing your tricks of returning the Total Queries? Are these queries database queries?
Thanks
He
@he-wang yes, it’s easy by installing this plugin https://de.wordpress.org/plugins/query-monitor/
Here https://wphave.de/acf-performance-optimieren-ohne-abhangigkeit-im-frontend/ I have listed some ACF functions and explained how you can instead use the default WordPress function to increase performance on the frontend without calling ACF functions here.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.