在mysql / php搜索引擎中结果太多而结果很慢

时间:2012-04-10 11:41:06

标签: php mysql

好的,所以我可能会看到为什么我会得到很多结果,因为我使用了MATCH查询。我想要的是输出相关的搜索。例如,如果搜索是“对绿色感兴趣”,那么它只会输出那些有兴趣或绿色或IT的表格中的结果,如果这是有意义的。

现在我得到所有东西的结果,另外它真的很慢。我不想使用除mysql / php之外的任何东西来实现这一点,因此没有像Lucene或替代品那样的第三方搜索引擎。

这是搜索代码:

    <?php

mysql_connect("webhost", "user", "pass") or die(mysql_error());
mysql_select_db("replies") or die(mysql_error());

// Retrieve result using term
$term = $_POST['term'];

$sql = mysql_query("SELECT * from 'replies' `where interest_in_green_it` MATCH '%$term%' or `green_it_good_thing MATCH` '%$term%' or    `green_it_save_environment` MATCH '%    $term%' or  `green_it_save_money` MATCH '%$term%' or    `green_it_incentive` MATCH '%$term%' or `uel_green_it MATCH` '%$term%' or   `MATCH_green_it` MATCH '%$term%' or     `printing_costs` MATCH '%$term%' or `travel_costs` MATCH '%$term%' or   `comments` MATCH '%$term%' or   `uel_green_modules` MATCH '%$term%' or  `year_of_study` MATCH '%    $term%' or      `questionnaire_filled` MATCH '%$term%'");

while ($row = mysql_fetch_array){
echo 'ID: '.$row['ID'];
echo '<br/> Do you have an interest in green IT?: '.$row['interest_in_green_it'];
echo '<br/> Do you think green IT is a good thing?: '.$row['green_it_good_thing'];
echo '<br/> Would you consider green IT if it meant saving the environment?: '.$row['green_it_save_environment'];
echo '<br/> Would you consider green IT if it meant saving money?: '.$row['green_it_save_money'];
echo '<br/> What would be the better incentive to practice green IT?: '.$row['green_it_incentive'];
echo '<br/> DO you think UEL is doing enough to practice green IT? : '.$row['uel_green_it'];
echo '<br/> Do you like green IT?: '.$row['like_green_it'];
echo '<br/> Your estimated monthly travel costs to UEL: '.$row['travel_costs'];
echo '<br/> Your estimated printing costs at UEL at any one time: '.$row['printing_costs'];
echo '<br/> Your comments: '.$row['comments'];
echo '<br/> Would you like to see more green modules at UEL?: '.$row['uel_green_modules'];
echo '<br/> What is your year of study?: '.$row['year_of_study'];
echo '<br/> If you did not fill in the questionnaire why not?: '.$row['questionnaire_filled'];
echo '<br/><br/>';
}

$_GET = array_map('trim', $_GET); 
$_POST = array_map('trim', $_POST); 
$_COOKIE = array_map('trim', $_COOKIE); 
$_REQUEST = array_map('trim', $_REQUEST); 
if(get_magic_quotes_gpc()): 
$_GET = array_map('stripslashes', $_GET); 
$_POST = array_map('stripslashes', $_POST); 
$_COOKIE = array_map('stripslashes', $_COOKIE); 
$_REQUEST = array_map('stripslashes', $_REQUEST); 
endif; 
$_GET = array_map('mysql_real_escape_string', $_GET); 
$_POST = array_map('mysql_real_escape_string', $_POST); 
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE); 
$_REQUEST = array_map('mysql_real_escape_string', $_REQUEST); 

?>

提前谢谢

1 个答案:

答案 0 :(得分:2)

“我该如何改进此查询?”

使用index。此处FULLTEXT index可能是有用的。

我相信最新的InnoDB引擎也支持FULLTEXT索引。 (MySQL 5.6 +)

要获得所有可能的结果,请计算一个分数,以确定元组与您的条件匹配的程度以及ORDER BY score DESC LIMIT 20,以获得最佳的20个匹配。有关示例,请参阅this answer