admin管理员组

文章数量:1429037

Hey i have a problem this my code and if i used with all parameters i get from $_GET its start work so slowly..... (like 5 min).

$checkbox = array(
    'immediate'=>'כניסה מידית',
    'air'=>'מיזוג',
    'furniture'=>'ריהוט',
    'renovated'=>'משופץ',
    'elevator'=>'מעלית',
    'parking'=>'חניה'
);

    if($_GET['mainCat']>0){$cat = array(array('taxonomy' => 'category-board', 'field' => 'term_id','terms' => $_GET['mainCat']));}
    if($_GET['fromNumberOfRooms']>0){$fromNumberOfRooms = array('key' => 'number-of-rooms','value' => $_GET['fromNumberOfRooms'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToNumberOfRooms']>0){$upToNumberOfRooms = array('key' => 'number-of-rooms','value' => $_GET['upToNumberOfRooms'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['fromSize']>0){$fromSize = array('key' => 'size','value' => $_GET['fromSize'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToSize']>0){$upToSize = array('key' => 'size','value' => $_GET['upToSize'],'compare' => '<=', 'type' => 'NUMERIC');}
    if($_GET['fromPrice']>0){$fromPrice = array('key' => 'price','value' => $_GET['fromPrice'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToPrice']>0){$upToPrice = array('key' => 'price','value' => $_GET['upToPrice'],'compare' => '<=', 'type' => 'NUMERIC');}

    foreach($checkbox as $key=>$val) {
        if ($_GET[$key] >= 1) {
            $$key = array('key' => $key, 'value' => $_GET[$key], 'compare' => '>=', 'type' => 'NUMERIC');
        }
    }
*/
    $args = array(
        'post_type' => 'board',  'tax_query' => $cat,
        'meta_query' => array($fromPrice, $upToPrice, $fromSize, $upToSize, $fromNumberOfRooms, $upToNumberOfRooms, $immediate, $air, $furniture, $renovated, $elevator, $parking)
    );

    query_posts( $args );

Hey i have a problem this my code and if i used with all parameters i get from $_GET its start work so slowly..... (like 5 min).

$checkbox = array(
    'immediate'=>'כניסה מידית',
    'air'=>'מיזוג',
    'furniture'=>'ריהוט',
    'renovated'=>'משופץ',
    'elevator'=>'מעלית',
    'parking'=>'חניה'
);

    if($_GET['mainCat']>0){$cat = array(array('taxonomy' => 'category-board', 'field' => 'term_id','terms' => $_GET['mainCat']));}
    if($_GET['fromNumberOfRooms']>0){$fromNumberOfRooms = array('key' => 'number-of-rooms','value' => $_GET['fromNumberOfRooms'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToNumberOfRooms']>0){$upToNumberOfRooms = array('key' => 'number-of-rooms','value' => $_GET['upToNumberOfRooms'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['fromSize']>0){$fromSize = array('key' => 'size','value' => $_GET['fromSize'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToSize']>0){$upToSize = array('key' => 'size','value' => $_GET['upToSize'],'compare' => '<=', 'type' => 'NUMERIC');}
    if($_GET['fromPrice']>0){$fromPrice = array('key' => 'price','value' => $_GET['fromPrice'],'compare' => '>=', 'type' => 'NUMERIC');}
    if($_GET['upToPrice']>0){$upToPrice = array('key' => 'price','value' => $_GET['upToPrice'],'compare' => '<=', 'type' => 'NUMERIC');}

    foreach($checkbox as $key=>$val) {
        if ($_GET[$key] >= 1) {
            $$key = array('key' => $key, 'value' => $_GET[$key], 'compare' => '>=', 'type' => 'NUMERIC');
        }
    }
*/
    $args = array(
        'post_type' => 'board',  'tax_query' => $cat,
        'meta_query' => array($fromPrice, $upToPrice, $fromSize, $upToSize, $fromNumberOfRooms, $upToNumberOfRooms, $immediate, $air, $furniture, $renovated, $elevator, $parking)
    );

    query_posts( $args );
Share Improve this question asked Oct 6, 2015 at 12:33 Mr.SmithMr.Smith 11 bronze badge 4
  • Don't use query_posts() - ever. – Nicolai Grossherr Commented Oct 6, 2015 at 12:40
  • so... with what yes? – Mr.Smith Commented Oct 6, 2015 at 13:00
  • I replaced the "query_posts" in "WP_Query" and yet it works slowly – Mr.Smith Commented Oct 7, 2015 at 8:26
  • I said nothing about this step making it faster, still it is a good thing you changed it. Meta queries can slow down the query, 5 minutes are really slow though. One option could be to just get the IDs, using 'fields => 'ids', of course only if that is applicable for you. Furthermore, I'm guessing you have a lot of database entries, if not your problem has another origin. Generally your database can be very big, but then you server has to be powerful. – Nicolai Grossherr Commented Oct 7, 2015 at 8:58
Add a comment  | 

1 Answer 1

Reset to default 1

When you have a few thousand entries and use post_type in combination with meta values, the MySQL query starts running very, very slowly because it seems to do a full table scan on the wp_postmeta table. The most direct solution is to add an index on the wp_postmeta table to avoid the full scan.

This is the SQL to add the index:

ALTER TABLE `wp_postmeta` ADD INDEX `key_value` (`meta_key`(20), `meta_value`(20)) USING BTREE;

The speed improvement was somewhere around 100x. I needed the first twenty characters of each field -- you could alter it to be more or less.

本文标签: custom post typesUsed with metaquery in queryposts works slowly