EnexoOnoma
EnexoOnoma

Reputation: 8836

Slow speed and high CPU usage for my code. How to improve it?

this is my code. It gets the variables from the url. It is very slow and sometimes don't even complete. How can I improve it?

Note: Most of the times some of the variables will not have any value. I thought that if I check this and remove it's array from the query it will improve it somehow.

$value1 = $_GET['extra1'];
$value2 = $_GET['extra2'];
$value3 = $_GET['extra3'];
$value4 = $_GET['extra4'];
$value5 = $_GET['extra5'];
$value6 = $_GET['extra6'];
$value7 = $_GET['extra7'];
$value8 = $_GET['extra8'];
$value9 = $_GET['extra9'];
$value10 = $_GET['extra10'];

$args = array(
    'meta_query' => array(
        array(
            'key' => extra1,
            'value' => $value1,
            'compare' => '='
        ),
        array(
            'key' => extra2,
            'value' => $value2,
            'compare' => '='
        ),
        array(
            'key' => extra3,
            'value' => $value3,
            'compare' => '='
        ),
        array(
            'key' => extra4,
            'value' => $value4,
            'compare' => '='
        ),
        array(
            'key' => extra5,
            'value' => $value5,
            'compare' => '='
        ),
        array(
            'key' => extra6,
            'value' => $value6,
            'compare' => '='
        ),
        array(
            'key' => extra7,
            'value' => $value7,
            'compare' => '='
        ),
        array(
            'key' => extra8,
            'value' => $value8,
            'compare' => '='
        ),
        array(
            'key' => extra9,
            'value' => $value9,
            'compare' => '='
        ),
        array(
            'key' => extra10,
            'value' => $value10,
            'compare' => '='
        )
    )
 );
$query = new WP_Query( $args );

if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post();
        the_title();
    endwhile;
endif;

Upvotes: 0

Views: 387

Answers (2)

EMI
EMI

Reputation: 826

You are probably requesting a slow query from the database.

Enable the slow query log and use it to find what that query looks like and then find a way to improve it.

http://dev.mysql.com/doc/refman/5.1/en/slow-query-log.html

Here's a profiler but it's probably just going to tell you what I just told you:

http://www.xdebug.org/docs/profiler

Unrelated, but loops are useful.

for ($i = 1; array_key_exists('extra'. $i, $_GET); $i++) {
    $args['meta_query'][] = array(
        'key' => 'extra'. $i,
        'value' => $_GET['extra'. $i],
        'compare' => '=',
    );
}

Upvotes: 1

rjha94
rjha94

Reputation: 4318

what does new WP_Query($args) do? I do not think the code above that would be slow (no matter how horribly written) you can put few print statements printing out the time in microseconds.

Put one right at top, one just before new WP_Query, one immediately after it and one right at the end. I suspect WP_QUERY is the one taking time.

Upvotes: 0

Related Questions