Home Forums General Issues Optimize slow queries


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()) {
        // 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


    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.

  • 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?


  • That’s a very helpful plugin, thanks mate.

  • Here 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.

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Optimize slow queries’ is closed to new replies.