Php mySQL查询似乎使服务器崩溃
我有一个mySQL查询来处理WordPress中相当复杂的搜索,因为我一直在努力让Php mySQL查询似乎使服务器崩溃,php,mysql,sql,wordpress,Php,Mysql,Sql,Wordpress,我有一个mySQL查询来处理WordPress中相当复杂的搜索,因为我一直在努力让wp\u Query做我需要的一切 但是,有时查询需要很长时间才能运行(有时10秒以上!),有时它似乎只是使服务器崩溃(media temple Grid),返回内部服务器错误或建立数据库连接错误 我不确定查询中是否存在导致其崩溃的一般语法错误,但基本上生成查询的PHP如下所示: <?php // declare wordpress database global global $wpdb; // ord
wp\u Query
做我需要的一切
但是,有时查询需要很长时间才能运行(有时10秒以上!),有时它似乎只是使服务器崩溃(media temple Grid),返回内部服务器错误
或建立数据库连接错误
我不确定查询中是否存在导致其崩溃的一般语法错误,但基本上生成查询的PHP如下所示:
<?php
// declare wordpress database global
global $wpdb;
// order by option
$order = $_SESSION['search']['sort-by'];
// users lat, long and distance preferences
$lat = $_SESSION['search']['lat'];
$long = $_SESSION['search']['long'];
$radius = $_SESSION['search']['distance'];
// user search start/end date
$startDate = date('Ymd', strtotime($_SESSION['search']['from']));
$endDate = date('Ymd', strtotime($_SESSION['search']['to']));
// get the main category search ID
$maincat = get_term_by( 'slug', $_SESSION['search']['cat'], 'main-cat');
$maincat = $maincat->term_taxonomy_id;
// grab keywords, replace special chars and spaces
$keywords = $_SESSION['search']['keyword'];
$keywords = preg_replace('/[^A-Za-z0-9 ,]/u','', strip_tags($keywords));
$keywords = str_replace(' ', '', $keywords);
// put keywords into array
$subcatItems = explode(',', $keywords);
$keywords = $subcatItems;
// for each keywords get the sub category id's
$subcats = array();
$count = count($subcatItems) - 2;
for ($i = 0; $i <= $count; $i++) {
$subcatItems[$i] = get_term_by( 'slug', $subcatItems[$i], 'sub-cat');
if ($subcatItems[$i] != '') :
$subcatItems[$i] = $subcatItems[$i]->term_taxonomy_id;
array_push($subcats, $subcatItems[$i]);
endif;
}
if( $subcats != '' ) :
$subcats = implode(',', $subcats);
endif;
// select
$query = 'SELECT SQL_CALC_FOUND_ROWS wp_posts.*, ';
// geo locate
$query .= '( 3959 * acos(
cos( radians('.$lat.') )
* cos( radians( lat ) )
* cos( radians( lng ) - radians('.$long.') )
+ sin( radians('.$lat.') )
* sin( radians( lat ) )
) )
AS distance , lat AS latitude , lng AS longitude ';
// from
$query .= 'FROM wp_posts ';
// inner joins
$query .= 'INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) ';
if ( $keywords != '' ) :
$query .= 'INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) ';
endif;
$query .= 'INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) ';
$query .= 'INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) ';
// if ordered by price, join post meta again
if( $order == 'mt2.meta_value+0' ) :
$query .= 'INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id) ';
endif;
// if there are keywords
if ( $keywords != '' ) :
$query .= 'INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id) ';
endif;
// join table to geo locate
$query .= 'INNER JOIN lat_lng_post ON wp_posts.ID = lat_lng_post.post_id ';
// basic filter
$query .= 'WHERE 1=1 ';
$query .= 'AND wp_posts.post_type = "event" ';
$query .= 'AND wp_posts.post_status = "publish" ';
// geo filter
$query .= 'AND lat_lng_post.lat = lat ';
$query .= 'AND lat_lng_post.lng = lng ';
// date filter
$query .= 'AND ( ';
$query .= '(wp_postmeta.meta_key LIKE "date_%_start-date" AND CAST(wp_postmeta.meta_value AS SIGNED) <= "'.$endDate.'") ';
$query .= 'AND (mt1.meta_key LIKE "date_%_end-date" AND CAST(mt1.meta_value AS SIGNED) >= "'.$startDate.'") ';
$query .= 'AND substr(wp_postmeta.meta_key, 1, 6) = substr(mt1.meta_key, 1, 6) ';
$query .= ') ';
// taxonomies filter
$query .= 'AND ( wp_term_relationships.term_taxonomy_id IN ('.$maincat.') ) ';
// if keywords
if ( $_SESSION['search']['keyword'] != '' ) :
$query .= 'AND ( ';
// for each keyword, and a statement to check post title
$keywordCount = 0;
foreach ( $keywords as $keyword ) :
if( $keyword != '' ) :
if( $keywordCount == 0 ) :
$query .= '(wp_posts.post_title LIKE "%'.$keyword.'%") ';
else :
$query .= 'OR (wp_posts.post_title LIKE "%'.$keyword.'%") ';
endif;
endif;
$keywordCount++;
endforeach;
// for each keyword, and a statement to check description
foreach ( $keywords as $keyword ) :
if( $keyword != '' ) :
$query .= 'OR (mt3.meta_key = "description" AND mt3.meta_value LIKE "%'.$keyword.'%") ';
endif;
endforeach;
// for each keyword, and a statement to check sub category taxonomy
if( $subcats != '' ) :
$query .= 'OR ( tt1.term_taxonomy_id IN ('.$subcats.') )';
endif;
$query .= ') ';
endif;
// if ordered by adult
if( $order == 'mt2.meta_value+0' ) :
$query .= 'AND mt2.meta_key = "adult" ';
endif;
// grouping and sorting
$query .= 'GROUP BY wp_posts.ID ';
$query .= 'HAVING distance <= '.$radius.' ';
$query .= 'ORDER BY '.$order.' ASC ';
$query .= 'LIMIT 0, 10';
$events = $wpdb->get_results( $query, 'OBJECT' );
?>
编辑2
我刚才的另一个想法是,将查询拆分为单独的查询会更快吗?那么,首先完成地理查询,获取那些帖子ID并进行日期查询,然后进行关键字查询,也许?我对mySQL很陌生,所以不太确定如何优化它:/尝试将最终条件添加到连接中(多个连接条件) 如果在连接处使用多个条件,而不是获取一个庞大的集合,然后将该条件放在末尾,则集合会更小,响应时间也会更好 例如,您有一个最终条件:
AND wp_posts.post_type = "event"
你可以把它放在第一个连接中
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) AND wp_posts.post_type = "event"
INNER JOIN...
等等。嗯,这是一个非常沉重的问题。也许将距离计算移出mysql会有所帮助。有可能将其移出mysql吗?它必须检查的所有纬度和经度都存储在数据库中:/mysql服务器的慢速查询日志你看过了吗?要排除常规SQL语法错误,可以在调用
$events=$wpdb->get_results($query,'OBJECT')之前将生成的查询附加到日志文件中代码>。借助此日志,您可以尝试借助EXPLAIN
来优化查询。我实际上没有研究过这一点,我曾经尝试将EXPLAIN添加到查询中,但它从未运行过,我不确定这是否与$wpdb有关?这确实会给mySQL带来语法错误,所以老实说,我想他们会出现在那里。谢谢你的建议,我现在就去试试:)嗨,刚刚尝试了一下,在网站初始加载之后,它似乎有帮助,我猜其中一些是缓存的,但是第一次加载仍然很大,我仍然收到500个错误:/you't put“。还有距离。你能检查一下时间响应是否真的变好了吗?(例如,将该命令放在phpmyadmin或类似文件上)我将查看一下,看看执行时间是否有差异:)
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) AND wp_posts.post_type = "event"
INNER JOIN...