首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有许多WP_Query参数的meta_query需要很长时间才能加载

具有许多WP_Query参数的meta_query需要很长时间才能加载
EN

WordPress Development用户
提问于 2020-07-16 18:23:31
回答 2查看 778关注 0票数 0

我有一个带有search form参数的wp_query,在这种形式中有很多checkbox

问题从这里开始,加载select all选项需要很长时间,这个查询几乎需要一分钟的时间才能完成。以前有人有过这个问题吗?

我在HTML中使用了这些代码

代码语言:javascript
复制
  Real-time Antivirus 

.
.
.
  adware_prevention 

Apply filter
在functions.php中add_action('wp_ajax_myfilter', 'misha_filter_function'); // wp_ajax_{ACTION HERE} 
add_action('wp_ajax_nopriv_myfilter', 'misha_filter_function');
 
function misha_filter_function(){
    $args = array(
        'posts_per_page' =>-1,
    );
    $args['meta_query'] = array( 'relation'=>'AND' );
    // Antivirus_featured_scaning
    if( isset( $_POST['real_time_antivirus'] ) && $_POST['real_time_antivirus'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'real_time_antivirus',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['manual_virus_scanning'] ) && $_POST['manual_virus_scanning'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'manual_virus_scanning',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['usb_virus_scan'] ) && $_POST['usb_virus_scan'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'usb_virus_scan',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['registry_startup_scan'] ) && $_POST['registry_startup_scan'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'registry_startup_scan',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['auto_virus_scanning'] ) && $_POST['auto_virus_scanning'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'auto_virus_scanning',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['scheduled_scan'] ) && $_POST['scheduled_scan'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_scaning',
            'value' => 'scheduled_scan',
            'compare' => 'LIKE'
        );
    // antivirus_featured_threat
    if( isset( $_POST['anti_spyware'] ) && $_POST['anti_spyware'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_spyware',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['anti_worm'] ) && $_POST['anti_worm'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_worm',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['anti_trojan'] ) && $_POST['anti_trojan'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_trojan',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['anti_rootkit'] ) && $_POST['anti_rootkit'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_rootkit',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['anti_phishing'] ) && $_POST['anti_phishing'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_phishing',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['anti_spam'] ) && $_POST['anti_spam'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'anti_spam',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['email_protection'] ) && $_POST['email_protection'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'email_protection',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['chat_im_protection'] ) && $_POST['chat_im_protection'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'chat_im_protection',
            'compare' => 'LIKE'
        );
    if( isset( $_POST['adware_prevention'] ) && $_POST['adware_prevention'] == 'on' )
        $args['meta_query'][] = array(
            'key' => 'antivirus_antivirus_featured_threat',
            'value' => 'adware_prevention',
            'compare' => 'LIKE'
        );
 

    $query = new WP_Query( $args );谢谢你的帮助。
EN

回答 2

WordPress Development用户

发布于 2020-07-16 18:36:35

是啊,我可以想象那个查询会很无聊。

造成这种情况的主要原因是,针对post元值的每一个额外条件都可能导致wp_posts表和wp_posts表之间的一个额外的SQL,而联接可能会导致成倍的开销。我很惊讶,如果你选择了所有这些选项,它甚至会马上回来。随着wp_postmeta表大小的增加,这个问题会变得更糟。

这么说,不清楚为什么要在这里使用LIKE而不是=作为比较值,除非确实需要匹配包含value参数的字符串?这可能会在速度上产生很大的差异,因为LIKE=在SQL中的开销更高。

除此之外,由于该查询在WP_Query中的内部工作方式,您可能很难使其运行得更快。您可能需要一个不同的数据结构,或者在不使用WP_Query和wp_postmeta表的键值结构的情况下进行更多的自定义操作。或者您可以考虑限制用户可以选择的这些选项的数量。

HTH

票数 0
EN

WordPress Development用户

发布于 2020-07-17 09:36:19

查询非常慢,因为它通过post元值搜索帖子。从根本上讲,使用meta_query是缓慢的,而且在数据库服务器上非常繁重和昂贵。当数据库中的帖子数量增加时,它会变得更慢。这就是为什么当站点很小时,这些查询运行得很快,但随着时间的推移会变得更慢。

如果要存储数据以筛选或搜索帖子,请使用自定义分类法。

  • 当您已经知道Post ID时,post元优化用于查找值。
  • 当您已经知道值时,可以优化分类法以查找帖子。

如果您使用的是ACF或字段管理器,可以告诉这些插件对这些字段使用分类法

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/371213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档