加快WordPress的慢速查询速度

问题描述 投票:0回答:1

我使用插件WP Job Manager为我的网站编写了一个自定义相关的职位空缺查询。使用查询监视器时,我一直认为查询非常慢(5秒以上)。

我已经实现了查询缓存,除了这需要查询至少运行一次。

是否有可能加快这样的查询速度?

    $location = get_post_meta($id, '_job_location', true);
    $category = get_post_meta($id, 'job_category_wo', true);
    $args = array(
    'post_type'  => 'job_listing',
    'numberposts' => 5,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key'     => '_job_location',
            'value'   => $location,
            'compare' => '='
        ),
        array(
            'relation' => 'AND',
            array(
                'key'     => '_job_location',
                'value'   => $location,
                'compare' => '='
            ),
            array(
                'key'     => 'job_category_wo',
                'value'   => $category[0],
                'compare' => 'LIKE'
            )
        )
    )
);
$postslist = get_posts( $args );
mysql wordpress performance
1个回答
1
投票

在您显示的示例中,您搜索符合此条件的帖子:

_job_location = $region OR _job_location = $region AND job_category_wo LIKE $category[0]

您可以将此表达式简化为:

_job_location = $region

布尔代数说(A) OR (A AND B)是多余的。它和(A)一样。

这将分解OR操作,这对SQL来说很难优化。

P.S。:我认为你的例子中还有其他错误。你设置一个变量$location但后来你使用$region。这是什么?

此外,您没有清理任何值以防止SQL注入。

© www.soinside.com 2019 - 2024. All rights reserved.