I have a form with 20 inputs that runs WP_Query and recieves results via ajax.
For some reason:
- query gets considerably slower when 5+ inputs are filled
- It takes like 10 - 15s with 7 input values
- 15 - 25s with 8 input values and it increases in big chuncks with every next input I've filled
- Finally there's like 15 - 20 values and it times out (server should have 300s timeout)
- Value itself doesn't matter, same results with 1, 4353452 or asadsghthsthhhgfshhds
- Ajax shouldn't have any effect because if there's like 2 inputs filled, there much more data to return, ajax is still fast while file size is bigger. More filled inputs just makes query string longer but it should return less data
There's currently 6 tax_queries
and 14 meta_queries
in total.
What could cause this?
Update:
I have 10 example posts, it used to take 20 seconds with 8 filled inputs and it timed out (over 300s) with 20 inputs Now, just after 1 change it takes 0.0012 seconds. Im having a feeling that there was also something else involved because I eliminated foreach
and switch
in the process and hardcoded everything with if
and else if
.
Changes I've made so far:
- Was:
$meta_query[] = array(
'key' => 'some_key',
'value' => $some_value,
'type' => 'NUMERIC',
'compare' => '>='
);
$meta_query[] = array(
'key' => 'some_key',
'value' => $some_value,
'type' => 'NUMERIC',
'compare' => '<='
);
- Is:
if ( $value_one && $value_two ) {
$meta_query[] = array(
'key' => 'some_key',
'value' => array( $value_one, $value_two ),
'type' => 'NUMERIC',
'compare' => 'BETWEEN'
);
}
else if ( $value_one && ! $value_two ) {
$meta_query[] = array(
'key' => 'some_key',
'value' => $value_one,
'type' => 'NUMERIC',
'compare' => '>='
);
}
else if ( ! $value_one && $value_two ) {
$meta_query[] = array(
'key' => 'some_key',
'value' => $value_two,
'type' => 'NUMERIC',
'compare' => '<='
);
}
- I also eliminated keyword search which uses
LIKE
operator and it basically searches every post for 2 rounds - titles and content. I figured while user have so many search possibilites, such expensive move is kind of pointless. Im sure that user prefers speed over keyword search.