与QUERY_POSTS中的META_QUERY一起使用运行缓慢

时间:2015-10-06 作者:Mr.Smith

嘿,我有一个问题,这是我的代码,如果我使用从$获得的所有参数,那么它的启动速度会很慢。。。。。(大约5分钟)。

$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 );

1 个回复
SO网友:JRalph

当您有数千个条目并使用post_type 结合元值,MySQL查询开始运行得非常非常慢,因为它似乎在wp_postmeta 桌子最直接的解决方案是在wp_postmeta 表以避免完全扫描。

这是添加索引的SQL:

ALTER TABLE `wp_postmeta` ADD INDEX `key_value` (`meta_key`(20), `meta_value`(20)) USING BTREE;
速度提高了大约100倍。我需要每个字段的前二十个字符--您可以将其更改为或多或少。