这是我第一次尝试构建比仅使用 LIKE 功能更复杂的搜索功能。此搜索返回的结果非常完美,但运行速度非常慢。有什么我可以明智地改进代码以加快速度或我应该在数据库上查看的任何内容吗?还是我需要考虑更多的服务器功能?
非常感谢任何和所有的帮助。非常感谢!
function new_live_search($q){
$title_score = 5;
$tags_score = 10;
$upvote_score = 1;
$subdomain = $this->config->item('subdomain_name');
$query = "SELECT DISTINCT results.*,
(
".$title_score."*(MATCH(title) AGAINST('$q' IN BOOLEAN MODE)) +
".$tags_score."*(MATCH(tags.name) AGAINST('$q' IN BOOLEAN MODE)) +
".$upvote_score."*usefulness
) AS score
FROM results
LEFT JOIN tags ON results.id = tags.result_id
WHERE (scope = 'all' OR scope = '$subdomain')
AND (published = 1)";
$query .= "
HAVING score - usefulness > 0
ORDER BY score DESC, title";
$query = $this->db->query($query);
$results = array();
foreach ($query->result() as $row){
$results[] = $row;
}
return $results;
}