3

when we have 10 plus meta key i get 100% server usage. is there any option to optimize query.

 $meta_query = array('relation' => 'AND');
    foreach ($requestData as  $key => $value) {
        $meta_query[] = array(
            'key' => $value,
            'value' => '1',
            'compare' => '='
        );
    }

    $args = array( 'post_type' => 'healthcare', 'post_status' => 'publish', 'posts_per_page' => -1, 'meta_query' => $meta_query);

    $loop = new WP_Query( $args );
    $total = $loop->found_posts;
3

Complex meta queries with a large number of meta values to find can be slow. Each meta key you query for generates one more JOIN in the query, and if you have a large database, it can be slow. You can try few things:

  • Optimize your database tables, it is possible that you have a lot of orphaned data that can contribute to database query speed.
  • Try limiting the number of posts query returns, if you have a lot of records in the results set that can cause speed problems too.
  • Contact your hosting (or do it yourself if you have access) to check MySQL slow query log to see why the query is slow, and maybe do some optimization on the MySQL server settings for better caching.
  • You are right. There were multiple tickets and a long discussion about it, and a patch has been applied in v. 4.1. But seems like there are not many improvements. Here is the subsequent ticket. – Max Yudin Feb 9 '18 at 17:09
  • @MaxYudin To be honest, there won't be any improvements, because custom fields are not meant to be used in complex searches. But if you really need it, then you can use more complex, custom tables and custom SQL search queries. – Krzysiek Dróżdż May 23 '18 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.