WP_Meta_Query causing long-running MySQL queries

I used the following WP_Meta_Query which queried posts for a specific custom fields pair.

$args=array(
            'meta_key'=> 'tnid_01',
            'meta_value'=>$tnid,
            'post_status'=>'publish',
            'post_type'=>'post',
            'orderby'=>'date',
            'order'=>'DESC',
            'posts_per_page' => -1,
        );

This query ran without a problem. Then i wanted to search for the same meta_value “$tnid” in a second meta_key, with the following code:

$args=array(
            'meta_query' => array(
            'relation' => 'OR',
                    array(
                        'key'     => 'tnid_01',
                        'value'   => $tnid,
                        'compare' => '=',
                    ),
                    array(
                        'key'     => 'tnid_02',
                        'value'   => $tnid,
                        'compare' => '=',
                    ),
            ),      
            'post_status'=>'publish',
            'post_type'=>'post',
            'orderby'=>'date',
            'order'=>'DESC',
            'posts_per_page' => -1,
        );

The code is working, but unfortunately my MySQL server cant handle the queries when the cache gets rebuild.

Now to my questions:

Is there a mistake in the second query that causes the mysql problems?

Is there a way to query more efficiently for 2 custom fields?

thx

(Additional Info: The site has 40.000 posts, and uses w3totalcache)

0

Leave a Comment