Home Forums General Issues Optimize slow queries Reply To: Optimize slow queries

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