Php 慢速搜索查询WordPress
我有这个查询,当我减少过滤器时,它会变快,但当我增加过滤器时,它会变得太慢,请我需要使用所有这些过滤器使它变快。 因为我的细节是清楚的,但我写了一些行,因为它不允许我张贴我的问题Php 慢速搜索查询WordPress,php,mysql,wordpress,performance,Php,Mysql,Wordpress,Performance,我有这个查询,当我减少过滤器时,它会变快,但当我增加过滤器时,它会变得太慢,请我需要使用所有这些过滤器使它变快。 因为我的细节是清楚的,但我写了一些行,因为它不允许我张贴我的问题 $args = array('post_type' => 'property', 'posts_per_page' => 1,'order' => 'asc', 'meta_query' => array(
$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'
),
)
);
最后我找到了一个解决方案,我在这里分享,因为其他人可能和我有同样的问题
$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;
不幸的是,我没有看到在一个查询中有那么多类似于的比较会很快。根据您正在搜索的数据集的大小,您可能希望为用户提供一组用于大多数输入的预定义选项,然后使用=比较而不是LIKE。感谢您的评论,我应该使用什么来代替“LIKE”?而不是
LIKE
,只需使用=
。我认为,state
、hospitaltype
和facilitytype
肯定可以切换到该模式,除非您允许人们搜索isconsi
或其他内容。您还可以从$wp_query
获取实际的SQL查询,并尝试删除或删除包含数据的表。请提供(1)生成的SQL,以及(2)显示创建表。它搜索了大约21k篇文章,让我可以编辑我的问题