我真的不喜欢Full-Text只是因为我没有让它为我工作所以我决定使用长期比赛。我想知道是否有办法在速度上改进这段代码。我理解它的笨重,其中99%是垃圾。但我真正想要的就是可以通过我的整个表格查看并根据相关性对其进行排序。
if (isset($_GET['post_title'])) {
$colname_getPosts = $_GET['post_title'];
}
mysql_select_db($database_Main);
function test($string){
$output = '';
$array = explode(' ', $string);
foreach ($array as $arraytags) {$output .= ' OR MATCH(post_title, post_entry, post_author, tags)
AGAINST("%'.$arraytags.'"IN BOOLEAN MODE)'; }
$array = explode(' ', $string);
foreach ($array as $arraytags) {$output .= ' OR MATCH(post_title, post_entry, post_author, tags)
AGAINST("%'.$arraytags.'%"IN BOOLEAN MODE)'; }
$array = explode(' ', $string);
foreach ($array as $arraytags) {$output .= ' OR MATCH(post_title, post_entry, post_author, tags)
AGAINST("'.$arraytags.'%"IN BOOLEAN MODE)'; }
$array = explode(' ', $string);
foreach ($array as $arraytags) {$output .= ' OR MATCH(post_title, post_entry, post_author, tags)
AGAINST("'.$arraytags.'"IN BOOLEAN MODE)'; }
return $output;
}
$query_getPosts = "
SELECT post_id, post_date, post_title, post_author, post_entry, tags, sum(relevance)
FROM (
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 10 AS relevance FROM blog_posts WHERE MATCH(post_title) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 2 AS relevance FROM blog_posts WHERE MATCH(post_author) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)".test($colname_getPosts)."
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 5 AS relevance FROM blog_posts WHERE MATCH(post_entry) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE)
UNION
SELECT post_id, post_date, post_title, post_author, post_entry, tags, 7 AS relevance FROM blog_posts WHERE MATCH(tags) AGAINST('%$colname_getPosts%' IN BOOLEAN MODE))results
GROUP BY post_id, post_date, post_title, post_author, post_entry, tags ORDER BY relevance DESC";
如何改进它,是一个更简单的代码?
答案 0 :(得分:1)
没有必要有4个foreach循环并且会爆炸4次,因为它每次都是相同的数组。
function test($string) {
$output = '';
$array = explode(' ', $string);
foreach ($array as $arraytags) {
$output .= ' OR MATCH(post_title, post_entry, post_author, tags) AGAINST("%'.$arraytags.'"IN BOOLEAN MODE)';
$output .= ' OR MATCH(post_title, post_entry, post_author, tags) AGAINST("%'.$arraytags.'%"IN BOOLEAN MODE)';
$output .= ' OR MATCH(post_title, post_entry, post_author, tags) AGAINST("'.$arraytags.'%"IN BOOLEAN MODE)';
$output .= ' OR MATCH(post_title, post_entry, post_author, tags) AGAINST("'.$arraytags.'"IN BOOLEAN MODE)';
}
return $output;
}
这会做同样的事情,通过数组的次数是nessesary的4倍。 你的格式需要一些工作,所有字母拼凑在一起很难理解。