【问题标题】:Slow speed Search Query WordPress慢速搜索查询WordPress
【发布时间】:2021-07-23 04:12:19
【问题描述】:

我有这个查询,当我减少过滤器时它变得很快,但是当我增加过滤器时它变得太慢,请我需要使用所有这些过滤器使其快速。 由于我的详细信息很清楚,但我正在写一些行,因为它不允许我发布我的问题

$args = array('post_type' => 'property', 'posts_per_page' => 1,'order' => 'asc', 
            'meta_query'    => array(
                            'relation'      => 'AND',
                                  array(
                                     'key'      => 'facilityname',
                                     'value'        => $_GET['keyword'],
                                     'compare'  => 'LIKE'
                                      ),
                                       array(
                                     'key'      => 'city',
                                     'value'        => $_GET['city'],
                                     'compare'  => 'LIKE'
                                      ),
                                       array(
                                     'key'      => 'facilitytype',
                                     'value'        => $_GET['facilitytype'],
                                     'compare'  => 'LIKE'
                                      ),
                                       array(
                                     'key'      => 'hospitaltype',
                                     'value'        => $_GET['hospitaltype'],
                                     'compare'  => 'LIKE'
                                      ),
                                       array(
                                     'key'      => 'state',
                                     'value'        => $_GET['state'],
                                     'compare'  => 'LIKE'
                                      ),
                                     
                                       array(
                                     'key'      => 'idn',
                                     'value'        => $_GET['idn'],
                                     'compare'  => 'LIKE'
                                      ),
                                       array(
                                     'key'      => 'gpoaffiliations',
                                     'value'        => $_GET['gpoaffiliations'],
                                     'compare'  => 'LIKE'
                                      ),
                                        array(
                                     'key'      => 'bedcounttotal',
                                     'value'        =>  array($_GET['min-bedcounttotal'], $_GET['max-bedcounttotal']),
                                      'compare' => 'BETWEEN',
                                      'type' => 'NUMERIC'
                                      ),
                                        array(
                                     'key'      => 'zipcode',
                                     'value'        =>  array($_GET['minzipcode'], $_GET['maxzipcode']),
                                      'compare' => 'BETWEEN',
                                      'type' => 'NUMERIC'
                                      ),
                                        array(
                                     'key'      => 'operatingroomcount',
                                     'value'        =>  array($_GET['minoperatingroomcount'], $_GET['maxoperatingroomcount']),
                                      'compare' => 'BETWEEN',
                                      'type' => 'NUMERIC'
                                      ),
                                     
                                     
                                      
                             )
          );

【问题讨论】:

  • 不幸的是,我没有看到在一个查询中进行这么多 LIKE 比较的情况可能会很快。根据您正在搜索的数据集的大小,您可能希望为用户提供一组预定义的选项,用于大多数输入,然后使用 = 比较而不是 LIKE。
  • 感谢您的评论,那么我应该用什么来代替“LIKE”?
  • 而不是LIKE,只需使用=。我认为statehospitaltypefacilitytype 绝对可以切换到那个,除非你让人们搜索isconsi 或其他东西。您还可以从$wp_query 获取实际的 SQL 查询,并尝试使用manually optimize it 或保存数据的表
  • 请提供(1)生成的SQL,和(2)SHOW CREATE TABLE。
  • 搜索大约 21k 个帖子让我可以编辑我的问题

标签: php mysql wordpress performance


【解决方案1】:

终于找到了解决办法,在这里分享给和我一样可能有同样问题的人

$meta_query = array();
    $args = array();
    $args = array('posts_per_page' => -1,);
    $meta_query[] = array(
        'key' => 'facilityname',
        'value' => $_GET['keyword'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'city',
        'value' => $_GET['city'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'facilitytype',
        'value' => $_GET['facilitytype'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'hospitaltype',
        'value' => $_GET['hospitaltype'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'state',
        'value' => $_GET['state'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'idn',
        'value' => $_GET['idn'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'gpoaffiliations',
        'value' => $_GET['gpoaffiliations'],
        'compare' => 'LIKE'
    );
    $meta_query[] = array(
        'key' => 'bedcounttotal',
        'value'     =>  array($_GET['min-bedcounttotal'], $_GET['max-bedcounttotal']),
        'compare'   => 'BETWEEN',
        'type' => 'NUMERIC'
    );
    $meta_query[] = array(
        'key' => 'zipcode',
        'value'     =>  array($_GET['minzipcode'], $_GET['maxzipcode']),
        'compare'   => 'BETWEEN',
        'type' => 'NUMERIC'
    );
    $meta_query[] = array(
        'key' => 'operatingroomcount',
        'value'     =>  array($_GET['minoperatingroomcount'], $_GET['maxoperatingroomcount']),
        'compare'   => 'BETWEEN',
        'type' => 'NUMERIC'
    );
    //if there is more than one meta query 'or' them
    if(count($meta_query) > 1) {
        $meta_query['relation'] = 'AND';
    }
    
    // The Query
    $args['post_type'] = "property";
    $args['meta_query'] = $meta_query;

【讨论】:

    猜你喜欢
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2013-12-02
    • 2017-06-30
    • 2014-03-24
    • 2020-02-24
    • 2011-03-07
    相关资源
    最近更新 更多